Class: Attributor::HashDSLCompiler::RequiresDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/attributor/hash_dsl_compiler.rb

Overview

A class that encapsulates the available DSL under the ‘requires` keyword. In particular it allows to define requirements like: requires.all :attr1, :attr2, :attr3 requires.exclusive :attr1, :attr2, :attr3 requires.at_most(2).of :attr1, :attr2, :attr3 requires.at_least(2).of :attr1, :attr2, :attr3 requires.exactly(2).of :attr1, :attr2, :attr3 Note: all and exclusive can also use .of , it is equivalent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, **opts) ⇒ RequiresDSL

Returns a new instance of RequiresDSL.



86
87
88
89
# File 'lib/attributor/hash_dsl_compiler.rb', line 86

def initialize(target, **opts)
  self.target = target
  self.options = opts
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



85
86
87
# File 'lib/attributor/hash_dsl_compiler.rb', line 85

def options
  @options
end

#targetObject

Returns the value of attribute target.



84
85
86
# File 'lib/attributor/hash_dsl_compiler.rb', line 84

def target
  @target
end

Instance Method Details

#all(*attr_names, **opts) ⇒ Object



91
92
93
94
95
# File 'lib/attributor/hash_dsl_compiler.rb', line 91

def all(*attr_names, **opts)
  req = Requirement.new(options.merge(opts).merge(all: attr_names))
  target.add_requirement req
  req
end

#at_least(number) ⇒ Object



103
104
105
106
107
# File 'lib/attributor/hash_dsl_compiler.rb', line 103

def at_least(number)
  req = Requirement.new(options.merge(at_least: number))
  target.add_requirement req
  req
end

#at_most(number) ⇒ Object



97
98
99
100
101
# File 'lib/attributor/hash_dsl_compiler.rb', line 97

def at_most(number)
  req = Requirement.new(options.merge(at_most: number))
  target.add_requirement req
  req
end

#exactly(number) ⇒ Object



109
110
111
112
113
# File 'lib/attributor/hash_dsl_compiler.rb', line 109

def exactly(number)
  req = Requirement.new(options.merge(exactly: number))
  target.add_requirement req
  req
end

#exclusive(*attr_names, **opts) ⇒ Object



115
116
117
118
119
# File 'lib/attributor/hash_dsl_compiler.rb', line 115

def exclusive(*attr_names, **opts)
  req = Requirement.new(options.merge(opts).merge(exclusive: attr_names))
  target.add_requirement req
  req
end