codenoble / cache-crispies

Speedy Rails JSON serialization with built-in caching
MIT License
156 stars 14 forks source link

Blocks for custom attributes #3

Closed adamcrown closed 4 years ago

adamcrown commented 5 years ago

Currently if you want to do anything other than return the value directly (or casted to another data type) you need to define a method for it.

class UserSerializer < CacheCrispies::Base
  serialize :full_name

  def full_name
    "#{model.first_name} #{model.last_name}"
  end
end

It would be nice to be able to do this more succinctly like

class UserSerializer < CacheCrispies::Base
    serialize :full_name { |user| "#{user.first_name} #{user.last_name}" }
end

But testing needs to be done to make sure there is little to no performance impact.