Repository navigation
Add bounded Date runtime for Spinel #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Default ActiveRecord JSON shape for the Spinel runtime. This is kept | ||
| # beside the bounded Date rather than in the shared runtime because the | ||
| # Ruby-family overlay supplies its own reflection-aware implementation. | ||
| module ActiveRecord | ||
| class Base | ||
| def as_json(options = {}) | ||
| only = options && options[:only] | ||
| only ||= self.class.schema_columns | ||
| _as_json_only(only) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| # A deliberately small, program-defined Date for the Spinel target. | ||
| # This is not Ruby's stdlib `date` package. The supported API is the | ||
| # surface analyzed for Rails date columns; unsupported formats fail | ||
| # loudly instead of being interpreted as timestamps or guessed dates. | ||
| class Date | ||
| class Error < ArgumentError | ||
| end | ||
|
|
||
| MONTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | ||
| WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] | ||
| SHORT_WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] | ||
| MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] | ||
| SHORT_MONTH_NAMES = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] | ||
|
|
||
| def initialize(year, month, day) | ||
| unless year >= 1 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 && day <= Date.month_length(year, month) | ||
| raise Date::Error, "invalid date" | ||
| end | ||
| @year = year | ||
| @month = month | ||
| @day = day | ||
| end | ||
|
|
||
| def self.civil(year, month = 1, day = 1) | ||
| Date.new(year, month, day) | ||
| end | ||
|
|
||
| def self.leap_year?(year) | ||
| year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) | ||
| end | ||
|
|
||
| def self.month_length(year, month) | ||
| return 29 if month == 2 && Date.leap_year?(year) | ||
| MONTHS[month - 1] | ||
| end | ||
|
|
||
| def self.iso8601(value) | ||
| unless value.is_a?(String) && value.length == 10 && value[4, 1] == "-" && value[7, 1] == "-" | ||
| raise Date::Error, "invalid date: #{value}" | ||
| end | ||
| year = value[0, 4].to_i | ||
| month = value[5, 2].to_i | ||
| day = value[8, 2].to_i | ||
| date = Date.new(year, month, day) | ||
| raise Date::Error, "invalid date: #{value}" unless date.iso8601 == value | ||
| date | ||
| rescue ArgumentError | ||
| raise Date::Error, "invalid date: #{value}" | ||
| end | ||
|
|
||
| def self.parse(value, _comp = true) | ||
| Date.iso8601(value) | ||
| end | ||
|
|
||
| def self.strptime(value, format) | ||
| raise Date::Error, "unsupported date format" unless format == "%Y-%m-%d" | ||
| Date.iso8601(value) | ||
| end | ||
|
|
||
| def self.today | ||
| now = Time.now | ||
| Date.new(now.year, now.month, now.day) | ||
| end | ||
|
|
||
| def year | ||
| @year | ||
| end | ||
|
|
||
| def month | ||
| @month | ||
| end | ||
|
|
||
| def mon | ||
| @month | ||
| end | ||
|
|
||
| def day | ||
| @day | ||
| end | ||
|
|
||
| def mday | ||
| @day | ||
| end | ||
|
|
||
| def leap? | ||
| Date.leap_year?(@year) | ||
| end | ||
|
|
||
| def wday | ||
| offsets = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4] | ||
| year = @year | ||
| year -= 1 if @month < 3 | ||
| (year + year / 4 - year / 100 + year / 400 + offsets[@month - 1] + @day) % 7 | ||
| end | ||
|
|
||
| def yday | ||
| total = @day | ||
| month = 1 | ||
| while month < @month | ||
| total += Date.month_length(@year, month) | ||
| month += 1 | ||
| end | ||
| total | ||
| end | ||
|
|
||
| def sunday? | ||
| wday == 0 | ||
| end | ||
|
|
||
| def monday? | ||
| wday == 1 | ||
| end | ||
|
|
||
| def tuesday? | ||
| wday == 2 | ||
| end | ||
|
|
||
| def wednesday? | ||
| wday == 3 | ||
| end | ||
|
|
||
| def thursday? | ||
| wday == 4 | ||
| end | ||
|
|
||
| def friday? | ||
| wday == 5 | ||
| end | ||
|
|
||
| def saturday? | ||
| wday == 6 | ||
| end | ||
|
|
||
| def >>(months) | ||
| shift_months(months) | ||
| end | ||
|
|
||
| def <<(months) | ||
| shift_months(-months) | ||
| end | ||
|
|
||
| def shift_months(amount) | ||
| index = @year * 12 + @month - 1 + amount | ||
| year = index / 12 | ||
| month = index % 12 + 1 | ||
| year -= 1 if month == 0 | ||
| month = 12 if month == 0 | ||
| day = @day | ||
| last = Date.month_length(year, month) | ||
| day = last if day > last | ||
| Date.new(year, month, day) | ||
| end | ||
|
|
||
| def to_date | ||
| self | ||
| end | ||
|
|
||
| def to_time | ||
| Time.local(@year, @month, @day, 0, 0, 0) | ||
| end | ||
|
|
||
| def iso8601 | ||
| format("%04d-%02d-%02d", @year, @month, @day) | ||
| end | ||
|
|
||
| def to_s | ||
| iso8601 | ||
| end | ||
|
|
||
| def as_json(_options = {}) | ||
| iso8601 | ||
| end | ||
|
|
||
| def strftime(pattern) | ||
| result = "" | ||
| index = 0 | ||
| while index < pattern.length | ||
| char = pattern[index, 1] | ||
| if char == "%" | ||
| index += 1 | ||
| code = pattern[index, 1] | ||
| value = case code | ||
| when "%" then "%" | ||
| when "Y" then format("%04d", @year) | ||
| when "m" then format("%02d", @month) | ||
| when "d" then format("%02d", @day) | ||
| when "e" then format("%2d", @day) | ||
| when "F" then iso8601 | ||
| when "j" then format("%03d", yday) | ||
| when "w" then wday.to_s | ||
| when "u" then (wday == 0 ? 7 : wday).to_s | ||
| when "a" then SHORT_WEEKDAYS[wday] | ||
| when "A" then WEEKDAYS[wday] | ||
| when "b", "h" then SHORT_MONTH_NAMES[@month - 1] | ||
| when "B" then MONTH_NAMES[@month - 1] | ||
| else raise Date::Error, "unsupported strftime directive: %#{code}" | ||
| end | ||
| result += value | ||
| else | ||
| result += char | ||
| end | ||
| index += 1 | ||
| end | ||
| result | ||
| end | ||
|
|
||
| def <=>(other) | ||
| return nil unless other.is_a?(Date) | ||
| return -1 if @year < other.year | ||
| return 1 if @year > other.year | ||
| return -1 if @month < other.month | ||
| return 1 if @month > other.month | ||
| return -1 if @day < other.day | ||
| return 1 if @day > other.day | ||
| 0 | ||
| end | ||
|
|
||
| def ==(other) | ||
| (self <=> other) == 0 | ||
| end | ||
|
|
||
| def <(other) | ||
| (self <=> other) == -1 | ||
| end | ||
|
|
||
| def <=(other) | ||
| (self <=> other) <= 0 | ||
| end | ||
|
|
||
| def >(other) | ||
| (self <=> other) == 1 | ||
| end | ||
|
|
||
| def >=(other) | ||
| (self <=> other) >= 0 | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Bounded Date value used by the Spinel runtime for Rails date columns. | ||
| class Date | ||
| class Error < ArgumentError | ||
| end | ||
|
|
||
| def initialize: (Integer year, Integer month, Integer day) -> void | ||
| def self.civil: (Integer year, ?Integer month, ?Integer day) -> Date | ||
| def self.iso8601: (String value) -> Date | ||
| def self.parse: (String value, ?bool comp) -> Date | ||
| def self.strptime: (String value, String format) -> Date | ||
| def self.today: () -> Date | ||
| def self.leap_year?: (Integer year) -> bool | ||
| def self.month_length: (Integer year, Integer month) -> Integer | ||
|
|
||
| def year: () -> Integer | ||
| def month: () -> Integer | ||
| def mon: () -> Integer | ||
| def day: () -> Integer | ||
| def mday: () -> Integer | ||
| def leap?: () -> bool | ||
| def wday: () -> Integer | ||
| def yday: () -> Integer | ||
| def sunday?: () -> bool | ||
| def monday?: () -> bool | ||
| def tuesday?: () -> bool | ||
| def wednesday?: () -> bool | ||
| def thursday?: () -> bool | ||
| def friday?: () -> bool | ||
| def saturday?: () -> bool | ||
| def >>: (Integer months) -> Date | ||
| def <<: (Integer months) -> Date | ||
| def shift_months: (Integer amount) -> Date | ||
| def to_date: () -> Date | ||
| def to_time: () -> Time | ||
| def iso8601: () -> String | ||
| def to_s: () -> String | ||
| def as_json: (?Hash[untyped, untyped] options) -> String | ||
| def strftime: (String pattern) -> String | ||
| def <=>: (Date? other) -> Integer? | ||
| def ==: (Date? other) -> bool | ||
| def <: (Date? other) -> bool | ||
| def <=: (Date? other) -> bool | ||
| def >: (Date? other) -> bool | ||
| def >=: (Date? other) -> bool | ||
| end | ||
|
|
||
| module ActiveRecord | ||
| class Base | ||
| def as_json: (?Hash[Symbol, untyped] options) -> Hash[String, untyped] | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 4621
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 23868
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 39807
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 11509
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 39630
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 18862
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 18841
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 12696
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 41874
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 20532
🏁 Script executed:
sed -n '3985,4045p' src/project.rsRepository: rubys/roundhouse
Length of output: 2573
🏁 Script executed:
Repository: rubys/roundhouse
Length of output: 3823
🏁 Script executed:
rg -n -C 7 'def self\.iso8601|def iso8601|iso8601' runtime/spinel/date.rbRepository: rubys/roundhouse
Length of output: 2258
Normalize blank date strings before parsing.
The Hash-based
update/update!path sends a present date value throughformat_db_date. Forupdate!(due_on: ""), Spinel’sDate.iso8601rejects the empty string and raisesDate::Errorinstead of clearing the nullable date.parse_db_datealready maps the empty date representation to nil.Suggested fix
def self.format_db_date(value) return nil if value.nil? + return nil if value.is_a?(String) && value == "" return value.iso8601 if value.is_a?(Date)🤖 Prompt for AI Agents