Class: EacRubyUtils::Compact
Constant Summary collapse
- ATTRIBUTE_SEPARATOR =
'.'
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
- #attribute_value(attr_path) ⇒ Object
-
#initialize(object, attributes) ⇒ Compact
constructor
A new instance of Compact.
- #to_a ⇒ Array
- #to_h ⇒ Hash
Constructor Details
#initialize(object, attributes) ⇒ Compact
Returns a new instance of Compact.
9 10 11 12 |
# File 'lib/eac_ruby_utils/compact.rb', line 9 def initialize(object, attributes) @object = object @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/eac_ruby_utils/compact.rb', line 7 def attributes @attributes end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
7 8 9 |
# File 'lib/eac_ruby_utils/compact.rb', line 7 def object @object end |
Instance Method Details
#attribute_value(attr_path) ⇒ Object
16 17 18 19 20 |
# File 'lib/eac_ruby_utils/compact.rb', line 16 def attribute_value(attr_path) attr_path.to_s.split(ATTRIBUTE_SEPARATOR).inject(object) do |a, e| a.send(e) end end |
#to_a ⇒ Array
23 24 25 |
# File 'lib/eac_ruby_utils/compact.rb', line 23 def to_a attributes.map { |attr| attribute_value(attr) } end |
#to_h ⇒ Hash
28 29 30 |
# File 'lib/eac_ruby_utils/compact.rb', line 28 def to_h attributes.to_h { |attr| [attr.to_sym, attribute_value(attr)] } end |