Module: Eco::Language::Models::Hierarchy

Included in:
API::Session::Batch::BasePolicy, Workflow
Defined in:
lib/eco/language/models/hierarchy.rb

Overview

Note:

you might want to use it in combination with Eco::Language::Klass::HelpersBuilt

Helpers for dynammic generation of classes based on a hierarchical model

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modelObject

Returns the value of attribute model.



7
8
9
# File 'lib/eco/language/models/hierarchy.rb', line 7

def model
  @model
end

Instance Method Details

#model_attrsArray<String>

Returns the keys of the current class' model.

Returns:

  • (Array<String>)

    the keys of the current class' model



15
16
17
# File 'lib/eco/language/models/hierarchy.rb', line 15

def model_attrs
  model&.keys || []
end

#parse_model(model) ⇒ Hash?

Thanks to this step the format on the declaration of the model is flexible

Parameters:

  • model (Hash, Enumerable, String, Symbol, nil)

Returns:

  • (Hash, nil)

    where keys are Symbol s



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eco/language/models/hierarchy.rb', line 22

def parse_model(model)
  case model
  when String
    parse_model(model.to_sym)
  when Symbol
    {model => nil}
  when Hash
    model.transform_keys(&:to_sym)
  when Enumerable
    model.each_with_object({}) do |sub, hash|
      hash.merge!(parse_model(sub))
    end
  when NilClass
    nil
  else
    raise "Incorrect model declaration, allowed String, Symbol, Hash and Enumerable. Given: #{model.class}"
  end
end