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.0"

Instance Method Summary collapse

Instance Method Details

#itemObject

Returns the single item interpreted.

Raises InvalidRepresentationError if more than one item was found.



28
29
30
31
32
33
# File 'lib/hal_interpretation.rb', line 28

def item
  (fail InvalidRepresentationError, "More than one representation found") if
    items.size > 1

  items.first
end

#itemsObject

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.


19
20
21
22
23
# File 'lib/hal_interpretation.rb', line 19

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.



47
48
49
50
51
52
53
54
# File 'lib/hal_interpretation.rb', line 47

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
# File 'lib/hal_interpretation.rb', line 10

def only_update(an_item)
  @item_to_update = an_item
end

#problemsObject

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.



38
39
40
# File 'lib/hal_interpretation.rb', line 38

def problems
  @problems ||= interpreters.flat_map(&:problems)
end