Class: Aquel::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/aquel/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
# File 'lib/aquel/context.rb', line 5

def initialize(block)
  @block = block
  @find_by_block = {}
  @header = false
end

Instance Attribute Details

#document_blockObject (readonly)

Returns the value of attribute document_block.



3
4
5
# File 'lib/aquel/context.rb', line 3

def document_block
  @document_block
end

#find_by_blockObject (readonly)

Returns the value of attribute find_by_block.



3
4
5
# File 'lib/aquel/context.rb', line 3

def find_by_block
  @find_by_block
end

#item_blockObject (readonly)

Returns the value of attribute item_block.



3
4
5
# File 'lib/aquel/context.rb', line 3

def item_block
  @item_block
end

#items_blockObject (readonly)

Returns the value of attribute items_block.



3
4
5
# File 'lib/aquel/context.rb', line 3

def items_block
  @items_block
end

#split_blockObject (readonly)

Returns the value of attribute split_block.



3
4
5
# File 'lib/aquel/context.rb', line 3

def split_block
  @split_block
end

Instance Method Details

#document(&block) ⇒ Object



52
53
54
# File 'lib/aquel/context.rb', line 52

def document(&block)
  @document_block = block
end

#execute(attributes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aquel/context.rb', line 11

def execute(attributes)
  instance_eval(&@block)

  items = []
  document = document_block.call(attributes.equal_values)

  if items_block
    items_block.call(document).each do |item|
      value = split_block.call(item)
      items << (value.kind_of?(Array) ? value : [value])
    end
  elsif find_by_block.size > 0
    find_by_block.each do |k, v|
      v.call(attributes.equal_values[k], document).each do |item|
        value = split_block.call(item)
        items << (value.kind_of?(Array) ? value : [value])
      end
    end
  else
    while item = item_block.call(document)
      items << split_block.call(item)
    end
  end

  if @header
    header = items.shift.map(&:downcase)
  else
    header = (1..(items.first.size)).to_a
  end

  items.map do |itm|
    item = {}
    itm.each_with_index { |v, i| item[header[i]] = v }
    item
  end
ensure
  if document.respond_to?(:close)
    document.close
  end
end

#find_by(name, &block) ⇒ Object



68
69
70
# File 'lib/aquel/context.rb', line 68

def find_by(name, &block)
  @find_by_block[name] = block
end

#has_headerObject



72
73
74
# File 'lib/aquel/context.rb', line 72

def has_header
  @header = true
end

#item(&block) ⇒ Object



56
57
58
# File 'lib/aquel/context.rb', line 56

def item(&block)
  @item_block = block
end

#items(&block) ⇒ Object



64
65
66
# File 'lib/aquel/context.rb', line 64

def items(&block)
  @items_block = block
end

#split(&block) ⇒ Object



60
61
62
# File 'lib/aquel/context.rb', line 60

def split(&block)
  @split_block = block
end