Skip to content

Commit

Permalink
Merge pull request #55 from madebymany/get-blocks-of-type
Browse files Browse the repository at this point in the history
Added a method to get all blocks of specified types
  • Loading branch information
Andrew Walker authored May 19, 2017
2 parents 3a52ef1 + 551738e commit 8e19c00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/sir_trevor_rails/block_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ def first_block_of_type(type)
klass = Block.block_class(type)
detect { |b| b.is_a? klass }
end

def get_blocks_of_types(types)
types = [types] unless types.is_a? Array
select { |b| types.include?(b.type) }
end
end
end
21 changes: 20 additions & 1 deletion spec/unit/block_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class BlockArraySpec < ActiveSupport::TestCase
describe BlockArray do
describe 'intialization' do
describe 'initialization' do
subject { BlockArray }

it 'can be initialized with an empty JSON array' do
Expand Down Expand Up @@ -65,6 +65,25 @@ class BlockArraySpec < ActiveSupport::TestCase
end
end

describe 'get_blocks_of_types' do
it 'returns all blocks of given types in the array' do
text_blocks = subject.get_blocks_of_types(:text)
text_blocks.each do |b|
assert { b.type == :text }
end
end

it 'returns correct number of blocks of given types in the array' do
text_blocks = subject.get_blocks_of_types([:text, :list])
assert { text_blocks.size == 2 }
end

it 'returns empty array if called with block type not in the array' do
nonexistent_blocks = subject.get_blocks_of_types(:nonexistent)
assert { nonexistent_blocks.size == 0 }
end
end

describe 'to_a' do
it 'returns array with same number of elements' do
assert { subject.to_a.class == Array }
Expand Down

0 comments on commit 8e19c00

Please sign in to comment.