Module: Structure

Defined in:
lib/structure.rb,
lib/structure/version.rb

Overview

A tiny library for lazy parsing data with memoized attributes

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'2.0.0'

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



39
40
41
# File 'lib/structure.rb', line 39

def ==(other)
  attributes == other.attributes
end

#attribute_namesObject



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

def attribute_names
  self.class.attribute_names
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/structure.rb', line 43

def eql?(other)
  return false if other.class != self.class

  self == other
end

#freezeObject



49
50
51
52
# File 'lib/structure.rb', line 49

def freeze
  attribute_names.each { |key| send(key) }
  super
end

#inspectObject Also known as: to_s



27
28
29
30
31
32
33
34
35
# File 'lib/structure.rb', line 27

def inspect
  detail = if public_methods(false).include?(:to_s)
             to_s
           else
             to_a.map { |key, val| "#{key}=#{val.inspect}" }.join(', ')
           end

  "#<#{self.class.name || '?'} #{detail}>"
end

#to_aObject



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

def to_a
  attribute_names.map { |key| [key, send(key)] }
end

#to_hObject Also known as: attributes



21
22
23
# File 'lib/structure.rb', line 21

def to_h
  Hash[to_a]
end