Class: APIQL::Entity
- Inherits:
-
Object
- Object
- APIQL::Entity
- Defined in:
- lib/apiql.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.apiql_attributes ⇒ Object
readonly
Returns the value of attribute apiql_attributes.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
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.
342 343 344 345 346 347 |
# File 'lib/apiql.rb', line 342 def initialize(object, context) @object = object @context = context :read, object @context.inject_delegators(self) end |
Class Attribute Details
.apiql_attributes ⇒ Object (readonly)
Returns the value of attribute apiql_attributes.
320 321 322 |
# File 'lib/apiql.rb', line 320 def apiql_attributes @apiql_attributes end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
340 341 342 |
# File 'lib/apiql.rb', line 340 def object @object end |
Class Method Details
.attributes(*attrs) ⇒ Object
334 335 336 337 |
# File 'lib/apiql.rb', line 334 def attributes(*attrs) @apiql_attributes ||= [] @apiql_attributes += attrs.map(&:to_sym) end |
Instance Method Details
#render(schema = nil) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/apiql.rb', line 349 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 |