Module: Shutl::Resource::Rest
- Includes:
- ActiveModel::Serialization
- Defined in:
- lib/shutl/resource/rest.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
58
59
60
61
62
|
# File 'lib/shutl/resource/rest.rb', line 58
def method_missing(method, *args, &block)
return resource_attributes['id'] if String(method) == 'id'
return resource_attributes[String(args.first)] if method.to_s == '[]'
resource_attributes.fetch(String(method)) { super }
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
12
13
14
|
# File 'lib/shutl/resource/rest.rb', line 12
def errors
@errors
end
|
Returns the value of attribute pagination.
12
13
14
|
# File 'lib/shutl/resource/rest.rb', line 12
def
@pagination
end
|
Class Method Details
.included(base) ⇒ Object
14
15
16
17
18
19
|
# File 'lib/shutl/resource/rest.rb', line 14
def self.included(base)
base.send :extend, Shutl::Resource::RestClassMethods
base.send :resource_name, base.name.split('::').last.underscore
base.send :resource_id, :id
end
|
Instance Method Details
#as_json(_) ⇒ Object
25
26
27
|
# File 'lib/shutl/resource/rest.rb', line 25
def as_json(_)
resource_attributes
end
|
#attributes ⇒ Object
84
85
86
|
# File 'lib/shutl/resource/rest.rb', line 84
def attributes
resource_attributes
end
|
#destroy(options) ⇒ Object
54
55
56
|
# File 'lib/shutl/resource/rest.rb', line 54
def destroy options
self.class.destroy self, options
end
|
#initialize(args = {}) ⇒ Object
21
22
23
|
# File 'lib/shutl/resource/rest.rb', line 21
def initialize(args = {})
update_attributes args
end
|
#next_resource ⇒ Object
68
69
70
|
# File 'lib/shutl/resource/rest.rb', line 68
def next_resource
["next_resource"] if
end
|
#previous_resource ⇒ Object
72
73
74
|
# File 'lib/shutl/resource/rest.rb', line 72
def previous_resource
["previous_resource"] if
end
|
#resource_attributes ⇒ Object
88
89
90
91
92
93
|
# File 'lib/shutl/resource/rest.rb', line 88
def resource_attributes
unless self.instance_variables.include?(:@resource_attributes)
self.instance_variable_set(:@resource_attributes, {})
end
self.instance_variable_get :@resource_attributes
end
|
#resource_id ⇒ Object
80
81
82
|
# File 'lib/shutl/resource/rest.rb', line 80
def resource_id
self.instance_variable_get :"@#{self.class.resource_id_name}"
end
|
#respond_to?(method) ⇒ Boolean
64
65
66
|
# File 'lib/shutl/resource/rest.rb', line 64
def respond_to? method
resource_attributes.has_key?(String(method)) ? true : super
end
|
#save(options = {}) ⇒ Object
50
51
52
|
# File 'lib/shutl/resource/rest.rb', line 50
def save options={}
self.class.save self, options
end
|
#to_json(options = nil) ⇒ Object
29
30
31
32
33
|
# File 'lib/shutl/resource/rest.rb', line 29
def to_json(options = nil)
{
:"#{prefix}" => resource_attributes
}.to_json(options)
end
|
#update!(attrs, headers = {}) ⇒ Object
44
45
46
47
48
|
# File 'lib/shutl/resource/rest.rb', line 44
def update!(attrs, = {})
new_attributes = resource_attributes.merge attrs
update_attributes(self.class.add_resource_id_to new_attributes)
save()
end
|
#update_attributes(attrs) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/shutl/resource/rest.rb', line 35
def update_attributes(attrs)
attrs.each do |a, v|
unless String(a) == 'id' && resource_attributes.include?('id')
a = 'id' if String(a) == 'new_id'
resource_attributes[String(a)] = v
end
end
end
|
#valid? ⇒ Boolean
76
77
78
|
# File 'lib/shutl/resource/rest.rb', line 76
def valid?
errors.blank?
end
|