Class: ParamsReady::Helpers::Rule
- Inherits:
-
Object
- Object
- ParamsReady::Helpers::Rule
- Defined in:
- lib/params_ready/helpers/rule.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #include?(name) ⇒ Boolean
-
#initialize(mode, values) ⇒ Rule
constructor
A new instance of Rule.
- #merge(other) ⇒ Object
Constructor Details
#initialize(mode, values) ⇒ Rule
Returns a new instance of Rule.
34 35 36 37 38 39 |
# File 'lib/params_ready/helpers/rule.rb', line 34 def initialize(mode, values) @mode = mode @values = values.freeze @hash = [@mode, @values].hash freeze end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
13 14 15 |
# File 'lib/params_ready/helpers/rule.rb', line 13 def hash @hash end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
13 14 15 |
# File 'lib/params_ready/helpers/rule.rb', line 13 def mode @mode end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
13 14 15 |
# File 'lib/params_ready/helpers/rule.rb', line 13 def values @values end |
Class Method Details
.instance(input) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/params_ready/helpers/rule.rb', line 15 def self.instance(input) mode, values = case input when :none, :all then [input, nil] when Hash if input.length > 1 || input.length < 1 raise ParamsReadyError, "Unexpected hash for rule: '#{input}'" end key, values = input.first case key when :except, :only then [key, values.to_set.freeze] else raise ParamsReadyError, "Unexpected mode for rule: '#{key}'" end else raise ParamsReadyError, "Unexpected input for rule: '#{input}'" end new(mode, values) end |
Instance Method Details
#==(other) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/params_ready/helpers/rule.rb', line 67 def ==(other) return false unless other.is_a? Rule return true if object_id == other.object_id return false unless @mode == other.instance_variable_get(:@mode) @values == other.instance_variable_get(:@values) end |
#include?(name) ⇒ Boolean
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/params_ready/helpers/rule.rb', line 55 def include?(name) case @mode when :none then false when :all then true when :only then @values.member? name when :except !@values.member? name else raise ParamsReadyError, "Unexpected mode for rule: '#{@mode}'" end end |
#merge(other) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/params_ready/helpers/rule.rb', line 41 def merge(other) return self if other.nil? raise ParamsReadyError, "Can't merge with #{other.class.name}" unless other.is_a? Rule raise ParamsReadyError, "Can't merge incompatible rules: #{mode}/#{other.mode}" if other.mode != mode case mode when :all, :none self when :only, :except values = self.values + other.values Rule.new(mode, values) end end |