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.
-
#non_root ⇒ Object
readonly
Returns the value of attribute non_root.
-
#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, non_root = false) ⇒ 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, non_root = false) ⇒ RecordProxy
Create a new RecordProxy object.
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.
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_chain ⇒ Object (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_root ⇒ Object (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_class ⇒ Object (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
#arity ⇒ Integer
Returns the arity of self#call.
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.
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 || result = result.send([:name], *[:args], &[:block]) end result.values end |
#dup ⇒ Object
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
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 |