How can I custom format an attribute using Ruby Grape Entity?

1k Views Asked by At

I have an attribute price in my model, and currently it is being formatted in scientific notation, and I would rather display in decimal notation.

ruby-grape suggests the following code for custom formatting a field:

class ExampleEntity < Grape::Entity
  expose :formatted_value
  # ...
private

  def formatted_value
    "+ X #{object.value}"
  end
end

However, I've noticed that the value, which is the object's attribute, is not available in this scope. Here an output of #{object.methods}:

[
{
"price": "0.12E3",
"formatted_value": "+ X [:rehash, :to_hash, :to_h, :to_a, :inspect, :to_s,    :==, :[], :hash, :eql?, :fetch, :[]=, :store, :default, :default=,     :default_proc, :default_proc=, :key, :index, :size, :length, :empty?,     :each_value, :each_key, :each_pair, :each, :keys, :values, :values_at,  :shift, :delete, :delete_if, :keep_if, :select, :select!, :reject, :reject!,  :clear, :invert, :update, :replace, :merge!, :merge, :assoc, :rassoc,  :flatten, :include?, :member?, :has_key?, :has_value?, :key?, :value?,  :compare_by_identity, :compare_by_identity?, :any?, :transform_keys,  :transform_keys!, :stringify_keys, :stringify_keys!, :symbolize_keys,  :to_options, :symbolize_keys!, :to_options!, :assert_valid_keys,  :deep_transform_keys, :deep_transform_keys!, :deep_stringify_keys,  :deep_stringify_keys!, :deep_symbolize_keys, :deep_symbolize_keys!,  :reverse_merge, :reverse_merge!, :reverse_update, :with_indifferent_access,  :nested_under_indifferent_access, :blank?, :extractable_options?,  :deep_merge, :deep_merge!, :except, :except!, :slice, :slice!, :extract!,  :to_query, :to_param, :to_xml, :to_json, :entries, :sort, :sort_by, :grep,  :count, :find, :detect, :find_index, :find_all, :collect, :map, :flat_map,  :collect_concat, :inject, :reduce, :partition, :group_by, :first, :all?,  :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by,  :each_with_index, :reverse_each, :each_entry, :each_slice, :each_cons,  :each_with_object, :zip, :take, :take_while, :drop, :drop_while, :cycle,  :chunk, :slice_before, :slice_after, :slice_when, :lazy, :to_set, :present?,  :presence, :acts_like?, :try, :try!, :psych_to_yaml, :to_yaml,  :to_yaml_properties, :nil?, :===, :=~, :!~, :<=>, :class, :singleton_class,  :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?,  :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods,  :private_methods, :public_methods, :instance_variables,  :instance_variable_get, :instance_variable_set, :instance_variable_defined?,  :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send,  :public_send, :respond_to?, :extend, :display, :method, :public_method,  :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for,  :class_eval, :silence_warnings, :enable_warnings, :with_warnings,  :silence_stderr, :silence_stream, :suppress, :capture, :silence, :quietly,  :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]"   
}
]

So the following code throws me { "error": "undefined method `price' for Hash:0x0055a87576f660" }

def formatted_price
 "#{object.price}"
end
1

There are 1 best solutions below

0
On

As the exception said, the exposed object is a hash. Therefore, it's attributes can be found using the object[:value] instead of object.value as following

        private
          def formatted_price
            "%f" % "#{object[:price]}"
          end
        end