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, non_root = false) ⇒ 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

  • non_root (Boolean) (defaults to: false)

    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`.



84
85
86
87
88
# 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

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



125
126
127
128
129
130
131
132
133
134
# 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

Instance Attribute Details

#call_chainObject (readonly)

Returns the value of attribute call_chain.



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

def call_chain
  @call_chain
end

#non_rootObject (readonly)

Returns the value of attribute non_root.



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

def non_root
  @non_root
end

#value_classObject (readonly)

Returns the value of attribute value_class.



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

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:



113
114
115
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 113

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



102
103
104
105
106
107
108
# 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

#dupObject



90
91
92
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 90

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 136

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