Class: Spanner
- Inherits:
-
Object
- Object
- Spanner
- Defined in:
- lib/spanner.rb,
lib/spanner/version.rb
Constant Summary collapse
- ParseError =
Class.new(RuntimeError)
- VERSION =
"0.0.4"
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#raise_on_error ⇒ Object
readonly
Returns the value of attribute raise_on_error.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #error(err) ⇒ Object
-
#initialize(opts) ⇒ Spanner
constructor
A new instance of Spanner.
- #length_of_month ⇒ Object
- #parse(value) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Spanner
Returns a new instance of Spanner.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/spanner.rb', line 13 def initialize(opts) @value = value @on_error = opts && opts.key?(:on_error) ? opts[:on_error] : :raise @length_of_month = opts && opts[:length_of_month] @from = if opts && opts.key?(:from) case opts[:from] when :now Time.new.to_i else opts[:from].to_i end else 0 end end |
Instance Attribute Details
#from ⇒ Object (readonly)
Returns the value of attribute from.
11 12 13 |
# File 'lib/spanner.rb', line 11 def from @from end |
#raise_on_error ⇒ Object (readonly)
Returns the value of attribute raise_on_error.
11 12 13 |
# File 'lib/spanner.rb', line 11 def raise_on_error @raise_on_error end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
11 12 13 |
# File 'lib/spanner.rb', line 11 def value @value end |
Class Method Details
.days_in_month(year, month) ⇒ Object
30 31 32 |
# File 'lib/spanner.rb', line 30 def self.days_in_month(year, month) (Date.new(year, 12, 31) << (12 - month)).day end |
Instance Method Details
#error(err) ⇒ Object
38 39 40 41 42 |
# File 'lib/spanner.rb', line 38 def error(err) if on_error == :raise raise ParseError.new(err) end end |
#length_of_month ⇒ Object
34 35 36 |
# File 'lib/spanner.rb', line 34 def length_of_month @length_of_month ||= Spanner.parse("#{Spanner.days_in_month(Time.new.year, Time.new.month)} days") end |
#parse(value) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/spanner.rb', line 44 def parse(value) parts = [] part_contextualized = nil value.scan(/[\+\-]?(?:\d*\.\d+|\d+)|[a-z]+/i).each do |part| part_as_float = Float(part) rescue nil if part_as_float parts << part_as_float part_contextualized = nil else if part_contextualized error "Part has already been contextualized with #{part_contextualized}" return nil end if parts.empty? parts << 1 end # part is context multiplier = case part when "s", "sec", "seconds" then 1 when "m", "min", "minutes" then 60 when "h", "hours", "hrs" then 3600 when "d", "days" then 86_400 when "w", "wks", "weeks" then 604_800 when "months", "month", "M" then length_of_month when "years", "y" then 31_556_926 when /\As/ then 1 when /\Am/ then 60 when /\Ah/ then 3600 when /\Ad/ then 86_400 when /\Aw/ then 604_800 when /\AM/ then length_of_month when /\Ay/ then 31_556_926 end part_contextualized = part parts << (parts.pop * multiplier) end end if parts.empty? nil else value = parts.inject(from) { |s, p| s += p } value.ceil == value ? value.ceil : value end end |