Module: ISO8601::Atomic
- Includes:
- Comparable
- Defined in:
- lib/iso8601/atomic.rb
Instance Attribute Summary collapse
-
#atom ⇒ Object
readonly
Returns the value of attribute atom.
Instance Method Summary collapse
- #<=>(other) ⇒ -1, ...
- #eql?(other) ⇒ Boolean
- #hash ⇒ Fixnum
-
#to_f ⇒ Float
The float representation.
-
#to_i ⇒ Integer
The integer representation.
-
#to_s ⇒ String
Returns the ISO 8601 representation for the atom.
-
#valid_atom?(atom) ⇒ Boolean
Validates the atom is a Numeric.
- #valid_base?(base) ⇒ Boolean
-
#value ⇒ Numeric
The simplest numeric representation.
Instance Attribute Details
#atom ⇒ Object (readonly)
Returns the value of attribute atom.
7 8 9 |
# File 'lib/iso8601/atomic.rb', line 7 def atom @atom end |
Instance Method Details
#<=>(other) ⇒ -1, ...
46 47 48 49 50 |
# File 'lib/iso8601/atomic.rb', line 46 def <=>(other) return nil unless other.is_a?(self.class) to_f <=> other.to_f end |
#eql?(other) ⇒ Boolean
56 57 58 |
# File 'lib/iso8601/atomic.rb', line 56 def eql?(other) (hash == other.hash) end |
#hash ⇒ Fixnum
62 63 64 |
# File 'lib/iso8601/atomic.rb', line 62 def hash [atom, self.class].hash end |
#to_f ⇒ Float
The float representation
21 22 23 |
# File 'lib/iso8601/atomic.rb', line 21 def to_f atom.to_f end |
#to_i ⇒ Integer
The integer representation
13 14 15 |
# File 'lib/iso8601/atomic.rb', line 13 def to_i atom.to_i end |
#to_s ⇒ String
Returns the ISO 8601 representation for the atom
29 30 31 |
# File 'lib/iso8601/atomic.rb', line 29 def to_s value.zero? ? '' : "#{value}#{symbol}" end |
#valid_atom?(atom) ⇒ Boolean
Validates the atom is a Numeric
68 69 70 71 |
# File 'lib/iso8601/atomic.rb', line 68 def valid_atom?(atom) raise(ISO8601::Errors::TypeError, "The atom argument for #{self.class} should be a Numeric value.") \ unless atom.is_a?(Numeric) end |
#valid_base?(base) ⇒ Boolean
73 74 75 76 |
# File 'lib/iso8601/atomic.rb', line 73 def valid_base?(base) raise(ISO8601::Errors::TypeError, "The base argument for #{self.class} should be a ISO8601::DateTime instance or nil.") \ unless base.is_a?(ISO8601::DateTime) || base.nil? end |
#value ⇒ Numeric
The simplest numeric representation. If modulo equals 0 returns an integer else a float.
38 39 40 |
# File 'lib/iso8601/atomic.rb', line 38 def value (atom % 1).zero? ? atom.to_i : atom end |