Class: Serializer::Item
- Inherits:
-
Object
- Object
- Serializer::Item
- Defined in:
- app/lib/serializer/item.rb
Instance Attribute Summary collapse
-
#compound_opts ⇒ Object
Returns the value of attribute compound_opts.
-
#current_controller ⇒ Object
Returns the value of attribute current_controller.
-
#format ⇒ Object
Returns the value of attribute format.
-
#includes ⇒ Object
Returns the value of attribute includes.
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(item, includes: [], compound_opts: {}, **params) ⇒ Item
constructor
A new instance of Item.
- #relation_includes ⇒ Object
- #serializer_klass ⇒ Object
Constructor Details
#initialize(item, includes: [], compound_opts: {}, **params) ⇒ Item
Returns a new instance of Item.
4 5 6 7 8 9 10 |
# File 'app/lib/serializer/item.rb', line 4 def initialize(item, includes: [], compound_opts: {}, **params) @record = item @includes = includes @compound_opts = compound_opts @current_controller = params[:current_controller] @format = params[:format] end |
Instance Attribute Details
#compound_opts ⇒ Object
Returns the value of attribute compound_opts.
2 3 4 |
# File 'app/lib/serializer/item.rb', line 2 def compound_opts @compound_opts end |
#current_controller ⇒ Object
Returns the value of attribute current_controller.
2 3 4 |
# File 'app/lib/serializer/item.rb', line 2 def current_controller @current_controller end |
#format ⇒ Object
Returns the value of attribute format.
2 3 4 |
# File 'app/lib/serializer/item.rb', line 2 def format @format end |
#includes ⇒ Object
Returns the value of attribute includes.
2 3 4 |
# File 'app/lib/serializer/item.rb', line 2 def includes @includes end |
#record ⇒ Object
Returns the value of attribute record.
2 3 4 |
# File 'app/lib/serializer/item.rb', line 2 def record @record end |
Instance Method Details
#as_json ⇒ Object
16 17 18 |
# File 'app/lib/serializer/item.rb', line 16 def as_json serializer_klass.new(record, include: relation_includes, format: format, current_controller: current_controller, **compound_opts).serializable_hash end |
#relation_includes ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/lib/serializer/item.rb', line 20 def relation_includes relations = serializer_klass.default_includes @includes = if includes.is_a? String includes.split(",").map(&:underscore).map(&:to_sym) else Array(includes).map(&:to_sym) end relations.concat(includes.compact) end |
#serializer_klass ⇒ Object
12 13 14 |
# File 'app/lib/serializer/item.rb', line 12 def serializer_klass "#{record.class.name}Serializer".constantize end |