Class: ParamsReady::Helpers::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Rule

Returns a new instance of Rule.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/params_ready/helpers/rule.rb', line 15

def initialize(value)
  @mode, @values = case value
  when :none, :all then [value, nil]
  when Hash
    if value.length > 1 || value.length < 1
      raise ParamsReadyError, "Unexpected hash for rule: '#{value}'"
    end
    key, values = value.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: '#{value}'"
  end
  @values.freeze
  @hash = [@mode, @values].hash
  freeze
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



13
14
15
# File 'lib/params_ready/helpers/rule.rb', line 13

def hash
  @hash
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/params_ready/helpers/rule.rb', line 48

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

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/params_ready/helpers/rule.rb', line 36

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