lib/repeatable/schedule.rb in repeatable-0.2.0 vs lib/repeatable/schedule.rb in repeatable-0.2.1

- old
+ new

@@ -1,17 +1,17 @@ require 'active_support/core_ext/string/inflections' module Repeatable class Schedule - def initialize(args) - case args - when Hash - @expression = build_expression(args) + def initialize(arg) + case arg when Repeatable::Expression::Base - @expression = args + @expression = arg + when Hash + @expression = Parser.call(arg) else - fail ArgumentError, "Can't build a Repeatable::Schedule from #{args.class}" + fail(ArgumentError, "Can't build a Repeatable::Schedule from #{arg.class}") end end def occurrences(start_date, end_date) start_date = Date(start_date) @@ -37,28 +37,7 @@ end private attr_reader :expression - - def build_expression(hash) - if hash.length != 1 - fail "Invalid expression: '#{hash}' should have single key and value" - else - expression_for(*hash.first) - end - end - - def expression_for(key, value) - klass = "repeatable/expression/#{key}".classify.safe_constantize - case klass - when nil - fail "Unknown mapping: Can't map key '#{key.inspect}' to an expression class" - when Repeatable::Expression::Set - args = value.map { |hash| build_expression(hash) } - klass.new(*args) - else - klass.new(value) - end - end end end