Class: ISO8601::Atom Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/iso8601/atoms.rb

Overview

This class is abstract.

A generic atom in a Duration

Direct Known Subclasses

Days, Hours, Minutes, Months, Seconds, Weeks, Years

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(atom, base = nil) ⇒ Atom

Returns a new instance of Atom.

Parameters:

  • atom (Numeric)

    The atom value

  • base (ISO8601::DateTime, nil) (defaults to: nil)

    (nil) The base datetime to compute the atom factor.



13
14
15
16
17
18
# File 'lib/iso8601/atoms.rb', line 13

def initialize(atom, base = nil)
  fail TypeError, "The atom argument for #{inspect} should be a Numeric value." unless atom.is_a?(Numeric)
  fail TypeError, "The base argument for #{inspect} should be a ISO8601::DateTime instance or nil." unless base.is_a?(ISO8601::DateTime) || base.nil?
  @atom = atom
  @base = base
end

Instance Attribute Details

#atomObject (readonly)

Returns the value of attribute atom.



19
20
21
# File 'lib/iso8601/atoms.rb', line 19

def atom
  @atom
end

#baseObject (readonly)

Returns the value of attribute base.



20
21
22
# File 'lib/iso8601/atoms.rb', line 20

def base
  @base
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)


61
62
63
# File 'lib/iso8601/atoms.rb', line 61

def ==(other)
  (hash == other.hash)
end

#eql?(other) ⇒ Boolean

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)


68
69
70
# File 'lib/iso8601/atoms.rb', line 68

def eql?(other)
  (hash == other.hash)
end

#factorObject

The atom factor to compute the amount of seconds for the atom



78
79
80
81
# File 'lib/iso8601/atoms.rb', line 78

def factor
  fail NotImplementedError,
       "The #factor method should be implemented by each subclass"
end

#hashFixnum

Returns:

  • (Fixnum)


73
74
75
# File 'lib/iso8601/atoms.rb', line 73

def hash
  [atom, self.class].hash
end

#to_fFloat

The float representation

Returns:

  • (Float)


32
33
34
# File 'lib/iso8601/atoms.rb', line 32

def to_f
  atom.to_f
end

#to_iInteger

The integer representation

Returns:

  • (Integer)


25
26
27
# File 'lib/iso8601/atoms.rb', line 25

def to_i
  atom.to_i
end

#to_sString

Returns the ISO 8601 representation for the atom

Returns:

  • (String)


39
40
41
# File 'lib/iso8601/atoms.rb', line 39

def to_s
  (value.zero?) ? '' : "#{value}#{symbol}"
end

#to_secondsNumeric

The amount of seconds

Returns:

  • (Numeric)


54
55
56
# File 'lib/iso8601/atoms.rb', line 54

def to_seconds
  atom * factor
end

#valueNumeric

The simplest numeric representation. If modulo equals 0 returns an integer else a float.

Returns:

  • (Numeric)


47
48
49
# File 'lib/iso8601/atoms.rb', line 47

def value
  (atom % 1).zero? ? atom.to_i : atom
end