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

Added a method to get all blocks of specified types #55

Merged
merged 1 commit into from
May 19, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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