Class: ISO8601::Atom Abstract
- Inherits:
-
Object
- Object
- ISO8601::Atom
- Defined in:
- lib/iso8601/atoms.rb
Overview
This class is abstract.
A generic atom in a Duration
Instance Attribute Summary collapse
-
#atom ⇒ Object
readonly
Returns the value of attribute atom.
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #eql?(other) ⇒ Boolean
-
#factor ⇒ Object
The atom factor to compute the amount of seconds for the atom.
- #hash ⇒ Fixnum
-
#initialize(atom, base = nil) ⇒ Atom
constructor
A new instance of Atom.
-
#to_f ⇒ Float
The float representation.
-
#to_i ⇒ Integer
The integer representation.
-
#to_s ⇒ String
Returns the ISO 8601 representation for the atom.
-
#to_seconds ⇒ Numeric
The amount of seconds.
-
#value ⇒ Numeric
The simplest numeric representation.
Constructor Details
#initialize(atom, base = nil) ⇒ Atom
Returns a new instance of Atom.
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
#atom ⇒ Object (readonly)
Returns the value of attribute atom.
19 20 21 |
# File 'lib/iso8601/atoms.rb', line 19 def atom @atom end |
#base ⇒ Object (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
61 62 63 |
# File 'lib/iso8601/atoms.rb', line 61 def ==(other) (hash == other.hash) end |
#eql?(other) ⇒ Boolean
68 69 70 |
# File 'lib/iso8601/atoms.rb', line 68 def eql?(other) (hash == other.hash) end |
#factor ⇒ Object
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 |
#hash ⇒ Fixnum
73 74 75 |
# File 'lib/iso8601/atoms.rb', line 73 def hash [atom, self.class].hash end |
#to_f ⇒ Float
The float representation
32 33 34 |
# File 'lib/iso8601/atoms.rb', line 32 def to_f atom.to_f end |
#to_i ⇒ Integer
The integer representation
25 26 27 |
# File 'lib/iso8601/atoms.rb', line 25 def to_i atom.to_i end |
#to_s ⇒ String
Returns the ISO 8601 representation for the atom
39 40 41 |
# File 'lib/iso8601/atoms.rb', line 39 def to_s (value.zero?) ? '' : "#{value}#{symbol}" end |
#to_seconds ⇒ Numeric
The amount of seconds
54 55 56 |
# File 'lib/iso8601/atoms.rb', line 54 def to_seconds atom * factor end |
#value ⇒ Numeric
The simplest numeric representation. If modulo equals 0 returns an integer else a float.
47 48 49 |
# File 'lib/iso8601/atoms.rb', line 47 def value (atom % 1).zero? ? atom.to_i : atom end |