From 551738eae877965f4e3481b1d0f637c29fb9fe39 Mon Sep 17 00:00:00 2001 From: ilyaporopudas Date: Fri, 19 May 2017 14:56:29 +0100 Subject: [PATCH] Added a method to get all blocks of specified types --- lib/sir_trevor_rails/block_array.rb | 5 +++++ spec/unit/block_array_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/sir_trevor_rails/block_array.rb b/lib/sir_trevor_rails/block_array.rb index 714c3f6..5065b2b 100644 --- a/lib/sir_trevor_rails/block_array.rb +++ b/lib/sir_trevor_rails/block_array.rb @@ -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 diff --git a/spec/unit/block_array_spec.rb b/spec/unit/block_array_spec.rb index 2f15404..2c2b6f7 100644 --- a/spec/unit/block_array_spec.rb +++ b/spec/unit/block_array_spec.rb @@ -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 @@ -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 }