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.
90 91 92 93 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 90 def initialize(target, **opts) self.target = target self. = opts end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
89 90 91 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 89 def @options end |
#target ⇒ Object
Returns the value of attribute target.
88 89 90 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 88 def target @target end |
Instance Method Details
#all(*attr_names, **opts) ⇒ Object
94 95 96 97 98 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 94 def all(*attr_names, **opts) req = Requirement.new( .merge(opts).merge(all: attr_names) ) target.add_requirement req req end |
#at_least(number) ⇒ Object
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
99 100 101 102 103 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 99 def at_most(number) req = Requirement.new( .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( .merge(exactly: number) ) target.add_requirement req req end |
#exclusive(*attr_names, **opts) ⇒ Object
114 115 116 117 118 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 114 def exclusive(*attr_names, **opts) req = Requirement.new( .merge(opts).merge(exclusive: attr_names) ) target.add_requirement req req end |