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.



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

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

Class Attribute Details

.apiql_attributesObject (readonly)

Returns the value of attribute apiql_attributes.



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

def apiql_attributes
  @apiql_attributes
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Class Method Details

.attributes(*attrs) ⇒ Object



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

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

.inherited(child) ⇒ Object



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

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



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

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