Module: HttpApiTools::BaseJsonSerializer
- Included in:
- Nesting::JsonSerializer, Sideloading::JsonSerializer
- Defined in:
- lib/http_api_tools/base_json_serializer.rb
Instance Attribute Summary collapse
-
#relation_includes ⇒ Object
readonly
Returns the value of attribute relation_includes.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#serializable ⇒ Object
readonly
Returns the value of attribute serializable.
Instance Method Summary collapse
- #attribute_hash ⇒ Object
- #attributes ⇒ Object
- #has_manys ⇒ Object
- #has_ones ⇒ Object
- #includable ⇒ Object
- #includes(*includes) ⇒ Object
- #initialize(serializable, attrs = {}) ⇒ Object
- #meta(data) ⇒ Object
- #to_json(*args) ⇒ Object
Instance Attribute Details
#relation_includes ⇒ Object
Returns the value of attribute relation_includes.
12 13 14 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 12 def relation_includes @relation_includes end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
12 13 14 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 12 def result @result end |
#serializable ⇒ Object (readonly)
Returns the value of attribute serializable.
12 13 14 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 12 def serializable @serializable end |
Instance Method Details
#attribute_hash ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 22 def attribute_hash attribute_hash = {} attributes.each do |attr_name| if self.respond_to?(attr_name) attribute_hash[attr_name] = self.send(attr_name) else attribute_hash[attr_name] = serializable.send(attr_name) end end attribute_hash end |
#attributes ⇒ Object
61 62 63 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 61 def attributes self.class._attributes end |
#has_manys ⇒ Object
69 70 71 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 69 def has_manys self.class.has_manys end |
#has_ones ⇒ Object
65 66 67 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 65 def has_ones self.class.has_ones end |
#includable ⇒ Object
73 74 75 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 73 def includable self.class._includable end |
#includes(*includes) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 43 def includes(*includes) if includable allowable_includes_to_add = RelationIncludes.new(*includes) & includable else allowable_includes_to_add = includes end relation_includes.include(allowable_includes_to_add) self end |
#initialize(serializable, attrs = {}) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 14 def initialize(serializable, attrs = {}) @serializable = serializable @relation_includes = attrs[:relation_includes] || RelationIncludes.new @result = attrs[:result] || {} @meta_data = { type: root_key.to_s.singularize, root_key: root_key.to_s } end |
#meta(data) ⇒ Object
56 57 58 59 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 56 def (data) .merge!(data) self end |
#to_json(*args) ⇒ Object
39 40 41 |
# File 'lib/http_api_tools/base_json_serializer.rb', line 39 def to_json(*args) JSON.fast_generate(as_json) end |