Class: Graphlyte::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/graphlyte/data.rb

Overview

Very simplistic data-class. Inheritance is not modelled.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Data

Permissive constructor: ignores unknown attributes



23
24
25
26
27
# File 'lib/graphlyte/data.rb', line 23

def initialize(**kwargs)
  self.class.attributes.each do |arg|
    send(:"#{arg}=", kwargs[arg]) if kwargs.key?(arg)
  end
end

Class Method Details

.attr_accessor(*names) ⇒ Object



8
9
10
11
# File 'lib/graphlyte/data.rb', line 8

def self.attr_accessor(*names)
  super
  attributes.merge(names)
end

.attr_reader(*names) ⇒ Object



13
14
15
16
# File 'lib/graphlyte/data.rb', line 13

def self.attr_reader(*names)
  super
  attributes.merge(names)
end

.attributesObject



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

def self.attributes
  @attributes ||= [].to_set
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/graphlyte/data.rb', line 33

def ==(other)
  eql?(other)
end

#dupObject



41
42
43
# File 'lib/graphlyte/data.rb', line 41

def dup
  self.class.new(**self.class.attributes.to_h { [_1, dup_attribute(_1)] })
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/graphlyte/data.rb', line 29

def eql?(other)
  other.is_a?(self.class) && state == other.send(:state)
end

#hashObject



37
38
39
# File 'lib/graphlyte/data.rb', line 37

def hash
  state.hash
end

#inspectObject



45
46
47
# File 'lib/graphlyte/data.rb', line 45

def inspect
  "#<#{self.class} #{self.class.attributes.map { "@#{_1}=#{send(_1).inspect}" }.join(' ')}>"
end