Skip to content

Commit

Permalink
Extract if check into method
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolgual committed Jun 4, 2012
1 parent 0e5afcb commit 4600a67
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/hypermodel/representation.rb
Expand Up @@ -26,31 +26,28 @@ def self.attributes(*attributes)

def links
@@links.inject({self: context.polymorphic_url(record)}) do |links, (name, options, block)|
if (options[:if] && options[:if].call(record, context)) || options[:if] == nil
value = if block
block.call(record, context)
else
context.polymorphic_url(record.send(name))
end
links.update(name => value)
next links if should_skip(options)

link = if block
block.call(record, context)
else
links
context.polymorphic_url(record.send(name))
end

links.update(name => link)
end
end

def embedded
@@embeds.inject({}) do |embedded, (name, options, block)|
if (options[:if] && options[:if].call(record, context)) || options[:if] == nil
value = if block
block.call(record, context)
else
record.send(name)
end
embedded.update(name => value)
next embedded if should_skip(options)

value = if block
block.call(record, context)
else
embedded
record.send(name)
end
embedded.update(name => value)
end
end

Expand All @@ -59,5 +56,13 @@ def attributes
@@selected_attributes.include? attribute
end
end

private
def should_skip(options)
return false unless options.include? :if
return false if options[:if] == nil

!options[:if].call(record, context)
end
end
end

0 comments on commit 4600a67

Please sign in to comment.