Class: Dimensional::Enum::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/dimensional/enum/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum_attributes_hash = {}) ⇒ Attributes



7
8
9
# File 'lib/dimensional/enum/attributes.rb', line 7

def initialize( enum_attributes_hash={} )
  @attrs = enum_attributes_hash
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



5
6
7
# File 'lib/dimensional/enum/attributes.rb', line 5

def attrs
  @attrs
end

Instance Method Details

#id(enum_key, enum_attr_key) ⇒ Object



31
32
33
34
35
# File 'lib/dimensional/enum/attributes.rb', line 31

def id(enum_key, enum_attr_key)
  @attrs[enum_key.to_sym][enum_attr_key.to_sym][:id]
rescue
  nil
end

#include?(key) ⇒ Boolean



11
12
13
# File 'lib/dimensional/enum/attributes.rb', line 11

def include?(key)
  @attrs.include? key
end

#name(enum_key, enum_attr_key) ⇒ Object



37
38
39
40
41
# File 'lib/dimensional/enum/attributes.rb', line 37

def name(enum_key, enum_attr_key)
  @attrs[enum_key.to_sym][enum_attr_key.to_sym][:name]
rescue
  nil
end

#to_attribute_h(enum_key) ⇒ Object Also known as: to_origin_h



15
16
17
# File 'lib/dimensional/enum/attributes.rb', line 15

def to_attribute_h(enum_key)
  @attrs[enum_key]
end

#to_display_h(enum_key) ⇒ Object



25
26
27
28
29
# File 'lib/dimensional/enum/attributes.rb', line 25

def to_display_h(enum_key)
  hash = @attrs[enum_key].each_with_object({}) do |(key, value), h| 
    h[key] = value[:name]
  end
end

#to_json(enum_key) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/dimensional/enum/attributes.rb', line 43

def to_json(enum_key)
  arr = []
  @attrs[enum_key].each do |key, value|
    arr << { value: key, text: value[:name] }
  end
  JSON.unparse arr
end

#to_value_h(enum_key) ⇒ Object



19
20
21
22
23
# File 'lib/dimensional/enum/attributes.rb', line 19

def to_value_h(enum_key)
  hash = @attrs[enum_key].each_with_object({}) do |(key, value), h| 
    h[key] = value[:id]
  end
end