Class: APIQL::Entity
- Inherits:
-
Object
- Object
- APIQL::Entity
- Defined in:
- lib/apiql.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
- .all ⇒ Object
- .create(params) ⇒ Object
- .destroy(id) ⇒ Object
- .find(id) ⇒ Object
- .update(id, params) ⇒ Object
Instance Method Summary collapse
-
#initialize(object, context) ⇒ Entity
constructor
A new instance of Entity.
- #render(schema = nil) ⇒ Object
Constructor Details
#initialize(object, context) ⇒ Entity
Returns a new instance of Entity.
401 402 403 404 405 406 |
# File 'lib/apiql.rb', line 401 def initialize(object, context) @object = object @context = context :read, object @context.inject_delegators(self) end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
399 400 401 |
# File 'lib/apiql.rb', line 399 def object @object end |
Class Method Details
.all ⇒ Object
316 317 318 319 320 |
# File 'lib/apiql.rb', line 316 def all :read, @apiql_entity_class @apiql_entity_class.eager_load(eager_load).all end |
.create(params) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/apiql.rb', line 330 def create(params) :create, @apiql_entity_class if respond_to?(:create_params, params) params = create_params(params) elsif respond_to?(:params, params) params = self.params(params) end @apiql_entity_class.create!(params) end |
.destroy(id) ⇒ Object
356 357 358 359 360 361 362 |
# File 'lib/apiql.rb', line 356 def destroy(id) item = @apiql_entity_class.find(id) :destroy, item item.destroy! end |
.find(id) ⇒ Object
322 323 324 325 326 327 328 |
# File 'lib/apiql.rb', line 322 def find(id) item = @apiql_entity_class.eager_load(eager_load).find(id) :read, item item end |
.update(id, params) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/apiql.rb', line 342 def update(id, params) item = @apiql_entity_class.find(id) :update, item if respond_to?(:update_params, params) params = update_params(params) elsif respond_to?(:params, params) params = self.params(params) end item.update!(params) end |
Instance Method Details
#render(schema = nil) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/apiql.rb', line 408 def render(schema = nil) return unless @object.present? respond = {} schema.each do |field| if field.is_a? Hash field.each do |field, sub_schema| reg = field.match(/\A((?<alias>[\w\.\!\?]+):\s*)?(?<name>[\w\.\!\?]+)(\((?<params>.*?)\))?\z/) raise Error, field unless reg.present? name = reg[:alias] || reg[:name] if respond[name].is_a? ::Hash respond = respond.deep_merge({ name => render_attribute(reg[:name], reg[:params].presence, sub_schema) }) else respond[name] = render_attribute(reg[:name], reg[:params].presence, sub_schema) end end else reg = field.match(/\A((?<alias>[\w\.\!\?]+):\s*)?(?<name>[\w\.\!\?]+)(\((?<params>.*?)\))?\z/) raise Error, field unless reg.present? name = reg[:alias] || reg[:name] if respond[name].is_a? ::Hash respond = respond.deep_merge({ name => render_attribute(reg[:name], reg[:params].presence) }) else respond[name] = render_attribute(reg[:name], reg[:params].presence) end end end respond end |