Class: HalInterpretation::Extractor
- Inherits:
-
Object
- Object
- HalInterpretation::Extractor
- Defined in:
- lib/hal_interpretation/extractor.rb
Instance Attribute Summary collapse
-
#attr ⇒ Object
readonly
Returns the value of attribute attr.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
Instance Method Summary collapse
-
#extract(opts) ⇒ Object
opts - named args :from - The HalRepresentation from which to extract attribute.
-
#initialize(opts) ⇒ Extractor
constructor
opts - named args :attr - name of attribute this object will extact.
Constructor Details
#initialize(opts) ⇒ Extractor
opts - named args
:attr - name of attribute this object will extact.
:location - JSON path from which to get the value.
:extraction_proc - Callable that can extract the value when
passed a HalClient::Representation of the item.
:coercion - proc to pass the extracted value through before
storing it.
15 16 17 18 19 20 21 22 23 |
# File 'lib/hal_interpretation/extractor.rb', line 15 def initialize(opts) @attr = opts.fetch(:attr) { fail ArgumentError, "attr is required" } @location = opts.fetch(:location) { "/#{attr}" } @fetcher = opts.fetch(:extraction_proc) { Hana::Pointer.new(location).method(:eval) } @value_coercion = opts.fetch(:coercion) { IDENTITY } fail(ArgumentError, ":coercion must respond to #call") unless value_coercion.respond_to? :call end |
Instance Attribute Details
#attr ⇒ Object (readonly)
Returns the value of attribute attr.
41 42 43 |
# File 'lib/hal_interpretation/extractor.rb', line 41 def attr @attr end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
41 42 43 |
# File 'lib/hal_interpretation/extractor.rb', line 41 def location @location end |
Instance Method Details
#extract(opts) ⇒ Object
opts - named args
:from - The HalRepresentation from which to extract attribute.
:to - The model that we are extracting
Returns any problems encountered.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hal_interpretation/extractor.rb', line 30 def extract(opts) from = opts.fetch(:from) { fail ArgumentError, "from is required" } to = opts.fetch(:to) { fail ArgumentError, "to is required" } to.send "#{attr}=", value_coercion.call(fetcher.call(from)) [] rescue => err [err.] end |