class ParamsReady::Helpers::Rule

Attributes

hash[R]

Public Class Methods

new(value) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# 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) click to toggle source
# 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