Class: ZuoraConnectUi::Serializer
- Inherits:
-
Object
- Object
- ZuoraConnectUi::Serializer
- Defined in:
- lib/zuora_connect_ui/serializer.rb,
lib/zuora_connect_ui/serializer/relationship.rb
Overview
Take a resource and dump it to Google Style JSON, really fast
Defined Under Namespace
Classes: Relationship
Instance Method Summary collapse
- #hash_for_collection ⇒ Object
- #hash_for_one ⇒ Object
-
#initialize(resource, options = {}) ⇒ Serializer
constructor
A new instance of Serializer.
- #serializable_hash ⇒ Object
- #serialized_json ⇒ Object
Constructor Details
#initialize(resource, options = {}) ⇒ Serializer
Returns a new instance of Serializer.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/zuora_connect_ui/serializer.rb', line 9 def initialize(resource, = {}) @resource = resource @kind = [:kind] @is_collection = [:is_collection] @record_type = parse_record_type(@kind, @resource) @camel_type = key_transform(@record_type) @cache_keys = {} @associations = parse_associations( [:has_many], [:belongs_to], [:has_one] ) @fields = parse_fields([:fields]) # TODO: if an attribute given is reserved, throw an error end |
Instance Method Details
#hash_for_collection ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/zuora_connect_ui/serializer.rb', line 54 def hash_for_collection hash = { data: { items: [] } } @resource.each do |resource| hash[:data][:items] << record_hash(resource, @record_type) end return hash if @associations.empty? hash[:data][:relationships] = get_associations(@resource) hash end |
#hash_for_one ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/zuora_connect_ui/serializer.rb', line 40 def hash_for_one hash = { data: nil } return hash unless @resource hash[:data] = record_hash(@resource, @record_type) return hash if @associations.empty? hash[:data][:relationships] = get_associations([@resource]) hash end |
#serializable_hash ⇒ Object
34 35 36 37 38 |
# File 'lib/zuora_connect_ui/serializer.rb', line 34 def serializable_hash return hash_for_collection if collection?(@resource) hash_for_one end |
#serialized_json ⇒ Object
27 28 29 30 31 32 |
# File 'lib/zuora_connect_ui/serializer.rb', line 27 def serialized_json require 'oj' # Enable to show Oj working in console # Oj.default_options = { trace: true } Oj.dump(serializable_hash, mode: :compat, time_format: :ruby) end |