class Hashematics::Configuration
This class understands how to take in a hash of options and construct an array of groups. See test fixtures for examples.
Attributes
groups[R]
type_dictionary[R]
Public Class Methods
new(config = {})
click to toggle source
# File lib/hashematics/configuration.rb, line 27 def initialize(config = {}) types = build_types(config_value(config, TYPES)) @type_dictionary = Dictionary.new(Type.null_type).add(types, &:name) @groups = build_groups(config_value(config, GROUPS)) freeze end
Private Instance Methods
build_groups(group_config = {}, parent_key_parts = [])
click to toggle source
# File lib/hashematics/configuration.rb, line 49 def build_groups(group_config = {}, parent_key_parts = []) (group_config || {}).map do |name, options| id_key_parts = make_id_key_parts(options) category = Category.new( id_key: id_key_parts, include_blank: include_blank?(options), parent_key: parent_key_parts ) Group.new( category: category, children: make_children(options, parent_key_parts + id_key_parts), name: name, type: make_type(options) ) end end
build_types(type_config = {})
click to toggle source
# File lib/hashematics/configuration.rb, line 40 def build_types(type_config = {}) (type_config || {}).map do |name, options| properties = config_value(options, PROPERTIES) object_class = config_value(options, OBJECT_CLASS) Type.new(name: name, properties: properties, object_class: object_class) end end
config_value(config, key)
click to toggle source
# File lib/hashematics/configuration.rb, line 86 def config_value(config, key) ObjectInterface.get(config, key) end
include_blank?(options)
click to toggle source
# File lib/hashematics/configuration.rb, line 68 def include_blank?(options) options.is_a?(Hash) ? config_value(options, INCLUDE_BLANK) : false end
make_children(options, parent_key_parts)
click to toggle source
# File lib/hashematics/configuration.rb, line 76 def make_children(options, parent_key_parts) options.is_a?(Hash) ? build_groups(config_value(options, GROUPS), parent_key_parts) : [] end
make_id_key_parts(options)
click to toggle source
# File lib/hashematics/configuration.rb, line 72 def make_id_key_parts(options) options.is_a?(Hash) ? Array(config_value(options, BY)) : Array(options) end
make_type(options)
click to toggle source
# File lib/hashematics/configuration.rb, line 80 def make_type(options) type_name = options.is_a?(Hash) ? config_value(options, TYPE) : nil type_dictionary.get(type_name) end