Class: Masterplan::Rule
- Inherits:
-
Object
- Object
- Masterplan::Rule
- Defined in:
- lib/masterplan/rule.rb
Constant Summary collapse
- OPTIONS =
["allow_nil", "compare_each", "included_in", "matches", "optional"]
Instance Attribute Summary collapse
-
#example_value ⇒ Object
Returns the value of attribute example_value.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(example, options = {}) ⇒ Rule
constructor
A new instance of Rule.
- #masterplan_compare(value, path) ⇒ Object
Constructor Details
#initialize(example, options = {}) ⇒ Rule
Returns a new instance of Rule.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/masterplan/rule.rb', line 8 def initialize(example, = {}) .stringify_keys! ['allow_nil'] ||= false ['compare_each'] ||= false ["included_in"] ||= false ["matches"] ||= false ["optional"] ||= false raise ArgumentError, "options can be #{OPTIONS.join(',')}, not #{.keys.inspect}" unless .keys.sort == OPTIONS.sort self.example_value = example self. = self.masterplan_compare(example, "initialization of rule") end |
Instance Attribute Details
#example_value ⇒ Object
Returns the value of attribute example_value.
6 7 8 |
# File 'lib/masterplan/rule.rb', line 6 def example_value @example_value end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/masterplan/rule.rb', line 6 def @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 |