lib/alba/resource.rb in alba-3.3.3 vs lib/alba/resource.rb in alba-3.4.0
- old
+ new
@@ -54,11 +54,11 @@
end
# Serialize object into JSON string
#
# @param root_key [Symbol, nil, true]
- # @param meta [Hash] metadata for this seialization
+ # @param meta [Hash] metadata for this serialization
# @return [String] serialized JSON string
def serialize(root_key: nil, meta: {})
serialize_with(as_json(root_key: root_key, meta: meta))
end
@@ -76,15 +76,15 @@
Kernel.warn(message)
end
serialize(root_key: root_key, meta: meta)
end
- # Returns a Hash correspondng {#serialize}
+ # Returns a Hash corresponding {#serialize}
#
# @param _options [Hash] dummy parameter for Rails compatibility
# @param root_key [Symbol, nil, true]
- # @param meta [Hash] metadata for this seialization
+ # @param meta [Hash] metadata for this serialization
# @return [Hash]
def as_json(_options = {}, root_key: nil, meta: {})
key = root_key.nil? ? fetch_key : root_key
key = Alba.regularize_key(key)
if key && !key.empty?
@@ -97,11 +97,11 @@
# A Hash for serialization
#
# @return [Hash]
def serializable_hash
- collection? ? serializable_hash_for_collection : converter.call(@object)
+ Alba.collection?(@object) ? serializable_hash_for_collection : converter.call(@object)
end
alias to_h serializable_hash
private
@@ -141,11 +141,11 @@
end
end
# @return [String]
def fetch_key
- k = collection? ? _key_for_collection : _key
+ k = Alba.collection?(@object) ? _key_for_collection : _key
transforming_root_key? ? transform_key(k) : k
end
def _key_for_collection
k = @_key_for_collection == true ? resource_name(pluralized: true) : @_key_for_collection
@@ -289,15 +289,9 @@
when Symbol then @within == association_name
when nil, true, false then false # Stop here
else
raise Alba::Error, "Unknown type for within option: #{@within.class}"
end
- end
-
- # Detect if object is a collection or not.
- # When object is a Struct, it's Enumerable but not a collection
- def collection?
- @object.is_a?(Enumerable) && !@object.is_a?(Struct)
end
end
# Class methods
module ClassMethods