Module: Eco::API::Common::ClassHierarchy

Includes:
ClassHelpers
Included in:
Session::Batch::BasePolicy, Session::Config::Workflow
Defined in:
lib/eco/api/common/class_hierarchy.rb

Overview

Helpers for dynammic generation of classes based on a hierarchical model

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Instance Attribute Details

#modelObject

Returns the value of attribute model.



9
10
11
# File 'lib/eco/api/common/class_hierarchy.rb', line 9

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



17
18
19
# File 'lib/eco/api/common/class_hierarchy.rb', line 17

def model_attrs
  (model && 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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/eco/api/common/class_hierarchy.rb', line 24

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