Sha256: 24971cfd47b4912a7fc45b8f6d02b01ac0fbf68ed46594aaaab36f01e6695160

Contents?: true

Size: 709 Bytes

Versions: 3

Compression:

Stored size: 709 Bytes

Contents

# frozen_string_literal: true

module EacRubyUtils
  class Compact
    ATTRIBUTE_SEPARATOR = '.'

    attr_reader :object, :attributes

    def initialize(object, attributes)
      @object = object
      @attributes = attributes
    end

    # @param attr_path [String, Symbol] A path separated by +ATTRIBUTE_SEPARATOR+.
    # @return [Object]
    def attribute_value(attr_path)
      attr_path.to_s.split(ATTRIBUTE_SEPARATOR).inject(object) do |a, e|
        a.send(e)
      end
    end

    # @return [Array]
    def to_a
      attributes.map { |attr| attribute_value(attr) }
    end

    # @return [Hash]
    def to_h
      attributes.to_h { |attr| [attr.to_sym, attribute_value(attr)] }
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
eac_ruby_utils-0.124.0 lib/eac_ruby_utils/compact.rb
eac_tools-0.97.2 sub/eac_ruby_utils/lib/eac_ruby_utils/compact.rb
eac_ruby_utils-0.123.0 lib/eac_ruby_utils/compact.rb