Module: ISO8601::Atomic

Includes:
Comparable
Included in:
Days, Hours, Minutes, Months, Seconds, Weeks, Years
Defined in:
lib/iso8601/atomic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#atomObject (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, ...

Parameters:

  • other (Atom)

    The contrast to compare against

Returns:

  • (-1, 0, 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

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)


56
57
58
# File 'lib/iso8601/atomic.rb', line 56

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

#hashFixnum

Returns:

  • (Fixnum)


62
63
64
# File 'lib/iso8601/atomic.rb', line 62

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

#to_fFloat

The float representation

Returns:

  • (Float)


21
22
23
# File 'lib/iso8601/atomic.rb', line 21

def to_f
  atom.to_f
end

#to_iInteger

The integer representation

Returns:

  • (Integer)


13
14
15
# File 'lib/iso8601/atomic.rb', line 13

def to_i
  atom.to_i
end

#to_sString

Returns the ISO 8601 representation for the atom

Returns:

  • (String)


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

Returns:

  • (Boolean)

Raises:



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

Returns:

  • (Boolean)

Raises:



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

#valueNumeric

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

Returns:

  • (Numeric)


38
39
40
# File 'lib/iso8601/atomic.rb', line 38

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