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.



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

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#targetObject

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



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

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



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

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

#at_most(number) ⇒ Object



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

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

#exactly(number) ⇒ Object



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

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

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



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

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