-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ extracted source setup into a separate helper module - trying to fr…
…ee the category from unnecessary methods
- Loading branch information
Showing
11 changed files
with
110 additions
and
122 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 was deleted.
Oops, something went wrong.
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,30 @@ | ||
module Picky | ||
|
||
module Generators | ||
|
||
module Source | ||
|
||
# Either a thing responding to #each or a block is fine. | ||
# | ||
def self.from thing, nil_ok, index_name = nil, category_name = nil | ||
if thing.respond_to?(:each) || thing.respond_to?(:call) | ||
thing | ||
else | ||
return if nil_ok | ||
|
||
specifics = "" | ||
specifics << index_name.to_s if index_name | ||
specifics = "for #{specifics} " unless specifics.empty? | ||
|
||
raise ArgumentError.new(<<-ERROR) | ||
The source #{specifics}should respond to either the method #each or | ||
it can be a lambda/block, returning such a source. | ||
ERROR | ||
end | ||
end | ||
|
||
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
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 was deleted.
Oops, something went wrong.
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,64 @@ | ||
require 'spec_helper' | ||
|
||
describe Picky::Generators::Source do | ||
let(:generator) { described_class } # "class", actually a module. | ||
context 'unblock_source' do | ||
context 'with block' do | ||
it 'unblocks' do | ||
result = generator.from ->() { :some_source }, false | ||
|
||
result.call == :some_source | ||
end | ||
end | ||
context 'with #each' do | ||
it 'takes the source directly' do | ||
generator.from([:some_source], true) == :some_source | ||
end | ||
it 'takes the source directly' do | ||
generator.from([:some_source], false) == :some_source | ||
end | ||
end | ||
end | ||
context 'extract_source' do | ||
context 'block with source hash' do | ||
it 'extracts a source' do | ||
generator.from(->(){}, false).should be_kind_of(Proc) | ||
end | ||
end | ||
context 'each source' do | ||
let(:source) do | ||
Class.new do | ||
def each | ||
|
||
end | ||
end.new | ||
end | ||
it 'extracts a source' do | ||
generator.from(source, false).should == source | ||
end | ||
end | ||
context 'invalid source with nil not ok' do | ||
it 'raises with a nice error message' do | ||
expect { | ||
generator.from Object.new, false | ||
}.to raise_error(<<-ERROR) | ||
The source should respond to either the method #each or | ||
it can be a lambda/block, returning such a source. | ||
ERROR | ||
end | ||
it 'raises with a nice error message' do | ||
expect { | ||
generator.from Object.new, false, 'some_index' | ||
}.to raise_error(<<-ERROR) | ||
The source for some_index should respond to either the method #each or | ||
it can be a lambda/block, returning such a source. | ||
ERROR | ||
end | ||
end | ||
context 'with nil ok' do | ||
it 'simply returns nil back' do | ||
generator.from(nil, true).should == nil | ||
end | ||
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