Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use trailblazer-option over declarative-option and upgrade declarative-builder #492

Merged
merged 2 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cells.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]
spec.required_ruby_version = ">= 2.2.10"

spec.add_dependency "declarative-builder", "< 0.2.0"
spec.add_dependency "declarative-option", "< 0.2.0"
spec.add_dependency "declarative-builder", "~> 0.2.0"
spec.add_dependency "trailblazer-option", "~> 0.1.0"
spec.add_dependency "tilt", ">= 1.4", "< 3"
spec.add_dependency "uber", "< 0.2.0"

Expand Down
18 changes: 8 additions & 10 deletions lib/cell/caching.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "declarative/options"
require "cell/option"

module Cell
module Caching
Expand All @@ -17,12 +17,10 @@ def self.included(includer)
end

module ClassMethods
def cache(state, *args, &block)
options = args.last.is_a?(Hash) ? args.pop : {} # I have to admit, Array#extract_options is a brilliant tool.

conditional_procs[state] = Declarative::Option(options.delete(:if) || true, instance_exec: true)
version_procs[state] = Declarative::Option(args.first || block, instance_exec: true)
cache_options[state] = Declarative::Options(options, instance_exec: true)
def cache(state, *args, **kwargs, &block)
conditional_procs[state] = Cell::Option(kwargs.delete(:if) || true)
version_procs[state] = Cell::Option(args.first || block)
cache_options[state] = Cell::Options(kwargs)
end

# Computes the complete, namespaced cache key for +state+.
Expand All @@ -45,8 +43,8 @@ def render_state(state, *args)
state = state.to_sym
return super(state, *args) unless cache?(state, *args)

key = self.class.state_cache_key(state, self.class.version_procs[state].(self, *args))
options = self.class.cache_options[state].(self, *args)
key = self.class.state_cache_key(state, self.class.version_procs[state].(*args, exec_context: self))
options = self.class.cache_options[state].(*args, exec_context: self)

fetch_from_cache_for(key, options) { super(state, *args) }
end
Expand All @@ -56,7 +54,7 @@ def cache_store # we want to use DI to set a cache store in cell/rails.
end

def cache?(state, *args)
perform_caching? and state_cached?(state) and self.class.conditional_procs[state].(self, *args)
perform_caching? and state_cached?(state) and self.class.conditional_procs[state].(*args, exec_context: self)
end

private
Expand Down
35 changes: 35 additions & 0 deletions lib/cell/option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "trailblazer/option"
require "uber/callable"

# DISCUSS: Should we move this to trb-option instead ?
# This is identical to `Representable::Option`.
module Cell
# Extend `Trailblazer::Option` to support static values as callables too.
class Option < ::Trailblazer::Option
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apotonick This is identical to Representable::Option.

Should we move Cell::Option and Cell::Options in trb-option instead ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, do we have use on a generic level?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, kind of.

  1. Handling static values might be needed somewhere else.
  2. But Cell::Options isn't generic. We can keep it here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is, do we have the static requirement anywhere else (e.g. Reform)? If yes, we can extract it. On the other hand, we can always do that later. 🥱

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we've the static requirement in reform, disposable and representer. Currently, it is done in representer itself and it's being used in other 2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, supporting Callable objects between these 2 types of gems (representable, reform, cells etc and trb-activity, DSL etc) is little different i.e.

  1. Extending Uber::Callable for identifying callable object.
  2. trb-activity and friends doesn't need static type support.

So other way is we should not mix these 2 types in trb-option but keep separate. If it's ok, then we can keep Cell::Option here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Reg. Uber::Callable, that one we should dump ASAP. Isn't it better if we always treat an object callable, anyway?
  2. Hm, but it also doesn't hurt to have it there instead of maintaining 4 different Options, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yeah. The problem with treating every object callable is we can't distinguish between them and static options (some static options also includes modules, classes etc), apart from call method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, ok right, so that was why I initially introduced Uber::Callable, damn! Hm... how could we do that in the future?

Ok, in that case, we won't move that behavior to TRB, for now. 🍰

Copy link
Member Author

@yogeshjain999 yogeshjain999 Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct (i.e. distinction between static and callable objects isn't needed in TRB). So we can merge this PR.

Uber::Callable is a nice alternative to mark callables for cells and representable friends for now.
In future versions, maybe we can have restrictions on static options (or ways to avoid them) ?

def self.callable?(value)
[Proc, Symbol, Uber::Callable].any?{ |kind| value.is_a?(kind) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this change. We quite frequently pass symbols as cell options, is this going to to try and evaluate them as a method name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samstickland This behaviour is same as the previous Declatative::Option. The difference with Trailblazer::Option in the API only, and it's compatible with Ruby-3.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yogeshjain999 We do have static values covered in the cells suite, I guess? 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh really ? I thought those are covered in declarative-option directly.

end

def self.build(value)
return ->(*) { value } unless callable?(value) # Wrap static `value` into a proc.
super
end
end

class Options < Hash
# Evaluates every element and returns a hash. Accepts arbitrary arguments.
def call(*args, **options, &block)
Hash[ collect { |k,v| [k,v.(*args, **options, &block) ] } ]
end
end

def self.Option(value)
::Cell::Option.build(value)
end

def self.Options(options)
Options.new.tap do |hsh|
options.each { |k,v| hsh[k] = Option(v) }
end
end
end
74 changes: 64 additions & 10 deletions test/cache_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "test_helper"

# TODO: test caching without rails

class CacheTest < Minitest::Spec
STORE = Class.new(Hash) do
def fetch(key, options, &block)
Expand All @@ -10,23 +8,79 @@ def fetch(key, options, &block)
end.new

module Cache
def show
def show(*)
"#{@model}"
end

def cache_store
STORE
end

def has_changed?(*)
@model < 3
end
end

Template = ->(action, *args, **kwargs, &block) {
Class.new(Cell::ViewModel) do
cache action, *args, **kwargs, &block
include Cache
end
}

it "without any arguments" do
Default = Template.(:show)

_(Default.new(1).()).must_equal("1")
_(Default.new(2).()).must_equal("1")
end

it "with specified version" do
# Cache invalidation using version proc
WithVersionArg = Template.(:show, ->(options) { options[:version] })

_(WithVersionArg.new(1).(:show, version: 1)).must_equal("1")
_(WithVersionArg.new(2).(:show, version: 1)).must_equal("1")

_(WithVersionArg.new(3).(:show, version: 2)).must_equal("3")

# Cache invalidation using version block
WithVersionBlock = Template.(:show) { |options| options[:version] }

_(WithVersionBlock.new(1).(:show, version: 1)).must_equal("1")
_(WithVersionBlock.new(2).(:show, version: 1)).must_equal("1")

_(WithVersionBlock.new(3).(:show, version: 2)).must_equal("3")
end

class Index < Cell::ViewModel
cache :show
include Cache
it "with conditional" do
WithConditional = Template.(:show, if: :has_changed?)

_(WithConditional.new(1).(:show)).must_equal("1")
_(WithConditional.new(2).(:show)).must_equal("1")

_(WithConditional.new(3).(:show)).must_equal("3")
end

it do
_(Index.new(1).()).must_equal("1")
_(Index.new(2).()).must_equal("1")
it "forwards remaining options to cache store" do
WithOptions = Class.new(Cell::ViewModel) do
cache :show, if: :has_changed?, expires_in: 10, tags: ->(*args) { Hash(args.first)[:tags] }
include Cache

NEW_STORE = Class.new(Hash) do
def fetch(key, options)
value = self[key] || self[key] = yield
[value, options]
end
end.new

def cache_store
NEW_STORE
end
end

_(WithOptions.new(1).(:show)).must_equal(%{["1", {:expires_in=>10, :tags=>nil}]})
_(WithOptions.new(2).(:show)).must_equal(%{["1", {:expires_in=>10, :tags=>nil}]})
_(WithOptions.new(2).(:show, tags: [:a, :b])).must_equal(%{["1", {:expires_in=>10, :tags=>[:a, :b]}]})
end
end