Class: Krikri::MappingDSL::ParserMethods::RecordProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/krikri/mapping_dsl/parser_methods.rb

Overview

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 Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call_chain = [], klass = Krikri::Parser::ValueArray) ⇒ RecordProxy

Create a new RecordProxy object.

Parameters:

  • call_chain (Array<Hash>) (defaults to: [])

    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 [].

  • klass (Class) (defaults to: Krikri::Parser::ValueArray)

    a Class that acts as the target for delayed method calls. Must respond to #build(record) and #values. Defaults to Krikri::Parser::ValueArray


81
82
83
84
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 81

def initialize(call_chain = [], klass = Krikri::Parser::ValueArray)
  @call_chain = call_chain
  @value_class = klass
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ RecordProxy

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

Returns:

  • (RecordProxy)

    self, after adding the method to the call chain

Raises:

  • (NoMethodError)

    when the method is unavailable on #value_class.

  • (ArgumentError)

    when the arity of the call does not match the method on #value_class


121
122
123
124
125
126
127
128
129
130
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 121

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

Instance Attribute Details

#call_chainObject (readonly)

Returns the value of attribute call_chain


68
69
70
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 68

def call_chain
  @call_chain
end

#value_classObject (readonly)

Returns the value of attribute value_class


68
69
70
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 68

def value_class
  @value_class
end

Instance Method Details

#arityInteger

Returns the arity of self#call

Returns:

  • (Integer)

    the arity of self#call

See Also:


109
110
111
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 109

def arity
  1
end

#call(record) ⇒ Object

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.

Parameters:

  • record

    A parsed record object (e.g. Krikri::Parser) to be sent to value_class#build.

Returns:

  • the values resulting from the full run of the call chain


98
99
100
101
102
103
104
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 98

def call(record)
  result = value_class.build(record)
  call_chain.each do |message|
    result = result.send(message[:name], *message[:args], &message[:block])
  end
  result.values
end

#dupObject


86
87
88
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 86

def dup
  RecordProxy.new(call_chain.dup, value_class)
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)

132
133
134
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 132

def respond_to?(name)
  value_class.instance_methods.include?(name) || super
end