Class: Masterplan::Rule

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

Constant Summary collapse

OPTIONS =
["allow_nil", "compare_each", "included_in", "matches", "optional"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example, options = {}) ⇒ Rule

Returns a new instance of Rule.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/masterplan/rule.rb', line 8

def initialize(example, options = {})
  options.stringify_keys!
  options['allow_nil'] ||= false
  options['compare_each'] ||= false
  options["included_in"] ||= false
  options["matches"] ||= false
  options["optional"] ||= false
  raise ArgumentError, "options can be #{OPTIONS.join(',')}, not #{options.keys.inspect}" unless options.keys.sort == OPTIONS.sort
  self.example_value = example
  self.options = options
  self.masterplan_compare(example, "initialization of rule")
end

Instance Attribute Details

#example_valueObject

Returns the value of attribute example_value.



6
7
8
# File 'lib/masterplan/rule.rb', line 6

def example_value
  @example_value
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/masterplan/rule.rb', line 6

def options
  @options
end

Class Method Details

.check_class_equality!(template, value, path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/masterplan/rule.rb', line 33

def self.check_class_equality!(template, value, path)

  value_klass = case value
    when Document
      Hash
    when Rule
      value.example_value.class
    else
      value.class
  end
  template_klass = case template
    when Document
      Hash
    when Rule
      template.example_value.class
    else
      template.class
  end
  if template_klass != value_klass
    raise FailedError, "value at #{path} (#{value_klass}) is not a #{template_klass} !"
  else
    true
  end
end

Instance Method Details

#masterplan_compare(value, path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/masterplan/rule.rb', line 21

def masterplan_compare(value, path)
#      puts "#{path} #{@masterplan_rule_options.inspect}"
#      puts self.inspect
#      puts value.inspect
#      puts @masterplan_rule_options["included_in"].inspect
#      puts !@masterplan_rule_options["included_in"].include?(value) if @masterplan_rule_options["included_in"]
  return true if masterplan_check_allowed_nil!(value, path)
  return true if masterplan_check_included_in!(value, path)
  return true if masterplan_check_matches!(value, path)
  return true if masterplan_check_class_equality!(value, path)
end