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.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#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, args: {}, model:, select: nil) ⇒ Serializer
constructor
A new instance of Serializer.
- #inspect ⇒ Object (also: #to_s)
- #relationships ⇒ Object
- #resource ⇒ Object
- #resource_instance ⇒ Object
- #result ⇒ Object
- #to_json(_options = nil) ⇒ Object
Constructor Details
#initialize(ability: nil, args: {}, model:, select: nil) ⇒ Serializer
Returns a new instance of Serializer.
14 15 16 17 18 19 |
# File 'lib/api_maker/serializer.rb', line 14 def initialize(ability: nil, args: {}, model:, select: nil) @args = args @model = model @ability = ability @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 |
#args ⇒ Object (readonly)
Returns the value of attribute args.
2 3 4 |
# File 'lib/api_maker/serializer.rb', line 2 def args @args 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
4 5 6 7 8 |
# File 'lib/api_maker/serializer.rb', line 4 def self.resource_for(klass) ApiMaker::MemoryStorage.current.resource_for_model(klass) rescue NameError nil end |
.resource_for!(klass) ⇒ Object
10 11 12 |
# File 'lib/api_maker/serializer.rb', line 10 def self.resource_for!(klass) ApiMaker::MemoryStorage.current.resource_for_model(klass) end |
Instance Method Details
#as_json(_options = nil) ⇒ Object
73 74 75 |
# File 'lib/api_maker/serializer.rb', line 73 def as_json( = nil) result end |
#attribute_value(attribute) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/api_maker/serializer.rb', line 41 def attribute_value(attribute) if resource_instance.respond_to?(attribute) resource_instance.__send__(attribute) else model.__send__(attribute) end end |
#attributes ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/api_maker/serializer.rb', line 21 def attributes ApiMaker::Configuration.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
37 38 39 |
# File 'lib/api_maker/serializer.rb', line 37 def attributes_to_read @attributes_to_read ||= @select || resource.default_select end |
#fetch(*args, &blk) ⇒ Object
49 50 51 |
# File 'lib/api_maker/serializer.rb', line 49 def fetch(*args, &blk) result.fetch(*args, &blk) end |
#inspect ⇒ Object Also known as: to_s
81 82 83 |
# File 'lib/api_maker/serializer.rb', line 81 def inspect "<ApiMaker::Serializer id=\"#{model.id}\" model=\"#{model.class.name}\" relationships=\"#{relationships}\">" end |
#relationships ⇒ Object
53 54 55 |
# File 'lib/api_maker/serializer.rb', line 53 def relationships @relationships ||= {} end |
#resource ⇒ Object
57 58 59 |
# File 'lib/api_maker/serializer.rb', line 57 def resource @resource ||= ApiMaker::MemoryStorage.current.resource_for_model(model.class) end |
#resource_instance ⇒ Object
61 62 63 |
# File 'lib/api_maker/serializer.rb', line 61 def resource_instance @resource_instance ||= resource.new(ability: ability, args: args, model: model) end |
#result ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/api_maker/serializer.rb', line 65 def result @result ||= begin result = {a: attributes} result[:r] = @relationships if @relationships # Only include relationships if set result end end |
#to_json(_options = nil) ⇒ Object
77 78 79 |
# File 'lib/api_maker/serializer.rb', line 77 def to_json( = nil) JSON.generate(as_json) end |