Skip to content

Commit

Permalink
Merge pull request #512 from trailblazer/trailblazer-option
Browse files Browse the repository at this point in the history
Declarative option is replaced by trailblazer option
  • Loading branch information
AlfonsoUceda authored Dec 2, 2024
2 parents ba33a96 + 88b42f8 commit 4fc33c6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cells.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.5'

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 "declarative-option", "< 0.2.0"
spec.add_dependency 'uber', '< 0.2.0'

spec.add_development_dependency 'capybara'
Expand Down
1 change: 1 addition & 0 deletions lib/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(prefixes, view)
end # Error
end

require "cell/option"
require "cell/caching"
require "cell/prefixes"
require "cell/layout"
Expand Down
14 changes: 6 additions & 8 deletions lib/cell/caching.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "declarative/options"

module Cell
module Caching
def self.included(includer)
Expand All @@ -20,9 +18,9 @@ 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)
conditional_procs[state] = Cell::Option(options.delete(:if) || true)
version_procs[state] = Cell::Option(args.first || block)
cache_options[state] = Cell::Options(options)
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"

module Cell
# Extend `Trailblazer::Option` to make static values as callables too.
class Option < ::Trailblazer::Option
def self.build(value)
callable = case value
when Proc, Symbol, Uber::Callable
value
else
->(*) { value } # Make non-callable value to callable.
end

super(callable)
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

0 comments on commit 4fc33c6

Please sign in to comment.