Skip to content

Commit

Permalink
Merge pull request #51 from nebulab/fix
Browse files Browse the repository at this point in the history
Some little fixes
  • Loading branch information
mtylty authored Apr 26, 2017
2 parents ce2c70b + 630e8f5 commit c0b2ce3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/pulsar/interactors/copy_initial_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call

FileUtils.cp_r(File.expand_path(initial_repo), context.directory)
rescue
context.fail! error: Pulsar::ContextError.new($!.message)
context_fail! $!.message
end
end
end
2 changes: 1 addition & 1 deletion lib/pulsar/interactors/create_capfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def application_exists?
end

def fail_on_missing_application!
context.fail! error: Pulsar::ContextError.new("The application #{context.application} does not exist in your repository")
context_fail! "The application #{context.application} does not exist in your repository"
end
end
end
2 changes: 1 addition & 1 deletion lib/pulsar/interactors/create_deploy_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def call
Rake.sh("cat #{default_deploy} >> #{context.deploy_file_path}") if File.exist?(default_deploy)
Rake.sh("cat #{app_deploy} >> #{context.deploy_file_path}") if File.exist?(app_deploy)
rescue
context.fail! error: Pulsar::ContextError.new($!.message)
context_fail!$!.message
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/pulsar/interactors/identify_repository_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def call
:remote
end
rescue
context.fail! error: Pulsar::ContextError.new($!.message)
context_fail! $!.message
end
end
end
22 changes: 8 additions & 14 deletions lib/pulsar/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ def self.included(klass)
end

def validate_context!
validable_properties.each do |property, validator|
result = if validator.is_a? Proc
validator.call(context.send(property), context)
else
context.send property.to_sym
end
validable_properties.each do |property|
result = context.send property.to_sym
context_fail! "Invalid context for #{property} [#{result}]" unless result
end
end
Expand All @@ -23,17 +19,15 @@ def context_fail!(msg)
def validable_properties
self.class.validable_properties
end
end

module ClassMethods
def validate_context_for!(*args)
args.each do |arg|
validable_properties[arg] = (block_given? ? Proc.new : nil)
module ClassMethods
def validate_context_for!(*args)
validable_properties.concat args
end
end

def validable_properties
@validable_properties ||= {}
def validable_properties
@validable_properties ||= []
end
end
end
end
50 changes: 9 additions & 41 deletions spec/units/validator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,31 @@
require 'spec_helper'

class BasicValidator
include Interactor
include Pulsar::Validator
end

class NotNilInteractor
include Interactor
include Pulsar::Validator

validate_context_for! :not_nil_property
end

class GreaterThanTenInteractor
include Interactor
include Pulsar::Validator

validate_context_for! :greater_than_ten do |prop|
prop > 10
end
end

RSpec.describe Pulsar::Validator do
let(:described_instance) { BasicValidator.new }
let(:described_instance) { NotNilInteractor.new }

it { expect(described_instance).to respond_to :validate_context! }

describe '#validate_context!' do
context 'without block' do
subject { NotNilInteractor.call initial_context }

context 'with valid property name' do
let(:initial_context) { { not_nil_property: true } }
subject { NotNilInteractor.call initial_context }

it { is_expected.to be_a_success }
it { is_expected.to eql Interactor::Context.new(not_nil_property: true) }
end
context 'with valid property name' do
let(:initial_context) { { not_nil_property: true } }

context 'with invalid property name' do
let(:initial_context) { { not_nil_property: nil } }

it { is_expected.to be_a_failure }
end
it { is_expected.to be_a_success }
it { is_expected.to eql Interactor::Context.new(not_nil_property: true) }
end

context 'with block' do
subject { GreaterThanTenInteractor.call initial_context }
context 'with valid property' do
let(:initial_context) { { greater_than_ten: 32 } }

it { is_expected.to be_a_success }
it { is_expected.to eql Interactor::Context.new(greater_than_ten: 32) }
end

context 'with invalid property' do
let(:initial_context) { { greater_than_ten: 4 } }
context 'with invalid property name' do
let(:initial_context) { { not_nil_property: nil } }

it { is_expected.to be_a_failure }
end
it { is_expected.to be_a_failure }
end
end
end

0 comments on commit c0b2ce3

Please sign in to comment.