Class: Krikri::MappingDSL::ParserMethods::RecordProxy
- Inherits:
-
Object
- Object
- Krikri::MappingDSL::ParserMethods::RecordProxy
- 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')
Instance Attribute Summary collapse
-
#call_chain ⇒ Object
readonly
Returns the value of attribute call_chain.
-
#value_class ⇒ Object
readonly
Returns the value of attribute value_class.
Instance Method Summary collapse
-
#arity ⇒ Integer
The arity of self#call.
-
#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.
- #dup ⇒ Object
-
#initialize(call_chain = [], klass = Krikri::Parser::ValueArray) ⇒ RecordProxy
constructor
Create a new RecordProxy object.
-
#method_missing(name, *args, &block) ⇒ RecordProxy
Adds method to the call chain if it is a valid method for value_class.
- #respond_to?(name) ⇒ Boolean
Constructor Details
#initialize(call_chain = [], klass = Krikri::Parser::ValueArray) ⇒ RecordProxy
Create a new RecordProxy object.
67 68 69 70 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 67 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.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 107 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_chain ⇒ Object (readonly)
Returns the value of attribute call_chain.
54 55 56 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 54 def call_chain @call_chain end |
#value_class ⇒ Object (readonly)
Returns the value of attribute value_class.
54 55 56 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 54 def value_class @value_class end |
Instance Method Details
#arity ⇒ Integer
Returns the arity of self#call.
95 96 97 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 95 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.
84 85 86 87 88 89 90 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 84 def call(record) result = value_class.build(record) call_chain.each do || result = result.send([:name], *[:args], &[:block]) end result.values end |
#dup ⇒ Object
72 73 74 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 72 def dup RecordProxy.new(call_chain.dup, value_class) end |
#respond_to?(name) ⇒ Boolean
118 119 120 |
# File 'lib/krikri/mapping_dsl/parser_methods.rb', line 118 def respond_to?(name) value_class.instance_methods.include?(name) || super end |