Class: SirTrevorRails::BlockArray
- Inherits:
-
Array
- Object
- Array
- SirTrevorRails::BlockArray
- Defined in:
- lib/sir_trevor_rails/block_array.rb
Class Method Summary collapse
Instance Method Summary collapse
- #first_block_of_type(type) ⇒ Object
- #get_blocks_of_types(types) ⇒ Object
- #has_block_of_type?(type) ⇒ Boolean
- #to_s ⇒ Object
Class Method Details
.from_json(blocks, parent = nil) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sir_trevor_rails/block_array.rb', line 4 def self.from_json(blocks, parent = nil) if blocks.is_a?(String) blocks = MultiJson.load(blocks, symbolize_keys: true) end if blocks.is_a?(Hash) blocks = blocks[:data] || blocks['data'] or raise IndexError, "No block data found" end new blocks.map { |obj| SirTrevorRails::Block.from_hash(obj.deep_symbolize_keys, parent) } end |
Instance Method Details
#first_block_of_type(type) ⇒ Object
28 29 30 31 |
# File 'lib/sir_trevor_rails/block_array.rb', line 28 def first_block_of_type(type) klass = Block.block_class(type) detect { |b| b.is_a? klass } end |
#get_blocks_of_types(types) ⇒ Object
33 34 35 36 |
# File 'lib/sir_trevor_rails/block_array.rb', line 33 def get_blocks_of_types(types) types = [types] unless types.is_a? Array select { |b| types.include?(b.type) } end |
#has_block_of_type?(type) ⇒ Boolean
23 24 25 26 |
# File 'lib/sir_trevor_rails/block_array.rb', line 23 def has_block_of_type?(type) klass = Block.block_class(type) any? { |b| b.is_a? klass } end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/sir_trevor_rails/block_array.rb', line 19 def to_s {data: as_json}.to_json end |