Class: Attributor::HashDSLCompiler::RequiresDSL
- Inherits:
-
Object
- Object
- Attributor::HashDSLCompiler::RequiresDSL
- 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
-
#options ⇒ Object
Returns the value of attribute options.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
- #all(*attr_names, **opts) ⇒ Object
- #at_least(number) ⇒ Object
- #at_most(number) ⇒ Object
- #exactly(number) ⇒ Object
- #exclusive(*attr_names, **opts) ⇒ Object
-
#initialize(target, **opts) ⇒ RequiresDSL
constructor
A new instance of RequiresDSL.
Constructor Details
#initialize(target, **opts) ⇒ RequiresDSL
Returns a new instance of RequiresDSL.
87 88 89 90 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 87 def initialize(target, **opts) self.target = target self. = opts end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
86 87 88 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 86 def @options end |
#target ⇒ Object
Returns the value of attribute target.
85 86 87 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 85 def target @target end |
Instance Method Details
#all(*attr_names, **opts) ⇒ Object
[View source]
92 93 94 95 96 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 92 def all(*attr_names, **opts) req = Requirement.new(**.merge(opts).merge(all: attr_names)) target.add_requirement req req end |
#at_least(number) ⇒ Object
[View source]
104 105 106 107 108 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 104 def at_least(number) req = Requirement.new(**.merge(at_least: number)) target.add_requirement req req end |
#at_most(number) ⇒ Object
[View source]
98 99 100 101 102 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 98 def at_most(number) req = Requirement.new(**.merge(at_most: number)) target.add_requirement req req end |
#exactly(number) ⇒ Object
[View source]
110 111 112 113 114 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 110 def exactly(number) req = Requirement.new(**.merge(exactly: number)) target.add_requirement req req end |
#exclusive(*attr_names, **opts) ⇒ Object
[View source]
116 117 118 119 120 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 116 def exclusive(*attr_names, **opts) req = Requirement.new(**.merge(opts).merge(exclusive: attr_names)) target.add_requirement req req end |