Module: Structure

Defined in:
lib/structure.rb,
lib/structure/utils.rb,
lib/structure/double.rb,
lib/structure/version.rb,
lib/structure/class_methods.rb

Defined Under Namespace

Modules: ClassMethods, Utils

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
# File 'lib/structure.rb', line 7

def self.included(base)
  base.extend ClassMethods
  base.__overwrite_initialize__
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



22
23
24
25
26
# File 'lib/structure.rb', line 22

def ==(other)
  return false unless other.respond_to?(:attributes)

  attributes == other.attributes
end

#attribute_namesObject



18
19
20
# File 'lib/structure.rb', line 18

def attribute_names
  self.class.attribute_names
end

#attributesObject Also known as: to_h



12
13
14
15
16
# File 'lib/structure.rb', line 12

def attributes
  attribute_names.each_with_object({}) do |key, hash|
    hash[key] = Utils.serialize(send(key))
  end
end

#inspectObject Also known as: to_s



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/structure.rb', line 28

def inspect
  name = self.class.name || self.class.to_s.gsub(/[^\w:]/, '')
  values =
    attribute_names
    .map do |key|
      value = send(key)
      if value.is_a?(::Array)
        description = value.take(3).map(&:inspect).join(', ')
        description += '...' if value.size > 3
        "#{key}=[#{description}]"
      else
        "#{key}=#{value.inspect}"
      end
    end
    .join(', ')

  "#<#{name} #{values}>"
end