Skip to content

Commit

Permalink
Merge 3065bb4 into 834e60c
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshiori committed Apr 22, 2013
2 parents 834e60c + 3065bb4 commit 517d410
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 29 deletions.
35 changes: 26 additions & 9 deletions lib/chanko/active_if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,43 @@ def active?(context, options = {})
def blocks
@blocks ||= begin
conditions.map do |condition|
condition.is_a?(Any) ? condition.to_block : self.class.find(condition)
Block.new(condition)
end << @block
end.compact
end

class Any
def initialize(*labels)
@labels = labels
class Block
def initialize(*conditions)
@conditions = conditions
end

def to_block
def call(context, options)
block.call(context, options)
end

def block
condition = @conditions.first
condition.is_a?(Block) ? condition : ActiveIf.find(condition)
end
end

class Any < Block
def block
proc do |context, options|
definitions.any? do |definition|
definition.call(context, options)
@conditions.any? do |condition|
Block.new(condition).call(context, options)
end
end
end
end

def definitions
@labels.map {|label| ActiveIf.find(label) }
class None < Block
def block
proc do |context, options|
@conditions.none? do |condition|
Block.new(condition).call(context, options)
end
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/chanko/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def any(*labels)
ActiveIf::Any.new(*labels)
end

def none(*labels)
ActiveIf::None.new(*labels)
end

def raise_error
@raise_error = true
end
Expand Down
104 changes: 84 additions & 20 deletions spec/chanko/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Chanko
Class.new { include Chanko::Helper }.new
end

describe ".active_if" do
describe "about active_if" do
before do
ActiveIf.define(:true) { true }
ActiveIf.define(:false) { false }
Expand All @@ -26,38 +26,102 @@ module Chanko

subject { unit.active?(view) }

context "in default configuration" do
it "is configured to return always true" do
should be_true
describe ".active_if" do
context "in default configuration" do
it "is configured to return always true" do
should be_true
end
end

context "when labels are specified" do
before do
unit.active_if(:true, :false)
end
specify "all of defined conditions must pass" do
should be_false
end
end

context "when block is passed" do
before do
unit.active_if(:true) { false }
end
specify "all of defined conditions and block must pass" do
should be_false
end
end
end

context "when labels are specified" do
before do
unit.active_if(:true, :false)
describe ".any" do
context "when conditions returned true and false" do
before do
unit.instance_eval do
active_if any(:true, :false)
end
end
it { should be_true }
end
specify "all of defined conditions must pass" do
should be_false

context "when all conditions returned false" do
before do
unit.instance_eval do
active_if any(:false, :false)
end
end
it { should be_false }
end
end

context "when block is passed" do
before do
unit.active_if(:true) { false }
describe ".none" do
context "when all conditions returned false" do
before do
unit.instance_eval do
active_if none(:false, :false)
end
end
it { should be_true }
end
specify "all of defined conditions and block must pass" do
should be_false

context "when conditions returned true and false" do
before do
unit.instance_eval do
active_if none(:true, :false)
end
end
it { should be_false }
end
end

context "when any is specified" do
before do
unit.instance_eval do
active_if any(:true, :false)
context "when using 'any' and 'none'" do
context "'any' in 'none'" do
before do
unit.instance_eval do
active_if none(any(:true, :false))
end
end
it "returns negatived result of inner-expression" do
should be_false
end
end

context "'none' in 'any'" do
before do
unit.instance_eval do
active_if any(none(:true), :false)
end
end
it "uses none(:true) as false" do
should be_false
end
end
specify "any of conditions must pass" do
should be_true

context "more nested" do
before do
unit.instance_eval do
active_if any(any(none(:false), :false), :false)
end
end
it { should be_true }
end
end
end
Expand Down

0 comments on commit 517d410

Please sign in to comment.