Skip to content

Commit

Permalink
RDoc for additions Date and DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
BurdetteLamar committed Nov 21, 2023
1 parent 5c1be59 commit 64681aa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
32 changes: 26 additions & 6 deletions lib/json/add/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@

class Date

# Deserializes JSON string by converting Julian year <tt>y</tt>, month
# <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
# See #as_json.
def self.json_create(object)
civil(*object.values_at('y', 'm', 'd', 'sg'))
end

alias start sg unless method_defined?(:start)

# Returns a hash, that will be turned into a JSON object and represent this
# object.
# Methods <tt>Date#as_json</tt> and +Date.json_create+ may be used
# to serialize and deserialize a \Date object;
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
#
# \Method <tt>Date#as_json</tt> serializes +self+,
# returning a 2-element hash representing +self+:
#
# require 'json/add/date'
# x = Date.today.as_json
# # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
#
# \Method +JSON.create+ deserializes such a hash, returning a \Date object:
#
# Date.json_create(x)
# # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
#
def as_json(*)
{
JSON.create_id => self.class.name,
Expand All @@ -26,8 +39,15 @@ def as_json(*)
}
end

# Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
# <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
# Returns a JSON string representing +self+:
#
# require 'json/add/date'
# puts Date.today.to_json
#
# Output:
#
# {"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
#
def to_json(*args)
as_json.to_json(*args)
end
Expand Down
35 changes: 26 additions & 9 deletions lib/json/add/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
end
require 'date'

class DateTime
class DateTimeTime

# Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
# See #as_json.
def self.json_create(object)
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
of_a, of_b = object['of'].split('/')
Expand All @@ -23,8 +21,21 @@ def self.json_create(object)

alias start sg unless method_defined?(:start)

# Returns a hash, that will be turned into a JSON object and represent this
# object.
# Methods <tt>DateTime#as_json</tt> and +DateTime.json_create+ may be used
# to serialize and deserialize a \DateTime object;
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
#
# \Method <tt>DateTime#as_json</tt> serializes +self+,
# returning a 2-element hash representing +self+:
#
# require 'json/add/datetime'
# x = DateTime.now.as_json
# # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
#
# \Method +JSON.create+ deserializes such a hash, returning a \DateTime object:
#
# DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
#
def as_json(*)
{
JSON.create_id => self.class.name,
Expand All @@ -39,9 +50,15 @@ def as_json(*)
}
end

# Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
# Returns a JSON string representing +self+:
#
# require 'json/add/datetime'
# puts DateTime.now.to_json
#
# Output:
#
# {"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
#
def to_json(*args)
as_json.to_json(*args)
end
Expand Down

0 comments on commit 64681aa

Please sign in to comment.