Class: CachedSerializer::Base
- Inherits:
-
Object
- Object
- CachedSerializer::Base
- Defined in:
- lib/cached_serializer/base.rb
Instance Attribute Summary collapse
-
#subject ⇒ Object
Returns the value of attribute subject.
Class Method Summary collapse
-
.columns(*column_names) ⇒ Object
Example (in a UserSerializer):.
-
.computed(attr_name, columns: [], recompute_if: nil, expires_in: nil, &recompute) ⇒ Object
Example (in a UserSerializer):.
-
.constant(*attr_names, &recompute) ⇒ Object
Example (in a UserSerializer):.
- .serializers ⇒ Object
-
.volatile(*attr_names, &recompute) ⇒ Object
Example (in a UserSerializer):.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(subject) ⇒ Base
constructor
A new instance of Base.
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(subject) ⇒ Base
Returns a new instance of Base.
144 145 146 |
# File 'lib/cached_serializer/base.rb', line 144 def initialize(subject) self.subject = subject end |
Instance Attribute Details
#subject ⇒ Object
Returns the value of attribute subject.
7 8 9 |
# File 'lib/cached_serializer/base.rb', line 7 def subject @subject end |
Class Method Details
.columns(*column_names) ⇒ Object
Example (in a UserSerializer):
columns :email, :phone
This will call ‘some_user.email` for the `:email` attribute, and `some_user.phone` for the `:phone` attribute. It will cache the value for each, until the attribute changes on the record.
24 25 26 27 28 29 |
# File 'lib/cached_serializer/base.rb', line 24 def columns(*column_names) column_names.each do |column_name| serializers << AttrSerializer.new(column_name) add_column_changed_cache_invalidator_callback(column_name, column_name) end end |
.computed(attr_name, columns: [], recompute_if: nil, expires_in: nil, &recompute) ⇒ Object
Example (in a UserSerializer):
computed :name, columns: [:first_name, :last_name] do |user|
"#{user.first_name} #{user.last_name}"
end
This will use the result of the block as the value for the ‘:name` attribute. It will cache the result until either `:first_name` or `:last_name` changes on the user record.
computed :active, recompute_if: ->(u) { u.last_logged_in_at > 1.week.ago } do |user|
user.purchases.where(created_at: 1.week.ago..Time.zone.now).present?
end
This will use the result of the block as the value for the ‘:active` attribute. It will cache the result until the `recompute_if` proc/lambda returns `true`.
computed :purchase_count, expires_in: 1.day do |user|
user.purchases.count
end
This will use the result of the block as the value for the ‘:purchase_count` attribute. It will cache the result for one day after the last time the `:purchase_count` attribute was recomputed.
computed :silly, columns: [:foo, :bar], recompute_if: ->(u) { u.silly? }, expires_in: 10.seconds do |user|
rand(100)
end
This will use the result of the block as the value for the ‘:silly` attribute. It will cache the result until `:foo` changes on the user, `:bar` changes on the user, `u.silly?` at the time of serialization, or it has been more than 10 seconds since the last time the `:silly` attribute was recomputed.
111 112 113 114 115 116 117 118 119 |
# File 'lib/cached_serializer/base.rb', line 111 def computed(attr_name, columns: [], recompute_if: nil, expires_in: nil, &recompute) if (columns.empty? && !recompute_if && !expires_in) raise ArgumentError, "Must provide :columns, :recompute_if, or :expires_in to a computed attribute setter" end serializers << AttrSerializer.new(attr_name, recompute_if, expires_in, &recompute) columns.each do |column_name| add_column_changed_cache_invalidator_callback(attr_name, column_name) end end |
.constant(*attr_names, &recompute) ⇒ Object
Example (in a UserSerializer):
constant :email, :phone
This will call ‘some_user.email` for the `:email` attribute, and `some_user.phone` for the `:phone` attribute. It will cache the value for each FOREVER, and never recompute it.
constant :name do |user|
"#{user.first_name} #{user.last_name}"
end
This will use the result of the block as the value for the ‘:name` attribute. It will cache the value FOREVER, and never recompute it.
46 47 48 49 50 |
# File 'lib/cached_serializer/base.rb', line 46 def constant(*attr_names, &recompute) attr_names.each do |attr_name| serializers << AttrSerializer.new(attr_name, &recompute) end end |
.serializers ⇒ Object
12 13 14 |
# File 'lib/cached_serializer/base.rb', line 12 def serializers @serializers end |
.volatile(*attr_names, &recompute) ⇒ Object
Example (in a UserSerializer):
volatile :email, :phone
This will call ‘some_user.email` for the `:email` attribute, and `some_user.phone` for the `:phone` attribute. It will ALWAYS recompute the values, EVERY time it serializes a user.
volatile :name do |user|
"#{user.first_name} #{user.last_name}"
end
This will use the result of the block as the value for the ‘:name` attribute. It will ALWAYS recompute the value, EVERY time it serializes a user.
68 69 70 71 72 73 |
# File 'lib/cached_serializer/base.rb', line 68 def volatile(*attr_names, &recompute) attr_names.each do |attr_name| always_recompute = proc { |_subj| true } serializers << AttrSerializer.new(attr_name, always_recompute, &recompute) end end |
Instance Method Details
#as_json ⇒ Object
152 153 154 |
# File 'lib/cached_serializer/base.rb', line 152 def as_json to_h end |
#to_h ⇒ Object
148 149 150 |
# File 'lib/cached_serializer/base.rb', line 148 def to_h self.class.serializers.serialize_for(subject) end |
#to_json ⇒ Object
156 157 158 |
# File 'lib/cached_serializer/base.rb', line 156 def to_json JSON.generate(as_json) end |