Class: ISO8601::Atom Abstract

Inherits:
Object
  • Object
show all
Includes:
Comparable
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.


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

def initialize(atom, base = nil)
  fail ISO8601::Errors::TypeError,
       "The atom argument for #{self.class} should be a Numeric value." unless atom.is_a?(Numeric)
  fail 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?
  @atom = atom
  @base = base
end

Instance Attribute Details

#atomObject (readonly)

Returns the value of attribute atom


23
24
25
# File 'lib/iso8601/atoms.rb', line 23

def atom
  @atom
end

#baseObject (readonly)

Returns the value of attribute base


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

def base
  @base
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Parameters:

  • other (Atom)

    The contrast to compare against

Returns:

  • (-1, 0, 1)

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

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)

82
83
84
# File 'lib/iso8601/atoms.rb', line 82

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

#factorObject

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


94
95
96
97
# File 'lib/iso8601/atoms.rb', line 94

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

#hashFixnum

Returns:

  • (Fixnum)

88
89
90
# File 'lib/iso8601/atoms.rb', line 88

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

#to_fFloat

The float representation

Returns:

  • (Float)

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

def to_f
  atom.to_f
end

#to_iInteger

The integer representation

Returns:

  • (Integer)

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

def to_i
  atom.to_i
end

#to_sString

Returns the ISO 8601 representation for the atom

Returns:

  • (String)

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

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

#to_secondsNumeric

The amount of seconds

Returns:

  • (Numeric)

64
65
66
# File 'lib/iso8601/atoms.rb', line 64

def to_seconds
  atom * factor
end

#valueNumeric

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

Returns:

  • (Numeric)

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

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