Module: Ecoportal::API::Common::Content::CollectionModel::Modifiers::ItemsKlass::ClassMethods

Defined in:
lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb

Constant Summary collapse

NOT_USED =
Content::ClassHelpers::NOT_USED

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#klass(value = NOT_USED) {|doc| ... } ⇒ Class, ...

Note:
  • use block to define klass callback
Note:

When klass is resolved, if the items are of type DoubleModel, it sets on the collection class the items_key

Note:

when klass is directly resolved (not via doc) only once it will set @klass as resolved and will use this class from now on. This is an optimization to cut class lookups

Resolves to the nuclear Class of the elements

Parameters:

  • value (Hash) (defaults to: NOT_USED)

    base doc (raw object) to create the object with

Yields:

  • (doc)

    identifies the target class of the raw object

Yield Parameters:

  • doc (Hash)

Yield Returns:

  • (Klass)

    the target class

Returns:

  • (Class, Proc, Hash)

    the target class

    • Hash tracks a symbol pending to be resovle from its referrer
    • Class an already resolve class
    • Proc a forker that pivots between multiple classes


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 57

def klass(value = NOT_USED, &block)
  @klass = block if block_given?

  if @klass.is_a?(Proc) && used_param?(value)
    @klass.call(value)
  elsif @klass && !@klass.is_a?(Proc) && !@klass.is_a?(Class)
    @klass = resolve_class(@klass, exception: false)
    @klass
  else
    @klass
  end.tap do |result|
    next unless result.is_a?(Class)
    next unless result < Ecoportal::API::Common::Content::DoubleModel

    self.items_key = result.key
  end
end

Instance Method Details

#klass?Boolean

Returns are there the factory logics to build item objects defined?.

Returns:

  • (Boolean)

    are there the factory logics to build item objects defined?



37
38
39
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 37

def klass?
  @klass || @new_item
end

#new_item(doc = NOT_USED, parent: nil, key: nil, read_only: false) {|doc, parent, key| ... } ⇒ Klass

Note:
  • use block to define new_item callback, which will prevail over klass
  • if new_item callback was not defined, it is required to defnie klass
Note:

if block is given, it ignores doc

Generates a new object of the target class

Parameters:

  • doc (Hash) (defaults to: NOT_USED)

    doc to parse

  • parent (CollectionModel) (defaults to: nil)

    the parent of the new item

  • key (Symbol, String) (defaults to: nil)

    the key value to access the item within collection Please observe that items in a CollectionModel are identified via their key attr. Meaning that there is actually no need to define this argument.

  • read_only (Boolean) (defaults to: false)

    whether or not this new element should be created as read-only.

Yields:

  • (doc, parent, key)

    creates an object instance of the target klass

Yield Parameters:

  • doc (Hash)

Yield Returns:

  • (Klass)

    instance object of the target klass

Returns:

  • (Klass)

    instance object of the target klass



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 91

def new_item(doc = NOT_USED, parent: nil, key: nil, read_only: false, &block)
  if block_given?
    @new_item = block
    return
  end

  msg = 'To define the new_item callback (factory), you need to use a block'
  raise msg unless used_param?(doc)

  msg = 'You should define either a klass or a new_item callback first'
  raise msg unless klass?

  return @new_item.call(doc, parent, key) if @new_item.is_a?(Proc)

  target_class = klass(doc)
  raise "Could not find a class for: #{doc}" unless target_class

  return doc if doc.is_a?(target_class)

  target_class.new(doc, parent: parent, key: key, read_only: read_only)
end

#new_item_class_based?Boolean

Optimization

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 28

def new_item_class_based?
  return false if @new_item.is_a?(Proc)
  return false if klass.is_a?(Proc)
  return true  if klass.is_a?(Class)

  false
end