Class: Saper::Items::Time

Inherits:
Saper::Item show all
Defined in:
lib/saper/items/time.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Saper::Item

[], exists?, inherited, subclasses, try, type, #type

Constructor Details

#initialize(vremya) ⇒ Time


35
36
37
# File 'lib/saper/items/time.rb', line 35

def initialize(vremya)
  @vremya = vremya
end

Class Method Details

.new(item, format = nil, tz = nil) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/saper/items/time.rb', line 6

def self.new(item, format = nil, tz = nil)
  super case item
  when Text
    parse(item.to_s, format, tz)
  when String
    parse(item, format, tz)
  when Vremya
    item
  else
    raise(Errors::InvalidItem, item)
  end
end

.now(tz = nil) ⇒ Object


30
31
32
# File 'lib/saper/items/time.rb', line 30

def self.now(tz = nil)
  new ::Vremya.now(tz)
end

.parse(string, format, tz = nil) ⇒ Object


20
21
22
23
24
25
26
27
# File 'lib/saper/items/time.rb', line 20

def self.parse(string, format, tz = nil)
  string = string.to_s.gsub(/\s+/," ").strip
  begin
    ::Vremya.parse(string, format, tz)
  rescue
    raise(Errors::InvalidItem, string)
  end
end

Instance Method Details

#==(other) ⇒ Object


40
41
42
# File 'lib/saper/items/time.rb', line 40

def ==(other)
  to_i == other.to_i || to_s == other.to_s
end

#serializeObject


60
61
62
# File 'lib/saper/items/time.rb', line 60

def serialize
  @vremya.to_utc.to_time
end

#to_iObject


55
56
57
# File 'lib/saper/items/time.rb', line 55

def to_i
  to_native.to_i
end

#to_nativeObject


50
51
52
# File 'lib/saper/items/time.rb', line 50

def to_native
  @vremya.to_time
end

#to_sObject


45
46
47
# File 'lib/saper/items/time.rb', line 45

def to_s
  @vremya.iso8601
end