Skip to content

Commit

Permalink
Adds is_collection? method in helpers for easier collection detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed Sep 11, 2011
1 parent 7279490 commit d6efc97
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/rabl/engine.rb
Expand Up @@ -33,7 +33,7 @@ def to_hash(options={})
data = data_object(@_data)
if is_object?(data) || !data # object @user
Rabl::Builder.new(@_data, options).to_hash(options)
elsif !is_object?(data) # collection @users
elsif is_collection?(data) # collection @users
object_name = data_name(@_data).to_s.singularize # @users => :users
data.map { |object| Rabl::Builder.new({ object => object_name }, options).to_hash(options) }
end
Expand Down
11 changes: 8 additions & 3 deletions lib/rabl/helpers.rb
Expand Up @@ -52,14 +52,19 @@ def resolve_condition(options)
result
end

# Returns true if item is not enumerable
# Returns true if obj is not enumerable
# is_object?(@user) => true
# is_object?([]) => false
# is_object?({}) => false
def is_object?(obj)
obj && !data_object(obj).is_a?(Enumerable)
end

# Returns true if the obj is a collection of items
def is_collection?(obj)
obj && data_object(obj).is_a?(Enumerable)
end

# Returns source for a given relative file
# fetch_source("show", :view_path => "...") => "...contents..."
def fetch_source(file, options={})
Expand All @@ -74,10 +79,10 @@ def fetch_source(file, options={})
# Padrino chops the extension, stitch it back on
file_path = File.join(@_scope.settings.views, (file_path.to_s + ".rabl"))
end

if file_path
return File.read(file_path.to_s), file_path.to_s
else
else
nil
end
end
Expand Down

0 comments on commit d6efc97

Please sign in to comment.