Class: ApiMaker::Serializer
- Inherits:
-
Object
- Object
- ApiMaker::Serializer
- Defined in:
- lib/api_maker/serializer.rb
Instance Attribute Summary collapse
-
#ability ⇒ Object
readonly
Returns the value of attribute ability.
-
#api_maker_args ⇒ Object
readonly
Returns the value of attribute api_maker_args.
-
#locals ⇒ Object
readonly
Returns the value of attribute locals.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
- #attribute_value(attribute) ⇒ Object
- #attributes ⇒ Object
- #attributes_to_read ⇒ Object
- #fetch(*args, &blk) ⇒ Object
-
#initialize(ability: nil, api_maker_args: {}, locals: nil, model:, select: nil) ⇒ Serializer
constructor
A new instance of Serializer.
- #inspect ⇒ Object (also: #to_s)
- #load_ability(ability_name, value) ⇒ Object
- #relationships ⇒ Object
- #resource ⇒ Object
- #resource_instance ⇒ Object
- #result ⇒ Object
- #to_json(options = nil) ⇒ Object
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
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
2 3 4 |
# File 'lib/api_maker/serializer.rb', line 2 def ability @ability end |
#api_maker_args ⇒ Object (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 |
#locals ⇒ Object (readonly)
Returns the value of attribute locals.
2 3 4 |
# File 'lib/api_maker/serializer.rb', line 2 def locals @locals end |
#model ⇒ Object (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( = nil) profile("as_json") do if && [: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 |
#attributes ⇒ Object
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_read ⇒ Object
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 |
#inspect ⇒ Object 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 |
#relationships ⇒ Object
65 66 67 |
# File 'lib/api_maker/serializer.rb', line 65 def relationships @relationships ||= {} end |
#resource ⇒ Object
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_instance ⇒ Object
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 |
#result ⇒ Object
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( = nil) profile("to_json") do JSON.generate(as_json()) end end |