Class: ApiMaker::Serializer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ability: nil, api_maker_args: {}, locals: nil, model:, select: nil) ⇒ Serializer

Returns a new instance of Serializer.



16
17
18
19
20
21
22
# File 'lib/api_maker/serializer.rb', line 16

def initialize(ability: nil, api_maker_args: {}, locals: nil, model:, select: nil)
  @ability = ability
  @api_maker_args = api_maker_args
  @locals = locals || api_maker_args&.dig(:locals) || {}
  @model = model
  @select = select
end

Instance Attribute Details

#abilityObject (readonly)

Returns the value of attribute ability.



2
3
4
# File 'lib/api_maker/serializer.rb', line 2

def ability
  @ability
end

#api_maker_argsObject (readonly)

Returns the value of attribute api_maker_args.



2
3
4
# File 'lib/api_maker/serializer.rb', line 2

def api_maker_args
  @api_maker_args
end

#localsObject (readonly)

Returns the value of attribute locals.



2
3
4
# File 'lib/api_maker/serializer.rb', line 2

def locals
  @locals
end

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'lib/api_maker/serializer.rb', line 2

def model
  @model
end

Class Method Details

.resource_for(klass) ⇒ Object



6
7
8
9
10
# File 'lib/api_maker/serializer.rb', line 6

def self.resource_for(klass)
  ApiMaker::MemoryStorage.current.resource_for_model(klass)
rescue NameError
  nil
end

.resource_for!(klass) ⇒ Object



12
13
14
# File 'lib/api_maker/serializer.rb', line 12

def self.resource_for!(klass)
  ApiMaker::MemoryStorage.current.resource_for_model(klass)
end

Instance Method Details

#as_json(options = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/api_maker/serializer.rb', line 88

def as_json(options = nil)
  profile("as_json") do
    if options && options[:result_parser]
      ApiMaker::ResultParser.new(result, ability: ability, api_maker_args: api_maker_args).result
    else
      result
    end
  end
end

#attribute_value(attribute) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/api_maker/serializer.rb', line 46

def attribute_value(attribute)
  profile("attribute_value #{attribute}") do
    if resource_instance.respond_to?(attribute)
      resource_instance.__send__(attribute)
    else
      model.__send__(attribute)
    end
  end
end

#attributesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_maker/serializer.rb', line 24

def attributes
  profile("attributes") do
    result = {}
    attributes_to_read.each do |attribute, data|
      if (if_name = data.dig(:args, :if))
        condition_result = attribute_value(if_name)
        next unless condition_result
      end

      result[attribute] = attribute_value(attribute)
    end

    result
  end
end

#attributes_to_readObject



40
41
42
43
44
# File 'lib/api_maker/serializer.rb', line 40

def attributes_to_read
  @attributes_to_read ||= profile("attributes_to_read") do
    @select || resource.default_select
  end
end

#fetch(*args, &blk) ⇒ Object



56
57
58
# File 'lib/api_maker/serializer.rb', line 56

def fetch(*args, &blk)
  result.fetch(*args, &blk)
end

#inspectObject Also known as: to_s



104
105
106
107
108
# File 'lib/api_maker/serializer.rb', line 104

def inspect
  profile("inspect") do
    "<ApiMaker::Serializer id=\"#{model.id}\" model=\"#{model.class.name}\" relationships=\"#{relationships}\">"
  end
end

#load_ability(ability_name, value) ⇒ Object



60
61
62
63
# File 'lib/api_maker/serializer.rb', line 60

def load_ability(ability_name, value)
  @abilities ||= {}
  @abilities[ability_name] = value
end

#relationshipsObject



65
66
67
# File 'lib/api_maker/serializer.rb', line 65

def relationships
  @relationships ||= {}
end

#resourceObject



69
70
71
72
73
# File 'lib/api_maker/serializer.rb', line 69

def resource
  @resource ||= profile("resource") do
    ApiMaker::MemoryStorage.current.resource_for_model(model.class)
  end
end

#resource_instanceObject



75
76
77
78
79
# File 'lib/api_maker/serializer.rb', line 75

def resource_instance
  @resource_instance ||= profile("resource_instance") do
    resource.new(ability: ability, api_maker_args: api_maker_args, locals: locals, model: model)
  end
end

#resultObject



81
82
83
84
85
86
# File 'lib/api_maker/serializer.rb', line 81

def result
  result = {a: attributes}
  result[:b] = @abilities if @abilities # Only use b-key if any abilities was loaded
  result[:r] = @relationships if @relationships # Only preload relationships if set
  result
end

#to_json(options = nil) ⇒ Object



98
99
100
101
102
# File 'lib/api_maker/serializer.rb', line 98

def to_json(options = nil)
  profile("to_json") do
    JSON.generate(as_json(options))
  end
end