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
59
60
61
62
63
|
# File 'lib/shutl/resource/rest.rb', line 59
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
26
27
28
|
# File 'lib/shutl/resource/rest.rb', line 26
def as_json(_)
resource_attributes
end
|
#attributes ⇒ Object
85
86
87
|
# File 'lib/shutl/resource/rest.rb', line 85
def attributes
resource_attributes
end
|
#destroy(options) ⇒ Object
55
56
57
|
# File 'lib/shutl/resource/rest.rb', line 55
def destroy options
self.class.destroy self, options
end
|
#initialize(args = {}) ⇒ Object
21
22
23
24
|
# File 'lib/shutl/resource/rest.rb', line 21
def initialize(args = {})
update_attributes args
end
|
#next_resource ⇒ Object
69
70
71
|
# File 'lib/shutl/resource/rest.rb', line 69
def next_resource
["next_resource"] if
end
|
#previous_resource ⇒ Object
73
74
75
|
# File 'lib/shutl/resource/rest.rb', line 73
def previous_resource
["previous_resource"] if
end
|
#resource_attributes ⇒ Object
89
90
91
92
93
94
|
# File 'lib/shutl/resource/rest.rb', line 89
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
81
82
83
|
# File 'lib/shutl/resource/rest.rb', line 81
def resource_id
self.instance_variable_get :"@#{self.class.resource_id_name}"
end
|
#respond_to?(method) ⇒ Boolean
65
66
67
|
# File 'lib/shutl/resource/rest.rb', line 65
def respond_to? method
resource_attributes.has_key?(String(method)) ? true : super
end
|
#save(options = {}) ⇒ Object
51
52
53
|
# File 'lib/shutl/resource/rest.rb', line 51
def save options={}
self.class.save self, options
end
|
#to_json(options = nil) ⇒ Object
30
31
32
33
34
|
# File 'lib/shutl/resource/rest.rb', line 30
def to_json(options = nil)
{
:"#{prefix}" => resource_attributes
}.to_json(options)
end
|
#update!(attrs, headers = {}) ⇒ Object
45
46
47
48
49
|
# File 'lib/shutl/resource/rest.rb', line 45
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
36
37
38
39
40
41
42
43
|
# File 'lib/shutl/resource/rest.rb', line 36
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
77
78
79
|
# File 'lib/shutl/resource/rest.rb', line 77
def valid?
errors.blank?
end
|