-
Notifications
You must be signed in to change notification settings - Fork 38
Prevent circular references (for comment/feedback) #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,7 @@ def meta(key, value) | |
|
|
||
| def entity(name, obj, serializer_class = nil, context_options = {}, &block) | ||
| ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block) | ||
| if ent | ||
| if ent && !serializer.serialized?(name, ent.item.id) | ||
| ent_hash = ent.to_hash | ||
| _name = entity_name(name) | ||
| entity_hash[_name.to_s.pluralize.to_sym] ||= [] | ||
|
|
@@ -80,16 +80,17 @@ def entity(name, obj, serializer_class = nil, context_options = {}, &block) | |
| end | ||
| end | ||
|
|
||
| def entities(name, collection, serializer_class = nil, context_options = {}, &block) | ||
| def entities(name, collection, serializer_class = nil, context_options = nil, &block) | ||
| return if collection.nil? || collection.empty? | ||
| context_options ||= {} | ||
| _name = entity_name(name) | ||
| link_name = _name.to_s.pluralize.to_sym | ||
| data[:links][link_name] = [] | ||
|
|
||
| collection.each do |obj| | ||
| entity_hash[link_name] ||= [] | ||
| ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block) | ||
| if ent | ||
| if ent && !serializer.serialized?(link_name, ent.item.id) | ||
| ent_hash = ent.to_hash | ||
| data[:links][link_name] << ent_hash[:id] | ||
| entity_hash[link_name] << ent_hash | ||
|
|
@@ -104,13 +105,19 @@ def entity_name(name) | |
|
|
||
| private :entity_name | ||
|
|
||
| def collection(name, collection, serializer_class = nil, context_options = {}, &block) | ||
| def collection(name, collection, serializer_class = nil, context_options = nil, &block) | ||
| context_options ||= {} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| @treat_as_resource_collection = true | ||
| data[:resource_collection] = [] unless data[:resource_collection].is_a?(Array) | ||
|
|
||
| collection.each do |obj| | ||
| ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block) | ||
| data[:resource_collection] << ent.to_hash if ent | ||
| if ent | ||
| unless serializer.serialized?(root_name, ent.item.id) | ||
| # serializer.set_serialized(root_name, ent.item.id) | ||
| data[:resource_collection] << ent.to_hash | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,10 @@ class Serializer | |
|
|
||
| class_attribute :_adapter, :logger | ||
|
|
||
| class << self | ||
| attr_accessor :type | ||
| end | ||
|
|
||
| def self.schema(&block) | ||
| @schema = block if block_given? | ||
| @schema || Proc.new{} | ||
|
|
@@ -20,11 +24,16 @@ def self.warn(msg) | |
|
|
||
| attr_reader :item, :context, :adapter_class, :adapter | ||
|
|
||
| def initialize(item, context = {}, _adapter_class = nil, parent_serializer = nil) | ||
| @item, @context = item, context | ||
| def initialize(item, context = nil, _adapter_class = nil, parent_serializer = nil) | ||
| @item = item | ||
| @context = context || {} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same: keeping
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry in this case it makes the conditional assignment redundant. |
||
| @parent_serializer = parent_serializer | ||
| @adapter_class = _adapter_class || self.class.adapter | ||
| @adapter = @adapter_class.new(self) | ||
| if self.class.type | ||
| type(self.class.type) | ||
| end | ||
| @context[:_serialized_entities] ||= {} | ||
| end | ||
|
|
||
| def top | ||
|
|
@@ -51,6 +60,9 @@ def respond_to_missing?(method_name, include_private = false) | |
|
|
||
| def to_hash | ||
| @to_hash ||= ( | ||
| if self.class.type | ||
| Array(item).each { |i| set_serialized(self.class.type, i.id) } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it needed to wrap |
||
| end | ||
| instance_eval(&self.class.schema) | ||
| adapter.to_hash | ||
| ) | ||
|
|
@@ -65,5 +77,16 @@ def map_property(name) | |
| property name, value | ||
| end | ||
|
|
||
| def set_serialized(type, id) | ||
| @context[:_serialized_entities][type] ||= {} | ||
| @context[:_serialized_entities][type][id] = true | ||
| end | ||
|
|
||
| def serialized?(type, id) | ||
| h = @context[:_serialized_entities] | ||
| h = h[type] if h | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| h[id] if h | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should always return h && h[id] |
||
| end | ||
|
|
||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the
content_optionsargument default value isnil, this line will always resolve to{}. So keeping the default value as{}means this line is not necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default only takes effect when the arg is not supplied. The intention is for
context_optionsto be{}even if the argument is passed in explicitly asnil.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, that makes sense. Thanks for clarifying.