Class: Frequency

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

Defined Under Namespace

Classes: Undefined

Constant Summary collapse

FREQUENCIES =

Notes:

  • biweekly is ambiguous so I left it out

  • biennially != biannually

Values checked with Apple Dictionary Version 2.1.1 (80.1).

{
  "each second"   => [315_360_000,   true ],
  "each minute"   => [  5_256_000,   true ],
  "hourly"        => [     87_600,   true ],
  "each hour"     => [     87_600,   false],
  "daily"         => [      3_650,   true ],
  "each day"      => [      3_650,   false],
  "weekly"        => [        520,   true ],
  "each week"     => [        520,   false],
  "fortnightly"   => [        260,   true ],
  "monthly"       => [        120,   true ],
  "each month"    => [        120,   false],
  "quarterly"     => [         40,   true ],
  "each quarter"  => [         40,   false],
  "biannually"    => [         20,   true ],
  "semiannual"    => [         20,   false],
  "semi-annual"   => [         20,   false],
  "semiannually"  => [         20,   false],
  "annually"      => [         10,   true ],
  "each year"     => [         10,   false],
  "yearly"        => [         10,   false],
  "annual"        => [         10,   false],
  "biennially"    => [          5,   true ],
  "biennial"      => [          5,   false],
  "quadrennially" => [          2.5, true ],
  "quadrennial"   => [          2.5, false],
  "each decade"   => [          1,   true ],
  "decade"        => [          1,   false],
  "one time"      => [          0,   true ],
  "one-time"      => [          0,   false],
  "other"         => [        nil,   true ],
  "unknown"       => [        nil,   true ],
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Frequency



46
47
48
# File 'lib/frequency.rb', line 46

def initialize(string)
  self.plain_text = string ? string.downcase : ''
end

Instance Attribute Details

#plain_textObject

Returns the value of attribute plain_text.



5
6
7
# File 'lib/frequency.rb', line 5

def plain_text
  @plain_text
end

Class Method Details

.listObject

A list suitable for a drop-down list



93
94
95
96
97
98
99
100
101
# File 'lib/frequency.rb', line 93

def self.list
  list = []
  sortable = FREQUENCIES.select { |k, v| v[0] } # not nil
  sorted = sortable.sort_by { |k, v| -v[0] }
  sorted.each { |k, v| list << k if v[1] }
  nil_values = FREQUENCIES.select { |k, v| v[0].nil? }.sort_by { |k, v| k }
  nil_values.each { |k, v| list << k }
  list
end

Instance Method Details

#<(other) ⇒ Object



58
59
60
# File 'lib/frequency.rb', line 58

def <(other)
  compare_safely(:<, other)
end

#<=(other) ⇒ Object



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

def <=(other)
  compare_safely(:<=, other)
end

#<=>(other) ⇒ Object



54
55
56
# File 'lib/frequency.rb', line 54

def <=>(other)
  compare_safely(:<=>, other)
end

#==(other) ⇒ Object



50
51
52
# File 'lib/frequency.rb', line 50

def ==(other)
  compare_safely(:==, other)
end

#>(other) ⇒ Object



66
67
68
# File 'lib/frequency.rb', line 66

def >(other)
  compare_safely(:>, other)
end

#>=(other) ⇒ Object



70
71
72
# File 'lib/frequency.rb', line 70

def >=(other)
  compare_safely(:>=, other)
end

#per_decadeObject



74
75
76
77
78
79
# File 'lib/frequency.rb', line 74

def per_decade
  value = FREQUENCIES[plain_text]
  return nil unless value
  return nil unless value[0]
  value[0]
end

#per_yearObject



81
82
83
84
85
86
# File 'lib/frequency.rb', line 81

def per_year
  value = FREQUENCIES[plain_text]
  return nil unless value
  return nil unless value[0]
  value[0] / 10.0
end

#valid?Boolean



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

def valid?
  FREQUENCIES.keys.include?(self.plain_text)
end