Class: Yahm::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/yahm/mapping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Mapping

Returns a new instance of Mapping.



8
9
10
11
12
13
14
15
16
# File 'lib/yahm/mapping.rb', line 8

def initialize(options = {}, &block)
  @context = options[:context]
  @rules = []
  @result = nil
  @use_threads = options[:use_threads] || false
  @thread_pool_size = options[:thread_pool_size] || 2

  self.instance_eval(&block) if block
end

Instance Attribute Details

#thread_pool_sizeObject

Returns the value of attribute thread_pool_size.



5
6
7
# File 'lib/yahm/mapping.rb', line 5

def thread_pool_size
  @thread_pool_size
end

#use_threadsObject

Returns the value of attribute use_threads.



6
7
8
# File 'lib/yahm/mapping.rb', line 6

def use_threads
  @use_threads
end

Instance Method Details

#apply_to(input_hash, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yahm/mapping.rb', line 18

def apply_to(input_hash, options = {})
  @result = {}

  if @use_threads
    thread_pool = Thread.pool(@thread_pool_size) if @use_threads

    @rules.each do |rule|
      thread_pool.process do
        apply_rule(rule, input_hash, @result, options)
      end
    end
  
    thread_pool.shutdown
  else
    @rules.each do |rule|
      apply_rule(rule, input_hash, @result, options)
    end
  end

  @result
end