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
|
# File 'lib/shutl/resource/rest.rb', line 58
def method_missing(method, *args, &block)
return attributes[String(args.first)] if method.to_s == '[]'
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(_)
attributes
end
|
#attributes ⇒ Object
83
84
85
|
# File 'lib/shutl/resource/rest.rb', line 83
def attributes
@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
67
68
69
|
# File 'lib/shutl/resource/rest.rb', line 67
def next_resource
["next_resource"] if
end
|
#previous_resource ⇒ Object
71
72
73
|
# File 'lib/shutl/resource/rest.rb', line 71
def previous_resource
["previous_resource"] if
end
|
#resource_id ⇒ Object
79
80
81
|
# File 'lib/shutl/resource/rest.rb', line 79
def resource_id
instance_variable_get :"@#{self.class.resource_id_name}"
end
|
#respond_to?(method) ⇒ Boolean
63
64
65
|
# File 'lib/shutl/resource/rest.rb', line 63
def respond_to? method
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}" => 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 = 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' && attributes.include?('id')
a = 'id' if String(a) == 'new_id'
attributes.update String(a) => v
end
end
end
|
#valid? ⇒ Boolean
75
76
77
|
# File 'lib/shutl/resource/rest.rb', line 75
def valid?
errors.blank?
end
|