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 |
# 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) @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
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/zuora_connect_ui/serializer.rb', line 52 def hash_for_collection hash = { data: {} } hash[:data][:items] = @resource.each_with_object([]) do |resource, items| items << record_hash(resource, @record_type) end return hash if @associations.empty? hash[:data][:relationships] = get_associations(@resource) hash end |
#hash_for_one ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/zuora_connect_ui/serializer.rb', line 38 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
32 33 34 35 36 |
# File 'lib/zuora_connect_ui/serializer.rb', line 32 def serializable_hash return hash_for_collection if collection?(@resource) hash_for_one end |
#serialized_json ⇒ Object
25 26 27 28 29 30 |
# File 'lib/zuora_connect_ui/serializer.rb', line 25 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 |