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



159
160
161
162
# File 'lib/apiql.rb', line 159

def initialize(object, context)
  @object = object
  @context = context
end

Class Attribute Details

.apiql_attributesObject (readonly)

Returns the value of attribute apiql_attributes.



137
138
139
# File 'lib/apiql.rb', line 137

def apiql_attributes
  @apiql_attributes
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Class Method Details

.attributes(*attrs) ⇒ Object



151
152
153
154
# File 'lib/apiql.rb', line 151

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

.inherited(child) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/apiql.rb', line 139

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/apiql.rb', line 164

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