Class: Objectifier::MapValue
- Inherits:
-
Object
- Object
- Objectifier::MapValue
- Defined in:
- lib/objectifier/map_value.rb
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
-
#call(parameters) ⇒ Object
examine parameters and return a hash of massaged data or an error results.
-
#initialize(scope = "", &block) ⇒ MapValue
constructor
A new instance of MapValue.
- #item(name, **args) ⇒ Object
- #items(name, **args) ⇒ Object
- #map(name, &block) ⇒ Object
- #name ⇒ Object
- #pp(indent = "") ⇒ Object
- #required? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(scope = "", &block) ⇒ MapValue
Returns a new instance of MapValue.
7 8 9 10 11 |
# File 'lib/objectifier/map_value.rb', line 7 def initialize(scope = "", &block) @scope = scope.to_s @rules = Hash.new instance_eval &block end |
Instance Attribute Details
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
5 6 7 |
# File 'lib/objectifier/map_value.rb', line 5 def rules @rules end |
Instance Method Details
#call(parameters) ⇒ Object
examine parameters and return a hash of massaged data or an error results
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/objectifier/map_value.rb', line 51 def call(parameters) if @scope.length > 0 parameters = parameters.fetch( @scope, parameters.fetch( @scope.to_s, {})) end ok, errors = @rules.values.map do |rule| rule.call(parameters) end.partition do |result| result.success? end if errors.empty? values = ok.select(&:value?) if values.empty? SuccessResult.new(@scope) else ValueResult.new( @scope, Hash[values.map { |v| [ v.name, v.value ] }]) end else errors.reduce(ErrorResult.new) { |total, err| total.merge(err) }.scope(@scope) end end |
#item(name, **args) ⇒ Object
23 24 25 26 |
# File 'lib/objectifier/map_value.rb', line 23 def item(name, **args) @rules[name.to_s] = ScalarValue.new(name, args) true end |
#items(name, **args) ⇒ Object
28 29 30 31 |
# File 'lib/objectifier/map_value.rb', line 28 def items(name, **args) @rules[name.to_s] = ArrayValue.new(name, args) true end |
#map(name, &block) ⇒ Object
33 34 35 36 |
# File 'lib/objectifier/map_value.rb', line 33 def map(name, &block) @rules[name.to_s] = MapValue.new(name, &block) true end |
#name ⇒ Object
13 14 15 |
# File 'lib/objectifier/map_value.rb', line 13 def name @scope end |
#pp(indent = "") ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/objectifier/map_value.rb', line 38 def pp(indent = "") next_indent = "#{indent} " str = "#{indent} #{@scope} {\n" str << @rules.values.map { |r| r.pp(next_indent) }.join("\n") str << "\n#{indent}}\n" str end |
#required? ⇒ Boolean
17 18 19 20 21 |
# File 'lib/objectifier/map_value.rb', line 17 def required? @rules.values.reduce(false) do |req, rule| req || rule.required? end end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/objectifier/map_value.rb', line 46 def to_s pp("") end |