lib/shamu/json_api/presenter.rb in shamu-0.0.9 vs lib/shamu/json_api/presenter.rb in shamu-0.0.11

- old
+ new

@@ -43,8 +43,30 @@ delegate :relationship, :attribute, :attributes, :link, :identifier, :meta, to: :builder attr_reader :resource attr_reader :builder + + # Present all the named attributes of the {#resource}. + # @param [Array<Symbol>] names of the resource to present. + def resource_attributes( *names ) + names.map do |name| + attribute name, attribute_value( resource.send( name ) ) + end + end + + # Get a JSON API safe version of the value. + # @param [Object] value the value to be coerced. + # @return [Object] + def attribute_value( value ) + case value + when Date, DateTime then + value.to_date.to_time.iso8601 + when Time, ActiveSupport::TimeWithZone then + value.iso8601 + else value + end + end + end end -end \ No newline at end of file +end