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

Instance Method Summary collapse

Instance Method Details

#collection?Boolean

returns true if the json interpreted was a collection (had embedded items) even if there was only one; otherwise false

Returns:

  • (Boolean)


46
47
48
# File 'lib/hal_interpretation.rb', line 46

def collection?
  repr.has_related?("item")
end

#itemObject

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

#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.


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.



55
56
57
58
59
60
61
62
# File 'lib/hal_interpretation.rb', line 55

def new_item(&blk)
  if item_to_update
    yield item_to_update
    item_to_update
  else
    item_class.new.tap(&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

#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.



40
41
42
# File 'lib/hal_interpretation.rb', line 40

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