module Eco::API::Common::ClassHierarchy
Helpers for dynammic generation of classes based on a hierarchical model @attr_reader model [Hash, nil] the `model` of the current `class`
Attributes
model[R]
Public Instance Methods
model=(value)
click to toggle source
@param value [Hash, Enumerable, String, Symbol, nil] unparsed model to be assigned to the `class`
# File lib/eco/api/common/class_hierarchy.rb, line 12 def model=(value) @model = parse_model(value) end
model_attrs()
click to toggle source
@return [Array<String>] the `keys` of the current class' `model`
# File lib/eco/api/common/class_hierarchy.rb, line 17 def model_attrs (model && model.keys) || [] end
parse_model(model)
click to toggle source
Thanks to this step the format on the declaration of the model is flexible @param model [Hash, Enumerable, String, Symbol, nil] @return [Hash, nil] where keys are `Symbol` s
# 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