Module: HalInterpretation
- Extended by:
- Forwardable
- Defined in:
- lib/hal_interpretation.rb,
lib/hal_interpretation/dsl.rb,
lib/hal_interpretation/errors.rb,
lib/hal_interpretation/version.rb,
lib/hal_interpretation/extractor.rb,
lib/hal_interpretation/item_interpreter.rb
Overview
Declarative interpretation of HAL documents into ActiveModel style objects.
Defined Under Namespace
Modules: ClassMethods, Dsl Classes: Extractor, InvalidRepresentationError, ItemInterpreter
Constant Summary collapse
- Error =
Class.new(StandardError)
- VERSION =
"1.4.1"
Instance Method Summary collapse
-
#item ⇒ Object
Returns the single item interpreted.
-
#items ⇒ Object
Returns array of models created from the HAL representation we are interpreting.
-
#new_item(&blk) ⇒ Object
Internal: builds and returns items that should be use interpreted into.
-
#only_update(an_item) ⇒ Object
Declares that this interpreter should only update ‘an_item`.
-
#problems ⇒ Object
Returns array of problems messages, or empty array if there are none.
Instance Method Details
#item ⇒ Object
Returns the single item interpreted.
Raises InvalidRepresentationError if more than one item was found.
30 31 32 33 34 35 |
# File 'lib/hal_interpretation.rb', line 30 def item (fail InvalidRepresentationError, "More than one representation found") if items.size > 1 items.first end |
#items ⇒ Object
Returns array of models created from the HAL representation we are interpreting.
Raises InvalidRepresentationError if any of the models are invalid
or the representation is not a HAL document.
21 22 23 24 25 |
# File 'lib/hal_interpretation.rb', line 21 def items (fail InvalidRepresentationError.new(problems)) if problems.any? @items ||= interpreters.flat_map(&:items) end |
#new_item(&blk) ⇒ Object
Internal: builds and returns items that should be use interpreted into.
49 50 51 52 53 54 55 56 |
# File 'lib/hal_interpretation.rb', line 49 def new_item(&blk) if item_to_update yield item_to_update item_to_update else item_class.new(&blk) end end |
#only_update(an_item) ⇒ Object
Declares that this interpreter should only update ‘an_item`.
10 11 12 13 14 |
# File 'lib/hal_interpretation.rb', line 10 def only_update(an_item) @item_to_update = an_item return self end |
#problems ⇒ Object
Returns array of problems messages, or empty array if there are none. This will do a complete interpretation of the representation if it has not already been done.
40 41 42 |
# File 'lib/hal_interpretation.rb', line 40 def problems @problems ||= interpreters.flat_map(&:problems) end |