Class: Beret::BlockSet

Inherits:
Object
  • Object
show all
Defined in:
lib/beret/block_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(block_json) ⇒ BlockSet

Returns a new instance of BlockSet.



4
5
6
# File 'lib/beret/block_set.rb', line 4

def initialize(block_json)
  @block_json = block_json
end

Instance Method Details

#blocksObject



29
30
31
32
33
# File 'lib/beret/block_set.rb', line 29

def blocks
  @blocks ||= block_json.map do |block|
    Block.new(block)
  end
end

#find(field_name, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/beret/block_set.rb', line 8

def find(field_name, options={})
  results = Search.new(blocks, field_name, options).results

  if block_given?
    results.each do |result|
      yield result.value, result.block
    end
  end

  results
end

#to_jsonObject



35
36
37
# File 'lib/beret/block_set.rb', line 35

def to_json
  blocks.map(&:to_json)
end

#update(field_name, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/beret/block_set.rb', line 20

def update(field_name, options={})
  results = find(field_name, options)

  results.each do |result|
    new_value = yield result.value, result.block
    result.block.update_attribute(field_name, new_value)
  end
end