-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Granite::Util to granite & use it in BasePrecondition
- Loading branch information
Showing
6 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Granite | ||
module Util | ||
extend ActiveSupport::Concern | ||
|
||
# Evaluates value and returns result based on what was passed: | ||
# - if Proc was passed, then executes it in context of self | ||
# - if Symbol was passed, then calls a method with that name and returns result | ||
# - otherwise just returns the value itself | ||
# @param value [Object] value to evaluate | ||
# @return [Object] result of evaluation | ||
def evaluate(value) | ||
case value | ||
when Proc | ||
evaluate_proc(value) | ||
when Symbol | ||
evaluate_symbol(value) | ||
else | ||
value | ||
end | ||
end | ||
|
||
# Evaluates `if` or `unless` conditions present in the supplied | ||
# `options` being it a symbol or callable. | ||
# | ||
# @param [Hash] options The method options to evaluate. | ||
# @option options :if method name or callable | ||
# @option options :unless method name or callable | ||
# @return [Boolean] whether conditions are satisfied | ||
def conditions_satisfied?(options) | ||
fail ArgumentError, 'You cannot specify both if and unless' if options.key?(:if) && options.key?(:unless) | ||
|
||
if options.key?(:if) | ||
evaluate(options[:if]) | ||
elsif options.key?(:unless) | ||
!evaluate(options[:unless]) | ||
else | ||
true | ||
end | ||
end | ||
|
||
private | ||
|
||
def evaluate_proc(value) | ||
instance_exec(&value) | ||
end | ||
|
||
def evaluate_symbol(value) | ||
__send__(value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Granite | ||
VERSION = '0.9.7'.freeze | ||
VERSION = '0.9.8'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
RSpec.describe Granite::Util do | ||
subject(:action) { DummyAction.new('John') } | ||
|
||
before do | ||
stub_class(:DummyAction, Object) do | ||
include Granite::Util | ||
|
||
attr_accessor :name | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
end | ||
end | ||
|
||
describe '#evaluate' do | ||
subject { action.evaluate(target) } | ||
let(:target) { 'Peter' } | ||
|
||
it { is_expected.to eq('Peter') } | ||
|
||
context 'when symbol is passed' do | ||
let(:target) { :name } | ||
|
||
it { is_expected.to eq('John') } | ||
end | ||
|
||
context 'when lambda is passed' do | ||
let(:target) { -> { name } } | ||
|
||
it { is_expected.to eq('John') } | ||
end | ||
end | ||
|
||
describe '#conditions_satisfied?' do | ||
subject { action.conditions_satisfied?(**conditions) } | ||
let(:conditions) { {if: -> { name == 'John' }} } | ||
|
||
it { is_expected.to be_truthy } | ||
|
||
context 'when if condition is satisfied' do | ||
before { action.name = 'Peter' } | ||
|
||
it { is_expected.to be_falsey } | ||
end | ||
|
||
context 'when unless condition is passed' do | ||
let(:conditions) { {unless: :name} } | ||
|
||
it { is_expected.to be_falsey } | ||
end | ||
|
||
context 'when no condition is passed' do | ||
let(:conditions) { {} } | ||
|
||
it { is_expected.to be_truthy } | ||
end | ||
|
||
context 'when both if & unless are passed' do | ||
let(:conditions) { {if: :name, unless: :name} } | ||
|
||
it { expect { subject }.to raise_error(ArgumentError) } | ||
end | ||
end | ||
end |