Class: IceCube::HashInput

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

Defined Under Namespace

Classes: Mash

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashInput

Returns a new instance of HashInput.



22
23
24
# File 'lib/ice_cube/hash_input.rb', line 22

def initialize(hash)
  @input = Mash.new(hash)
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/ice_cube/hash_input.rb', line 26

def [](key)
  @input[key]
end

#intervalObject



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

def interval
  @input[:interval] || 1
end

#limit_countObject



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

def limit_count
  @input[:count]
end

#limit_timeObject



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

def limit_time
  @limit_time ||= TimeUtil.deserialize_time(@input[:until])
end

#rule_classObject



40
41
42
43
44
# File 'lib/ice_cube/hash_input.rb', line 40

def rule_class
  return @rule_class if @rule_class
  match = @input[:rule_type].match(/::(\w+Rule)$/)
  @rule_class = IceCube.const_get(match[1]) if match
end

#to_ruleObject



30
31
32
33
34
35
36
37
38
# File 'lib/ice_cube/hash_input.rb', line 30

def to_rule
  return nil unless rule_class
  rule = rule_class.new(interval, week_start)
  rule.until(limit_time) if limit_time
  rule.count(limit_count) if limit_count
  validations.each do |validation, args|
    rule.send(validation, *args)
  end
end

#validationsObject



62
63
64
65
66
67
68
# File 'lib/ice_cube/hash_input.rb', line 62

def validations
  input_validations = Mash.new(@input[:validations] || {})
  ValidatedRule::VALIDATION_ORDER.each_with_object({}) do |key, output_validations|
    args = input_validations[key]
    output_validations[key] = Array(args) if args
  end
end

#week_startObject



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

def week_start
  @input[:week_start] || :sunday
end