Class: APIQL::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/apiql.rb

Direct Known Subclasses

HashEntity

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, context) ⇒ Entity

Returns a new instance of Entity.



165
166
167
168
169
# File 'lib/apiql.rb', line 165

def initialize(object, context)
  @object = object
  @context = context
  @context.inject_delegators(self)
end

Class Attribute Details

.apiql_attributesObject (readonly)

Returns the value of attribute apiql_attributes.



143
144
145
# File 'lib/apiql.rb', line 143

def apiql_attributes
  @apiql_attributes
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



163
164
165
# File 'lib/apiql.rb', line 163

def object
  @object
end

Class Method Details

.attributes(*attrs) ⇒ Object



157
158
159
160
# File 'lib/apiql.rb', line 157

def attributes(*attrs)
  @apiql_attributes ||= []
  @apiql_attributes += attrs.map(&:to_sym)
end

.inherited(child) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/apiql.rb', line 145

def inherited(child)
  super

  return if self.class == ::APIQL::Entity

  attributes = apiql_attributes&.try(:deep_dup) || []

  child.instance_eval do
    @apiql_attributes = attributes
  end
end

Instance Method Details

#render(schema = nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/apiql.rb', line 171

def render(schema = nil)
  return unless @object.present?

  respond = {}

  schema.each do |field|
    if field.is_a? Hash
      field.each do |field, sub_schema|
        name = field.is_a?(Array) ? field.first : field
        respond[name] = render_attribute(field, sub_schema)
      end
    else
      name = field.is_a?(Array) ? field.first : field
      respond[name] = render_attribute(field)
    end
  end

  respond
end