class Objectifier::MapValue

Attributes

rules[R]

Public Class Methods

new(scope = "", &block) click to toggle source
# File lib/objectifier/map_value.rb, line 7
def initialize(scope = "", &block)
  @scope = scope.to_s
  @rules = Hash.new
  instance_eval &block
end

Public Instance Methods

call(parameters) click to toggle source

examine parameters and return a hash of massaged data or an error results

# File lib/objectifier/map_value.rb, line 39
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?)
    ValueResult.new(
      @scope,
      Hash[values.map { |v| [ v.name, v.value ] }])
  else
    errors.reduce(ErrorResult.new) { |total, err| total.merge(err) }.scope(@scope)
  end
end
item(name, args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/objectifier/map_value.rb, line 33
def map(name, &block)
  @rules[name.to_s] = MapValue.new(name, &block)
  true
end
name() click to toggle source
# File lib/objectifier/map_value.rb, line 13
def name
  @scope
end
required?() click to toggle source
# File lib/objectifier/map_value.rb, line 17
def required?
  @rules.values.reduce(false) do |req, rule|
    req || rule.required?
  end
end