Class: RemoteRecord::Base
- Inherits:
-
Object
- Object
- RemoteRecord::Base
show all
- Includes:
- ActiveSupport::Rescuable
- Defined in:
- lib/remote_record/base.rb
Overview
Remote record classes should inherit from this class and define #get.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(remote_resource_id, remote_record_config = Config.defaults, initial_attrs = {}) ⇒ Base
18
19
20
21
22
23
24
25
|
# File 'lib/remote_record/base.rb', line 18
def initialize(remote_resource_id,
remote_record_config = Config.defaults,
initial_attrs = {})
@remote_resource_id = remote_resource_id
@remote_record_config = remote_record_config
@attrs = HashWithIndifferentAccess.new(initial_attrs)
@fetched = initial_attrs.present?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args, &_block) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/remote_record/base.rb', line 27
def method_missing(method_name, *_args, &_block)
fetch unless @remote_record_config.memoize && @fetched
transform(@attrs).fetch(method_name)
rescue KeyError
super
end
|
Instance Attribute Details
#remote_record_config ⇒ Object
Returns the value of attribute remote_record_config.
16
17
18
|
# File 'lib/remote_record/base.rb', line 16
def remote_record_config
@remote_record_config
end
|
#remote_resource_id ⇒ Object
Returns the value of attribute remote_resource_id.
15
16
17
|
# File 'lib/remote_record/base.rb', line 15
def remote_resource_id
@remote_resource_id
end
|
Class Method Details
.inherited(subclass) ⇒ Object
When you inherit from ‘Base`, it’ll set up an Active Record Type for you available on its Type constant. It’ll also have a Collection.
10
11
12
13
14
|
# File 'lib/remote_record/base.rb', line 10
def self.inherited(subclass)
subclass.const_set :Type, RemoteRecord::Type.for(subclass)
subclass.const_set :Collection, Class.new(RemoteRecord::Collection) unless subclass.const_defined? :Collection
super
end
|
Instance Method Details
#attrs=(new_attrs) ⇒ Object
47
48
49
50
|
# File 'lib/remote_record/base.rb', line 47
def attrs=(new_attrs)
@attrs.update(new_attrs)
@fetched = true
end
|
#fetch ⇒ Object
42
43
44
45
|
# File 'lib/remote_record/base.rb', line 42
def fetch
@attrs.update(get)
@fetched = true
end
|
#fresh ⇒ Object
52
53
54
55
|
# File 'lib/remote_record/base.rb', line 52
def fresh
fetch
self
end
|
#get ⇒ Object
38
39
40
|
# File 'lib/remote_record/base.rb', line 38
def get
raise NotImplementedError.new, '#get should return a hash of data that represents the remote record.'
end
|
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
34
35
36
|
# File 'lib/remote_record/base.rb', line 34
def respond_to_missing?(method_name, _include_private = false)
@attrs.key?(method_name)
end
|