Class: ParameterCapture

Inherits:
Object
  • Object
show all
Defined in:
lib/data_frame/parameter_capture.rb

Overview

Captures the intent of a model definition in a block. Usage: pc = ParameterCapture.new do |p|

p.whatever :some_value
p.another :one
p.or_list [1, 2]
p.or_range (1..2)

end pc.parameters

> => :some_value, :another => :one, :or_list => [1,2], :or_range => (1..2)

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ParameterCapture

Returns a new instance of ParameterCapture.



11
12
13
# File 'lib/data_frame/parameter_capture.rb', line 11

def initialize(&block)
  self.instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *values, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/data_frame/parameter_capture.rb', line 41

def method_missing(key, *values, &block)
  if self.parameters.table.keys.include?(key)
    self.parameters.send(key)
  elsif values.size == 1
    self.parameters.table[key] = values.first
  else
    self.parameters.table[key] = values
  end
end

Instance Method Details

#filter(row) ⇒ Object

can be used in a data_frame filter. @pc.filter(row) Using a Hash as a cast type for the filter.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/data_frame/parameter_capture.rb', line 26

def filter(row)
  self.keys.each do |key|
    value = self.parameters.send(key)
    case value
    when Array
      return false unless value.include?(row[key])
    when Range
      return false unless value.include?(row[key])
    else
      return false unless value === row[key]
    end
  end
  return true
end

#keysObject

Exposes the set keys



20
21
22
# File 'lib/data_frame/parameter_capture.rb', line 20

def keys
  self.parameters.table.keys
end

#parametersObject



15
16
17
# File 'lib/data_frame/parameter_capture.rb', line 15

def parameters
  @parameters ||= OpenStruct.new
end