class Krikri::MappingDSL::ParserMethods::RecordProxy

This class acts as a proxy for a parsed record's nodes, wrapped in the class passed as the second argument. All methods available on the wrapper class are accepted via method_missing, added to the call_chain, and return `self`, allowing chained method calls.

record.field('dct:title').field('foaf:name')

@see Krikri::Parser::ValueArray

Attributes

call_chain[R]
non_root[R]
value_class[R]

Public Class Methods

new(call_chain = [], klass = Krikri::Parser::ValueArray, non_root = false) click to toggle source

Create a new RecordProxy object.

@param call_chain [Array<Hash>] an array of hashes representing method

calls. Hashes need a :name (method name), :args (array of arguments),
and :block (a Proc to pass as a block). Defaults to [].

@param klass [Class] a Class that acts as the target for delayed method

calls. Must respond to #build(record) and #values. Defaults to
Krikri::Parser::ValueArray

@param non_root [Boolean] a flag indicating whether to build the root

node of the record before beginning the call chain. The default case
call `value_class.build`; `false` allows you to skip this step and
begin the call chain directly on object passed to `#call`.

@return [RecordProxy]

# File lib/krikri/mapping_dsl/parser_methods.rb, line 84
def initialize(call_chain = [], klass = Krikri::Parser::ValueArray, non_root = false)
  @call_chain = call_chain
  @value_class = klass
  @non_root = non_root
end

Public Instance Methods

arity() click to toggle source

@return [Integer] the arity of self#call @see call

# File lib/krikri/mapping_dsl/parser_methods.rb, line 113
def arity
  1
end
call(record) click to toggle source

Wraps a given record in value_class and applies the call chain; each method is sent to the result of the previous method. Finally, calls values on the result.

@param record A parsed record object (e.g. Krikri::Parser) to be sent to

value_class#build.

@return the values resulting from the full run of the call chain

# File lib/krikri/mapping_dsl/parser_methods.rb, line 102
def call(record)
  result = non_root ? record : value_class.build(record)
  call_chain.each do |message|
    result = result.send(message[:name], *message[:args], &message[:block])
  end
  result.values
end
dup() click to toggle source
# File lib/krikri/mapping_dsl/parser_methods.rb, line 90
def dup
  RecordProxy.new(call_chain.dup, value_class, non_root)
end
method_missing(name, *args, &block) click to toggle source

Adds method to the call chain if it is a valid method for value_class.

@return [RecordProxy] self, after adding the method to the call chain

@raise [NoMethodError] when the method is unavailable on value_class. @raise [ArgumentError] when the arity of the call does not match the

method on #value_class
Calls superclass method
# File lib/krikri/mapping_dsl/parser_methods.rb, line 125
def method_missing(name, *args, &block)
  super unless respond_to? name

  arity = value_class.instance_method(name).arity
  raise ArgumentError, "Method #{name} called with #{args.length} " \
  "arguments, expected #{arity}." unless arity < 0 || args.length == arity

  call_chain << { name: name, args: args, block: block }
  self
end
respond_to?(name) click to toggle source
Calls superclass method
# File lib/krikri/mapping_dsl/parser_methods.rb, line 136
def respond_to?(name)
  value_class.instance_methods.include?(name) || super
end