Class: Fast::Json::Serializer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fast/json/serializer/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Base

Returns a new instance of Base.



32
33
34
# File 'lib/fast/json/serializer/base.rb', line 32

def initialize(query)
  @query = (query.is_a?(String) ? query : query.to_sql).squish
end

Instance Attribute Details

#queryObject

Returns the value of attribute query.



9
10
11
# File 'lib/fast/json/serializer/base.rb', line 9

def query
  @query
end

Class Method Details

.attribute(attr_name, options = {}, &block) ⇒ Object



25
26
27
28
29
# File 'lib/fast/json/serializer/base.rb', line 25

def attribute(attr_name, options = {}, &block)
  @serializable_attributes ||= []
  fast_attr = Fast::Json::Serializer::Attr.new(attr_name.to_s, options, &block)
  @serializable_attributes.append(fast_attr)
end

.attributes(*attr_names) ⇒ Object



21
22
23
# File 'lib/fast/json/serializer/base.rb', line 21

def attributes(*attr_names)
  attr_names.each { |attr_name| attribute(attr_name) }
end

.set_id(attr_name) ⇒ Object



12
13
14
# File 'lib/fast/json/serializer/base.rb', line 12

def set_id(attr_name)
  @id_column = attr_name.to_s
end

.set_type(attr_name) ⇒ Object

if set_type is not defined, it will return flat result, ie, not fast json api format



17
18
19
# File 'lib/fast/json/serializer/base.rb', line 17

def set_type(attr_name)
  @resource_type = attr_name.to_s
end

Instance Method Details

#build_formatted_hash(hash, resource_type, serializable_attributes, id_key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fast/json/serializer/base.rb', line 55

def build_formatted_hash(hash, resource_type, serializable_attributes, id_key)
  attributes = {}.tap do |h|
    serializable_attributes.collect do |fast_attr|
      h[fast_attr.name] = fast_attr.calc_value(self, hash)
    end
  end

  return attributes unless resource_type

  {
    'id' => hash[id_key],
    'type' => resource_type,
    'attributes' => attributes
  }
end

#pg_resultObject



37
38
39
# File 'lib/fast/json/serializer/base.rb', line 37

def pg_result
  @pg_result ||= ActiveRecord::Base.connection.execute(query)
end

#serializable_hash(data_only: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fast/json/serializer/base.rb', line 41

def serializable_hash(data_only: false)
  resource_type = self.class.instance_variable_get('@resource_type')
  serializable_attributes = self.class.instance_variable_get('@serializable_attributes')
  id_key = self.class.instance_variable_get('@id_column') || 'id'

  array_of_hash = pg_result.collect do |hash|
    build_formatted_hash(hash, resource_type, serializable_attributes, id_key)
  end

  return array_of_hash if data_only || !resource_type

  { 'data' => array_of_hash }
end

#serialized_jsonObject



71
72
73
# File 'lib/fast/json/serializer/base.rb', line 71

def serialized_json
  Oj.dump(serializable_hash)
end