Class: Attributor::HashDSLCompiler::Requirement
- Inherits:
-
Object
- Object
- Attributor::HashDSLCompiler::Requirement
- Defined in:
- lib/attributor/hash_dsl_compiler.rb
Overview
A class that encapsulates the definition of a requirement for Hash attributes It implements the validation against incoming values and it describes its format for documentation purposes
Instance Attribute Summary collapse
-
#attr_names ⇒ Object
readonly
Returns the value of attribute attr_names.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #describe(_shallow = false, _example: nil) ⇒ Object
-
#initialize(description: nil, **spec) ⇒ Requirement
constructor
A new instance of Requirement.
- #of(*args) ⇒ Object
- #validate(keys, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil) ⇒ Object
Constructor Details
#initialize(description: nil, **spec) ⇒ Requirement
Returns a new instance of Requirement.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 13 def initialize(description: nil, **spec) @description = description @type = spec.keys.first case type when :all of(*spec[type]) when :exclusive of(*spec[type]) else @number = spec[type] end end |
Instance Attribute Details
#attr_names ⇒ Object (readonly)
Returns the value of attribute attr_names.
10 11 12 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 10 def attr_names @attr_names end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
11 12 13 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 11 def description @description end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
9 10 11 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 9 def number @number end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 8 def type @type end |
Instance Method Details
#describe(_shallow = false, _example: nil) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 67 def describe(_shallow = false, _example: nil) hash = { type: type, attributes: attr_names } hash[:count] = number unless number.nil? hash[:description] = description unless description.nil? hash end |
#of(*args) ⇒ Object
26 27 28 29 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 26 def of(*args) @attr_names = args self end |
#validate(keys, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/attributor/hash_dsl_compiler.rb', line 31 def validate(keys, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil) result = [] case type when :all rest = attr_names - keys unless rest.empty? rest.each do |attr| result.push "Key #{attr} is required for #{Attributor.humanize_context(context)}." end end when :exactly included = attr_names & keys unless included.size == number result.push "Exactly #{number} of the following keys #{attr_names} are required for #{Attributor.humanize_context(context)}. Found #{included.size} instead: #{included.inspect}" end when :at_most rest = attr_names & keys if rest.size > number found = rest.empty? ? 'none' : rest.inspect result.push "At most #{number} keys out of #{attr_names} can be passed in for #{Attributor.humanize_context(context)}. Found #{found}" end when :at_least rest = attr_names & keys if rest.size < number found = rest.empty? ? 'none' : rest.inspect result.push "At least #{number} keys out of #{attr_names} are required to be passed in for #{Attributor.humanize_context(context)}. Found #{found}" end when :exclusive intersection = attr_names & keys if intersection.size > 1 result.push "keys #{intersection.inspect} are mutually exclusive for #{Attributor.humanize_context(context)}." end end result end |