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.3.0'

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object


45
46
47
48
49
50
51
# File 'lib/structure.rb', line 45

def ==(other)
  if public_methods(false).include?(:<=>)
    super
  else
    attributes == other.attributes
  end
end

#attribute_namesObject


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

def attribute_names
  self.class.attribute_names
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

53
54
55
56
57
# File 'lib/structure.rb', line 53

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

  self == other
end

#freezeObject


59
60
61
62
# File 'lib/structure.rb', line 59

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

#inspectObject Also known as: to_s


33
34
35
36
37
38
39
40
41
# File 'lib/structure.rb', line 33

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

#marshal_dumpObject


64
65
66
# File 'lib/structure.rb', line 64

def marshal_dump
  attributes.values
end

#marshal_load(data) ⇒ Object


68
69
70
71
72
73
# File 'lib/structure.rb', line 68

def marshal_load(data)
  @mutex = ::Thread::Mutex.new
  attribute_names.zip(data).each do |key, val|
    instance_variable_set(:"@#{key}", val)
  end
end

#to_aObject


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

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

#to_hObject Also known as: attributes


27
28
29
# File 'lib/structure.rb', line 27

def to_h
  Hash[to_a]
end