Class: Parse::ParseDate

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/parse/date.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#string_keyed_hash

Constructor Details

#initialize(hash = {}) ⇒ ParseDate

Returns a new instance of ParseDate.



20
21
22
23
24
# File 'lib/parse/date.rb', line 20

def initialize hash={}
  hash = string_keyed_hash hash
  @raw_hash = hash
  @time = ::Time.parse hash['iso'] if hash.has_key? 'iso'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



45
46
47
# File 'lib/parse/date.rb', line 45

def method_missing name, *args, &block
  @time.__send__ name, *args, &block
end

Instance Attribute Details

#timeObject

Returns the value of attribute time.



18
19
20
# File 'lib/parse/date.rb', line 18

def time
  @time
end

Class Method Details

.parse(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/parse/date.rb', line 7

def parse *args
  if args.size == 1 && args.first.is_a?(String)
    new :iso => args.first
  else
    new.tap do |dt|
      dt.time = Time.gm *args
    end
  end
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
# File 'lib/parse/date.rb', line 26

def <=> other
  self.time <=> other.time
end

#to_hObject



30
31
32
33
34
35
# File 'lib/parse/date.rb', line 30

def to_h
  {
    "__type" => "Date",
    "iso" => @time.iso8601
  }
end

#to_json(*args) ⇒ Object



37
38
39
# File 'lib/parse/date.rb', line 37

def to_json *args
  to_h.to_json
end

#to_sObject



41
42
43
# File 'lib/parse/date.rb', line 41

def to_s
  to_json
end