Module: Kamaze::DockerImage::Concern::ReadableAttrs

Included in:
Kamaze::DockerImage
Defined in:
lib/kamaze/docker_image/concern/readable_attrs.rb

Overview

Provides readable_attrs method

Readable attributes have an instance variable and an accessor, boolean accessors are also supported.

As a result, class including this module obtains a Hash representation.

Instance Method Summary collapse

Instance Method Details

#readable_attrsArray<Symbol>

Get readable attributes

Returns:

  • (Array<Symbol>)


21
22
23
24
25
26
27
28
29
# File 'lib/kamaze/docker_image/concern/readable_attrs.rb', line 21

def readable_attrs
  instance_variables.clone.map do |attr|
    k = attr.to_s.gsub(/^@/, '').to_sym

    methods = ([k, "#{k}?".to_sym] & public_methods)

    methods.empty? ? nil : k
  end.compact.sort
end

#readable_attrs_valuesArray<Array> (protected)

Get readable attributes with values

Returns:

  • (Array<Array>)


40
41
42
43
44
45
46
47
# File 'lib/kamaze/docker_image/concern/readable_attrs.rb', line 40

def readable_attrs_values
  readable_attrs.map do |k|
    # booleanaccessor will override "real" accessor
    ([k, "#{k}?".to_sym] & public_methods)
      .map { |m| [k, self.public_send(m)] }
      .first
  end.compact
end

#to_hObject



31
32
33
# File 'lib/kamaze/docker_image/concern/readable_attrs.rb', line 31

def to_h
  readable_attrs_values.to_h
end