Skip to content

Commit

Permalink
use Declarative::Option and :Builder instead of uber's.
Browse files Browse the repository at this point in the history
this allows removing the uber version restriction.
  • Loading branch information
apotonick committed Jan 28, 2017
1 parent 6d54b99 commit 682cee4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -6,3 +6,5 @@ gem "benchmark-ips"
gem "minitest-line"

gemspec

gem "uber", path: "../uber"
6 changes: 4 additions & 2 deletions cells.gemspec
Expand Up @@ -11,15 +11,17 @@ Gem::Specification.new do |spec|
spec.email = ["apotonick@gmail.com"]
spec.homepage = "https://github.com/apotonick/cells"
spec.summary = %q{View Models for Ruby and Rails.}
spec.description = %q{Cells replaces partials and helpers with OOP view models, giving you proper encapsulation, inheritance, testability and a cleaner view architecture.}
spec.description = %q{View Models for Ruby and Rails, replacing helpers and partials while giving you a clean view architecture with proper encapsulation.}
spec.license = "MIT"

spec.files = `git ls-files`.split("\n")
spec.test_files = `git ls-files -- {test}/*`.split("\n")
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
spec.require_paths = ["lib"]

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

spec.add_development_dependency "rake"
Expand Down
1 change: 0 additions & 1 deletion lib/cell.rb
@@ -1,6 +1,5 @@
require "tilt"
require "uber/inheritable_attr"
require "uber/delegates"
require "cell/version"

module Cell
Expand Down
6 changes: 3 additions & 3 deletions lib/cell/builder.rb
@@ -1,15 +1,15 @@
require "uber/builder"
require "declarative/builder"

module Cell
module Builder
def self.included(base)
base.send :include, Uber::Builder
base.send :include, Declarative::Builder
base.extend ClassMethods
end

module ClassMethods
def build(*args)
build!(self, *args).new(*args) # Uber::Builder#build!.
build!(self, *args).new(*args) # Declarative::Builder#build!.
end
end
end
Expand Down
29 changes: 13 additions & 16 deletions lib/cell/caching.rb
@@ -1,4 +1,4 @@
require 'uber/options'
require "declarative/options"

module Cell
module Caching
Expand All @@ -10,19 +10,19 @@ def self.included(includer)
inheritable_attr :conditional_procs
inheritable_attr :cache_options

self.version_procs = {}
self.version_procs = {}
self.conditional_procs = {}
self.cache_options = Uber::Options.new({})
self.cache_options = {}
end
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 brillant tool.
options = args.last.is_a?(Hash) ? args.pop : {} # I have to admit, Array#extract_options is a brilliant tool.

self.conditional_procs[state] = Uber::Options::Value.new(options.delete(:if) || true)
self.version_procs[state] = Uber::Options::Value.new(args.first || block)
self.cache_options[state] = Uber::Options.new(options)
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)
end

# Computes the complete, namespaced cache key for +state+.
Expand All @@ -37,17 +37,16 @@ def expire_cache_key_for(key, cache_store, *args)
private

def expand_cache_key(key)
key.join("/") # TODO: test me!
key.join("/")
end
end


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].evaluate(self, *args))
options = self.class.cache_options.eval(state, self, *args)
key = self.class.state_cache_key(state, self.class.version_procs[state].(self, *args))
options = self.class.cache_options[state].(self, *args)

fetch_from_cache_for(key, options) { super(state, *args) }
end
Expand All @@ -57,7 +56,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].evaluate(self, *args)
perform_caching? and state_cached?(state) and self.class.conditional_procs[state].(self, *args)
end

private
Expand All @@ -66,10 +65,8 @@ def perform_caching?
true
end

def fetch_from_cache_for(key, options)
cache_store.fetch(key, options) do
yield
end
def fetch_from_cache_for(key, options, &block)
cache_store.fetch(key, options, &block)
end

def state_cached?(state)
Expand Down
2 changes: 2 additions & 0 deletions lib/cell/view_model.rb
@@ -1,3 +1,5 @@
require "uber/delegates"

module Cell
class ViewModel
extend Abstract
Expand Down
32 changes: 32 additions & 0 deletions test/cache_test.rb
@@ -0,0 +1,32 @@
require "test_helper"

# TODO: test caching without rails

class CacheTest < Minitest::Spec
STORE = Class.new(Hash) do
def fetch(key, options, &block)
self[key] || self[key] = yield
end
end.new

module Cache
def show
"#{@model}"
end

def cache_store
STORE
end
end

class Index < Cell::ViewModel
cache :show
include Cache
end

it do
Index.new(1).().must_equal("1")
Index.new(2).().must_equal("1")
end
end

0 comments on commit 682cee4

Please sign in to comment.