Module: Only

Defined in:
lib/only.rb,
lib/only/version.rb,
lib/only/base_constraint.rb

Defined Under Namespace

Classes: BaseConstraint

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Class Method Details

.allow(options = {}) ⇒ Object

Options:

:prefix
:suffix


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/only.rb', line 13

def allow(options = {})
  prefix = options.delete :prefix
  suffix = options.delete :suffix
  
  # Extract the param attribute name and collection to match against
  attribute_name, collection = options.first
  
  # This is the name used for the DSL
  name = attribute_name.to_s.pluralize
  name = :"#{prefix}_#{name}" if prefix
  name = :"#{name}_#{suffix}" if suffix
      
  define(name) { collection.include? params[attribute_name] }
end

.constraintsObject



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

def constraints
  @constraints ||= {}
end

.define(name, &match_proc) ⇒ Object



28
29
30
31
# File 'lib/only.rb', line 28

def define(name, &match_proc)
  constraints[name] = Class.new(BaseConstraint)
  constraints[name].define_singleton_method :condition_matches?, match_proc
end