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

Except #47

Merged
merged 1 commit into from Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 2 additions & 17 deletions lib/ruy/conditions/except.rb
Expand Up @@ -7,36 +7,21 @@ module Conditions
# When a sub-rule is not given, Except will expect a context attribute is not equal to a given
# value.
class Except < CompoundCondition
attr_reader :attr, :value

# @param value Non-expected value
# @param attr Context attribute's name
# @yield a block in the context of the current rule
def initialize(value = nil, attr = nil, &block)
def initialize(&block)
super
@value = value
@attr = attr
instance_exec(&block) if block_given?
end

def call(ctx)
result = true

if @attr
result &&= ctx.resolve(@attr) != @value
end

if self.conditions.any?
result &&= !super(ctx)
end

result
!super
end

def ==(o)
o.kind_of?(Except) &&
@attr == o.attr &&
@value == o.value &&
@conditions == o.conditions
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ruy/dsl.rb
Expand Up @@ -55,8 +55,8 @@ def cond(&block)
# Adds an Except condition.
#
# @param (see Conditions::Except#initialize)
def except(value = nil, attr = nil, &block)
self.conditions << Conditions::Except.new(value, attr, &block)
def except(&block)
self.conditions << Conditions::Except.new(&block)
end

# Adds a GreaterThan condition.
Expand Down
66 changes: 14 additions & 52 deletions spec/lib/ruy/conditions/except_spec.rb
Expand Up @@ -4,84 +4,46 @@

describe '#call' do

subject(:condition) { Ruy::Conditions::Except.new(false, :enabled) }
subject(:condition) do
Ruy::Conditions::Except.new { assert :sunday }
end

it 'is true when enabled != false' do
context = Ruy::Context.new({:enabled => true})
it 'is true when !sunday' do
context = Ruy::Context.new({:sunday => false})

expect(condition.call(context)).to be
end

it 'is false when enabled = false' do
context = Ruy::Context.new({:enabled => false})
it 'is false when sunday' do
context = Ruy::Context.new({:sunday => true})

expect(condition.call(context)).to_not be
end

context 'when nested conditions' do
subject(:condition) do
Ruy::Conditions::Except.new(false, :enabled) do
assert :success
end
end

it 'is true when !success' do
context = Ruy::Context.new({:enabled => true, :success => false})

expect(condition.call(context)).to be
end

it 'is false when success' do
context = Ruy::Context.new({:enabled => true, :success => true})

expect(condition.call(context)).to_not be
end

context 'when no attribute condition' do
subject(:condition) do
Ruy::Conditions::Except.new(nil, nil) do
assert :success
end
end

it 'is true when !success' do
context = Ruy::Context.new({:enabled => true, :success => false})

expect(condition.call(context)).to be
end

it 'is false when success' do
context = Ruy::Context.new({:enabled => true, :success => true})

expect(condition.call(context)).to_not be
end
end
end
end

describe '#==' do
subject(:condition) { Ruy::Conditions::Except.new(false, :enabled) }
subject(:condition) { Ruy::Conditions::Except.new { assert :sunday } }

context 'when comparing against self' do
let(:other) { condition }

it { should eq(other) }
end

context 'when same condition values' do
let(:other) { Ruy::Conditions::Except.new(false, :enabled) }
context 'when same nested condition' do
let(:other) { Ruy::Conditions::Except.new { assert :sunday } }

it { should eq(other) }
end

context 'when different rule' do
let(:other) { Ruy::Conditions::All.new }
context 'when different nested condition' do
let(:other) { Ruy::Conditions::Except.new { assert :monday } }

it { should_not eq(other) }
end

context 'when different values' do
let(:other) { Ruy::Conditions::Except.new(true, :disabled) }
context 'when different rule' do
let(:other) { Ruy::Conditions::All.new }

it { should_not eq(other) }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/ruy/dsl_spec.rb
Expand Up @@ -79,7 +79,7 @@ def conditions

describe '#except' do
it 'adds an Except condition' do
host.except(false, :enabled)
host.except

expect(host.conditions).to include(be_a(Ruy::Conditions::Except))
end
Expand Down