Class: Bulk::Resource
- Inherits:
-
Object
- Object
- Bulk::Resource
- Defined in:
- lib/bulk/resource.rb
Direct Known Subclasses
Constant Summary collapse
- @@resources =
[]
Class Attribute Summary collapse
-
.abstract ⇒ Object
(also: abstract?)
readonly
Returns the value of attribute abstract.
- .application_resource_class ⇒ Object
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
Class Method Summary collapse
- .abstract! ⇒ Object
- .inherited(base) ⇒ Object
- .resource_class(klass = nil) ⇒ Object
- .resource_name(name = nil) ⇒ Object
- .resources(*resources) ⇒ Object
Instance Method Summary collapse
- #as_json_options(klass) ⇒ Object
- #create(hashes) ⇒ Object
- #delete(ids) ⇒ Object
- #get(ids = 'all') ⇒ Object
-
#initialize(controller, options = {}) ⇒ Resource
constructor
A new instance of Resource.
- #resource_name ⇒ Object
- #update(hashes) ⇒ Object
Constructor Details
#initialize(controller, options = {}) ⇒ Resource
Returns a new instance of Resource.
101 102 103 104 105 106 107 |
# File 'lib/bulk/resource.rb', line 101 def initialize(controller, = {}) @controller = controller @resource_name = [:resource_name].to_s if [:resource_name] # try to get klass to raise error early if something is not ok klass unless [:abstract] end |
Class Attribute Details
.abstract ⇒ Object (readonly) Also known as: abstract?
Returns the value of attribute abstract.
14 15 16 |
# File 'lib/bulk/resource.rb', line 14 def abstract @abstract end |
.application_resource_class ⇒ Object
32 33 34 35 |
# File 'lib/bulk/resource.rb', line 32 def application_resource_class @application_resource_class ||= "ApplicationResource" @application_resource_class.is_a?(Class) ? @application_resource_class : Object.const_get(@application_resource_class.to_sym) end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
7 8 9 |
# File 'lib/bulk/resource.rb', line 7 def controller @controller end |
Class Method Details
.abstract! ⇒ Object
51 52 53 54 |
# File 'lib/bulk/resource.rb', line 51 def abstract! @abstract = true @@resources = [] end |
.inherited(base) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/bulk/resource.rb', line 37 def inherited(base) if base.name == application_resource_class.to_s base.abstract! elsif base.name =~ /(.*)Resource$/ base.resource_name($1.underscore.pluralize) end end |
.resource_class(klass = nil) ⇒ Object
17 18 19 20 |
# File 'lib/bulk/resource.rb', line 17 def resource_class(klass = nil) @resource_class = klass if klass @resource_class end |
.resource_name(name = nil) ⇒ Object
22 23 24 25 |
# File 'lib/bulk/resource.rb', line 22 def resource_name(name = nil) @resource_name = name if name @resource_name end |
.resources(*resources) ⇒ Object
27 28 29 30 |
# File 'lib/bulk/resource.rb', line 27 def resources(*resources) @@resources = resources unless resources.blank? @@resources end |
Instance Method Details
#as_json_options(klass) ⇒ Object
182 183 184 |
# File 'lib/bulk/resource.rb', line 182 def (klass) {} end |
#create(hashes) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bulk/resource.rb', line 127 def create(hashes) collection = Collection.new ids = hashes.map { |r| r[:_local_id] } with_records_auth :create, collection, ids do hashes.each do |attrs| local_id = attrs.delete(:_local_id) record = klass.new(filter_params(attrs)) record[:_local_id] = local_id yield record if block_given? with_record_auth :create, collection, local_id, record do record.save set_with_validity_check(collection, local_id, record) end end end collection end |
#delete(ids) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/bulk/resource.rb', line 163 def delete(ids) collection = Collection.new with_records_auth :delete, collection, ids do ids.each do |id| record = klass.where(:id => id).first yield record if block_given? with_record_auth :delete, collection, record.id, record do record.destroy set_with_validity_check(collection, record.id, record) end end end collection end |
#get(ids = 'all') ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bulk/resource.rb', line 109 def get(ids = 'all') ids = ids.to_s == 'all' ? nil : ids collection = Collection.new with_records_auth :get, collection, ids do records = if block_given? yield ids else ids ? klass.where(:id => ids) : klass.all end records.each do |r| with_record_auth :get, collection, r.id, r do collection.set(r.id, r) end end end collection end |
#resource_name ⇒ Object
178 179 180 |
# File 'lib/bulk/resource.rb', line 178 def resource_name @resource_name || self.class.resource_name end |
#update(hashes) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/bulk/resource.rb', line 145 def update(hashes) collection = Collection.new ids = hashes.map { |r| r[:id] } with_records_auth :update, collection, ids do hashes.each do |attrs| attrs.delete(:_local_id) record = klass.where(:id => attrs[:id]).first record.attributes = filter_params(attrs) yield record if block_given? with_record_auth :update, collection, record.id, record do record.save set_with_validity_check(collection, record.id, record) end end end collection end |