Class: JsonapiCompliable::Resource
- Inherits:
-
Object
- Object
- JsonapiCompliable::Resource
- Defined in:
- lib/jsonapi_compliable/resource.rb
Class Attribute Summary collapse
-
.config ⇒ Object
Returns the value of attribute config.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
- .allow_filter(name, *args, &blk) ⇒ Object
- .allow_stat(symbol_or_hash, &blk) ⇒ Object
- .default_filter(name, &blk) ⇒ Object
- .default_page_number(val) ⇒ Object
- .default_page_size(val) ⇒ Object
- .default_sort(val) ⇒ Object
- .extra_field(name, &blk) ⇒ Object
- .inherited(klass) ⇒ Object
- .model(klass) ⇒ Object
- .paginate(&blk) ⇒ Object
- .sideload_whitelist(whitelist) ⇒ Object
- .sideloading ⇒ Object
- .sort(&blk) ⇒ Object
- .type(value = nil) ⇒ Object
- .use_adapter(klass) ⇒ Object
Instance Method Summary collapse
- #adapter ⇒ Object
- #allowed_sideloads(namespace = nil) ⇒ Object
- #association_names ⇒ Object
- #build_scope(base, query, opts = {}) ⇒ Object
- #create(create_params) ⇒ Object
- #default_filters ⇒ Object
- #default_page_number ⇒ Object
- #default_page_size ⇒ Object
- #default_sort ⇒ Object
- #destroy(id) ⇒ Object
- #extra_fields ⇒ Object
- #filters ⇒ Object
- #model ⇒ Object
- #pagination ⇒ Object
- #persist_with_relationships(meta, attributes, relationships) ⇒ Object
- #resolve(scope) ⇒ Object
- #sideload_whitelist ⇒ Object
- #sideloading ⇒ Object
- #sorting ⇒ Object
- #stat(attribute, calculation) ⇒ Object
- #stats ⇒ Object
- #transaction ⇒ Object
- #type ⇒ Object
- #update(update_params) ⇒ Object
- #with_context(object, namespace = nil) ⇒ Object
Class Attribute Details
.config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/jsonapi_compliable/resource.rb', line 6 def config @config end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
3 4 5 |
# File 'lib/jsonapi_compliable/resource.rb', line 3 def context @context end |
Class Method Details
.allow_filter(name, *args, &blk) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/jsonapi_compliable/resource.rb', line 31 def self.allow_filter(name, *args, &blk) opts = args. aliases = [name, opts[:aliases]].flatten.compact config[:filters][name.to_sym] = { aliases: aliases, if: opts[:if], filter: blk } end |
.allow_stat(symbol_or_hash, &blk) ⇒ Object
41 42 43 44 45 |
# File 'lib/jsonapi_compliable/resource.rb', line 41 def self.allow_stat(symbol_or_hash, &blk) dsl = Stats::DSL.new(config[:adapter], symbol_or_hash) dsl.instance_eval(&blk) if blk config[:stats][dsl.name] = dsl end |
.default_filter(name, &blk) ⇒ Object
47 48 49 50 51 |
# File 'lib/jsonapi_compliable/resource.rb', line 47 def self.default_filter(name, &blk) config[:default_filters][name.to_sym] = { filter: blk } end |
.default_page_number(val) ⇒ Object
81 82 83 |
# File 'lib/jsonapi_compliable/resource.rb', line 81 def self.default_page_number(val) config[:default_page_number] = val end |
.default_page_size(val) ⇒ Object
85 86 87 |
# File 'lib/jsonapi_compliable/resource.rb', line 85 def self.default_page_size(val) config[:default_page_size] = val end |
.default_sort(val) ⇒ Object
73 74 75 |
# File 'lib/jsonapi_compliable/resource.rb', line 73 def self.default_sort(val) config[:default_sort] = val end |
.extra_field(name, &blk) ⇒ Object
65 66 67 |
# File 'lib/jsonapi_compliable/resource.rb', line 65 def self.extra_field(name, &blk) config[:extra_fields][name] = blk end |
.inherited(klass) ⇒ Object
19 20 21 |
# File 'lib/jsonapi_compliable/resource.rb', line 19 def self.inherited(klass) klass.config = Util::Hash.deep_dup(self.config) end |
.model(klass) ⇒ Object
53 54 55 |
# File 'lib/jsonapi_compliable/resource.rb', line 53 def self.model(klass) config[:model] = klass end |
.paginate(&blk) ⇒ Object
61 62 63 |
# File 'lib/jsonapi_compliable/resource.rb', line 61 def self.paginate(&blk) config[:pagination] = blk end |
.sideload_whitelist(whitelist) ⇒ Object
27 28 29 |
# File 'lib/jsonapi_compliable/resource.rb', line 27 def self.sideload_whitelist(whitelist) config[:sideload_whitelist] = JSONAPI::IncludeDirective.new(whitelist).to_hash end |
.sideloading ⇒ Object
23 24 25 |
# File 'lib/jsonapi_compliable/resource.rb', line 23 def self.sideloading @sideloading ||= Sideload.new(:base, resource: self) end |
.sort(&blk) ⇒ Object
57 58 59 |
# File 'lib/jsonapi_compliable/resource.rb', line 57 def self.sort(&blk) config[:sorting] = blk end |
.type(value = nil) ⇒ Object
77 78 79 |
# File 'lib/jsonapi_compliable/resource.rb', line 77 def self.type(value = nil) config[:type] = value end |
.use_adapter(klass) ⇒ Object
69 70 71 |
# File 'lib/jsonapi_compliable/resource.rb', line 69 def self.use_adapter(klass) config[:adapter] = klass.new end |
Instance Method Details
#adapter ⇒ Object
220 221 222 |
# File 'lib/jsonapi_compliable/resource.rb', line 220 def adapter self.class.config[:adapter] end |
#allowed_sideloads(namespace = nil) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/jsonapi_compliable/resource.rb', line 151 def allowed_sideloads(namespace = nil) return {} unless sideloading namespace ||= context[:namespace] sideloads = sideloading.to_hash[:base] if !sideload_whitelist.empty? && namespace sideloads = Util::IncludeParams.scrub(sideloads, sideload_whitelist[namespace]) end sideloads end |
#association_names ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/jsonapi_compliable/resource.rb', line 141 def association_names @association_names ||= begin if sideloading Util::Hash.keys(sideloading.to_hash[:base]) else [] end end end |
#build_scope(base, query, opts = {}) ⇒ Object
119 120 121 |
# File 'lib/jsonapi_compliable/resource.rb', line 119 def build_scope(base, query, opts = {}) Scope.new(base, self, query, opts) end |
#create(create_params) ⇒ Object
123 124 125 |
# File 'lib/jsonapi_compliable/resource.rb', line 123 def create(create_params) adapter.create(model, create_params) end |
#default_filters ⇒ Object
212 213 214 |
# File 'lib/jsonapi_compliable/resource.rb', line 212 def default_filters self.class.config[:default_filters] end |
#default_page_number ⇒ Object
176 177 178 |
# File 'lib/jsonapi_compliable/resource.rb', line 176 def default_page_number self.class.config[:default_page_number] || 1 end |
#default_page_size ⇒ Object
180 181 182 |
# File 'lib/jsonapi_compliable/resource.rb', line 180 def default_page_size self.class.config[:default_page_size] || 20 end |
#default_sort ⇒ Object
172 173 174 |
# File 'lib/jsonapi_compliable/resource.rb', line 172 def default_sort self.class.config[:default_sort] || [{ id: :asc }] end |
#destroy(id) ⇒ Object
131 132 133 |
# File 'lib/jsonapi_compliable/resource.rb', line 131 def destroy(id) adapter.destroy(model, id) end |
#extra_fields ⇒ Object
204 205 206 |
# File 'lib/jsonapi_compliable/resource.rb', line 204 def extra_fields self.class.config[:extra_fields] end |
#filters ⇒ Object
188 189 190 |
# File 'lib/jsonapi_compliable/resource.rb', line 188 def filters self.class.config[:filters] end |
#model ⇒ Object
216 217 218 |
# File 'lib/jsonapi_compliable/resource.rb', line 216 def model self.class.config[:model] end |
#pagination ⇒ Object
200 201 202 |
# File 'lib/jsonapi_compliable/resource.rb', line 200 def pagination self.class.config[:pagination] end |
#persist_with_relationships(meta, attributes, relationships) ⇒ Object
135 136 137 138 139 |
# File 'lib/jsonapi_compliable/resource.rb', line 135 def persist_with_relationships(, attributes, relationships) persistence = JsonapiCompliable::Util::Persistence \ .new(self, , attributes, relationships) persistence.run end |
#resolve(scope) ⇒ Object
224 225 226 |
# File 'lib/jsonapi_compliable/resource.rb', line 224 def resolve(scope) adapter.resolve(scope) end |
#sideload_whitelist ⇒ Object
208 209 210 |
# File 'lib/jsonapi_compliable/resource.rb', line 208 def sideload_whitelist self.class.config[:sideload_whitelist] end |
#sideloading ⇒ Object
168 169 170 |
# File 'lib/jsonapi_compliable/resource.rb', line 168 def sideloading self.class.sideloading end |
#sorting ⇒ Object
192 193 194 |
# File 'lib/jsonapi_compliable/resource.rb', line 192 def sorting self.class.config[:sorting] end |
#stat(attribute, calculation) ⇒ Object
162 163 164 165 166 |
# File 'lib/jsonapi_compliable/resource.rb', line 162 def stat(attribute, calculation) stats_dsl = stats[attribute] || stats[attribute.to_sym] raise Errors::StatNotFound.new(attribute, calculation) unless stats_dsl stats_dsl.calculation(calculation) end |
#stats ⇒ Object
196 197 198 |
# File 'lib/jsonapi_compliable/resource.rb', line 196 def stats self.class.config[:stats] end |
#transaction ⇒ Object
228 229 230 231 232 |
# File 'lib/jsonapi_compliable/resource.rb', line 228 def transaction adapter.transaction(model) do yield end end |
#type ⇒ Object
184 185 186 |
# File 'lib/jsonapi_compliable/resource.rb', line 184 def type self.class.config[:type] || :undefined_jsonapi_type end |
#update(update_params) ⇒ Object
127 128 129 |
# File 'lib/jsonapi_compliable/resource.rb', line 127 def update(update_params) adapter.update(model, update_params) end |
#with_context(object, namespace = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/jsonapi_compliable/resource.rb', line 105 def with_context(object, namespace = nil) begin prior = context @context = { object: object, namespace: namespace } yield ensure @context = prior end end |