Class: Temporal::Classification

Inherits:
Object
  • Object
show all
Defined in:
lib/temporals/types.rb

Direct Known Subclasses

Month, WDay

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Classification

Returns a new instance of Classification.



31
32
33
34
# File 'lib/temporals/types.rb', line 31

def initialize(word)
  @name = word.is_a?(String) ? self.class.normalize(word) : self.class.order[word]
  @ord  = self.class.order.index(@name)
end

Class Attribute Details

.orderObject (readonly)

Returns the value of attribute order.



12
13
14
# File 'lib/temporals/types.rb', line 12

def order
  @order
end

.translationsObject (readonly)

Returns the value of attribute translations.



12
13
14
# File 'lib/temporals/types.rb', line 12

def translations
  @translations
end

Instance Attribute Details

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



27
28
29
# File 'lib/temporals/types.rb', line 27

def name
  @name
end

#ordObject (readonly)

Returns the value of attribute ord.



27
28
29
# File 'lib/temporals/types.rb', line 27

def ord
  @ord
end

Class Method Details

.abbreviationsObject



14
15
16
17
18
19
# File 'lib/temporals/types.rb', line 14

def abbreviations
  @abbreviations ||= translations.inject({}) do |h,(k,v)|
    h[v] = k unless h.has_key?(v) && h[v].length < k.length
    h
  end
end

.normalize(word) ⇒ Object



21
22
23
24
# File 'lib/temporals/types.rb', line 21

def normalize(word)
  word = word.capitalize
  order.include?(word) ? word : (translations.has_key?(word) ? translations[word] : nil)
end

Instance Method Details

#<=>(other) ⇒ Object



36
37
38
# File 'lib/temporals/types.rb', line 36

def <=>(other)
  ord <=> other.ord
end

#==(other) ⇒ Object



39
40
41
# File 'lib/temporals/types.rb', line 39

def ==(other)
  ord == other.ord && name == other.name
end