Module: Greeve::Helpers::AttributeToHash

Included in:
BaseItem, Namespace, Row
Defined in:
lib/greeve/helpers/attribute_to_hash.rb

Overview

Adds the `#to_h` method.

Instance Method Summary collapse

Instance Method Details

#to_hHash

The (private) `#_attributes` method must be implemented for this method to work.

Returns:

  • (Hash)

    a hash of non-nil attributes



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/greeve/helpers/attribute_to_hash.rb', line 9

def to_h
  _attributes
  .keys
  .map { |name|
    value = __send__(name)
    value = value.to_a if value.is_a?(Rowset)
    value = value.to_h if value.is_a?(Namespace)

    [name, value]
  }
  .to_h
  .reject { |k, v| v.nil? }
end