Class: Pakyow::Data::Object

Inherits:
Object
  • Object
show all
Extended by:
Support::ClassState, Support::Makeable
Includes:
Support::Bindable
Defined in:
lib/pakyow/data/object.rb

Overview

Wraps values for a data object returned by a source.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Object

Returns a new instance of Object.



25
26
27
# File 'lib/pakyow/data/object.rb', line 25

def initialize(values)
  @values = Support::IndifferentHash.new(values).freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/pakyow/data/object.rb', line 47

def method_missing(name, *_args)
  if include?(name)
    @values[name]
  else
    super
  end
end

Class Attribute Details

.nameObject (readonly)

Returns the value of attribute name.



86
87
88
# File 'lib/pakyow/data/object.rb', line 86

def name
  @name
end

Instance Attribute Details

#originating_sourceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def originating_source
  @originating_source
end

#valuesObject (readonly)

Returns the value of attribute values.



15
16
17
# File 'lib/pakyow/data/object.rb', line 15

def values
  @values
end

Class Method Details

.make(name, state: nil, parent: nil, **kwargs, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
# File 'lib/pakyow/data/object.rb', line 89

def make(name, state: nil, parent: nil, **kwargs, &block)
  super(name, state: state, parent: parent, **kwargs, &block)
end

.serialize(*methods) ⇒ Object



93
94
95
# File 'lib/pakyow/data/object.rb', line 93

def serialize(*methods)
  @__serialized_methods.concat(methods.map(&:to_sym)).uniq!
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pakyow/data/object.rb', line 72

def ==(other)
  comparator = case other
  when self.class
    other
  when Result
    other.__getobj__
  else
    nil
  end

  comparator && comparator.class == self.class && comparator.values == @values
end

#[](key) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/pakyow/data/object.rb', line 38

def [](key)
  key = key.to_s.to_sym
  if respond_to?(key)
    public_send(key)
  else
    @values[key]
  end
end

#include?(key) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)


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

def include?(key)
  respond_to?(key)
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/pakyow/data/object.rb', line 55

def respond_to_missing?(name, *)
  @values.include?(name) || super
end

#sourceObject



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

def source
  @originating_source.__object_name.name
end

#to_hObject



59
60
61
62
63
64
65
66
# File 'lib/pakyow/data/object.rb', line 59

def to_h
  hash = @values.dup
  self.class.__serialized_methods.each do |method|
    hash[method] = public_send(method)
  end

  hash
end

#to_jsonObject



68
69
70
# File 'lib/pakyow/data/object.rb', line 68

def to_json(*)
  @values.to_json
end