Class: EacRubyUtils::Compact

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/compact.rb

Constant Summary collapse

ATTRIBUTE_SEPARATOR =
'.'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/eac_ruby_utils/compact.rb', line 7

def attributes
  @attributes
end

#objectObject (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

Parameters:

  • attr_path (String, Symbol)

    A path separated by ATTRIBUTE_SEPARATOR.

Returns:



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_aArray

Returns:

  • (Array)


23
24
25
# File 'lib/eac_ruby_utils/compact.rb', line 23

def to_a
  attributes.map { |attr| attribute_value(attr) }
end

#to_hHash

Returns:



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