Class: Polyseerio::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/resource/base.rb

Overview

Base class for any resource.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
# File 'lib/resource/base.rb', line 10

def initialize(attributes = {})
  @eid = if attributes.include? :eid
           attributes[:eid]
         else
           Polyseerio::SDK::Helper.resolve_eid(copts)
         end
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

rubocop:disable all



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/resource/base.rb', line 61

def method_missing(name, *args) # rubocop:disable all
  # Setter.
  if name =~ /^(\w+)=$/
    name = :"#{$1}" # rubocop:disable all

    @attributes[:"#{$1}"] = args[0] # rubocop:disable all
  end

  # Getter.
  @attributes.fetch(name, nil)
end

Instance Attribute Details

#eidObject

Returns the value of attribute eid.



7
8
9
# File 'lib/resource/base.rb', line 7

def eid
  @eid
end

Instance Method Details

#coptsObject

Returns the client options this class was constructed with.



37
38
39
# File 'lib/resource/base.rb', line 37

def copts
  self.class.copts
end

#get(key, default = nil) ⇒ Object

TODO: move towards this



52
53
54
# File 'lib/resource/base.rb', line 52

def get(key, default = nil)
  @attributes.fetch(key, default)
end

#new?Boolean

True if the resource is new (not yet saved).

Returns:

  • (Boolean)


47
48
49
# File 'lib/resource/base.rb', line 47

def new?
  id.nil?
end

#override_properties(properties) ⇒ Object

Set a property hash on the instance.



20
21
22
23
24
# File 'lib/resource/base.rb', line 20

def override_properties(properties)
  properties.each_with_object(self) do |(key, value), this|
    this.send(:"#{key}=", value)
  end
end

#propertiesObject

Returns a copy of class attributes.



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

def properties
  attributes.clone
end

#requestObject

Returns the client’s request object.



42
43
44
# File 'lib/resource/base.rb', line 42

def request
  self.class.request
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/resource/base.rb', line 73

def respond_to_missing?(method_name)
  !methods.include(method_name)
end

#set(key, value) ⇒ Object

TODO: move towards this



57
58
59
# File 'lib/resource/base.rb', line 57

def set(key, value)
  @attributes[key] = value
end

#typeObject

Returns the type of the class.



32
33
34
# File 'lib/resource/base.rb', line 32

def type
  self.class.type
end