lib/strong_attributes/displayable.rb in strong_attributes-0.0.1 vs lib/strong_attributes/displayable.rb in strong_attributes-0.0.2

- old
+ new

@@ -6,52 +6,42 @@ # Displays the result of each method call if permitted. To permit all without checking, call `permit!` first. # # Calls and returns the result of each method call in the argument list. If a block is given, then each result # is passed to the block. # - # user.displays :username, :email # returns [user.username, user.email] + # user.presents :username, :email # returns [user.username, user.email] # # Or with two arguments, the name of the field is passed first: # # <ul> - # <% user.displays :username, :email, :address do |field, value| %> + # <% user.presents :username, :email, :address do |field, value| %> # <li><%= field.capitalize %>: <% value %></li> # <% end %> # </ul> # # If only the presented value is desired, use `each`: # - # <% user.displays(:username, :email).each do |value| %> + # <% user.presents(:username, :email).each do |value| %> # <td><%= value %></td> # <% end %> # # Arguments can be included in an array: # - # user.displays :username, [:notifications, :unread] # returns [user.username, user.notifications(:unread)] + # user.presents :username, [:notifications, :unread] # returns [user.username, user.notifications(:unread)] # - def displays *attributes - select_permitted(*attributes).map do |attribute| - args = Array(attribute).dup - value = self - until args.empty? do - arity = value.method(args[0]).arity - if arity >= 0 - value = value.public_send *args.slice!(0, arity+1) - else - value = value.public_send *args - break - end - end - yield attribute, value if block_given? + def presents *attributes + select_permitted(*attributes).map do |attribute_path| + value = Array(attribute_path).inject(self) { |obj, args| obj.public_send *args } + yield attribute_path, value if block_given? value end end - # Same as displays, but for a single attribute. The differences are: + # Same as presents, but for a single attribute. The differences are: # - the return value is not in an Array # - passes the value only (without attribute key as the 1st argument) to a block - def display field - displays field do |key, value| + def present field + presents field do |key, value| yield value if block_given? end.first end end end