Class: ImageVise::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/image_vise/pipeline.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operators = []) ⇒ Pipeline

Returns a new instance of Pipeline.



18
19
20
# File 'lib/image_vise/pipeline.rb', line 18

def initialize(operators = [])
  @ops = operators.to_a
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *a, &blk) ⇒ Object



30
31
32
33
# File 'lib/image_vise/pipeline.rb', line 30

def method_missing(method_name, *a, &blk)
  operator_builder = ImageVise.operator_from(method_name)
  self << operator_builder.new(*a)
end

Class Method Details

.from_param(array_of_operator_names_to_operator_params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/image_vise/pipeline.rb', line 6

def self.from_param(array_of_operator_names_to_operator_params)
  operators = array_of_operator_names_to_operator_params.map do |(operator_name, operator_params)|
    operator_class = operator_by_name(operator_name)
    if operator_params.any? && operator_class.method(:new).arity.nonzero?
      operator_class.new(**operator_params)
    else
      operator_class.new
    end
  end
  new(operators)
end

.operator_by_name(name) ⇒ Object



2
3
4
# File 'lib/image_vise/pipeline.rb', line 2

def self.operator_by_name(name)
  operator = ImageVise.operator_from(name)  or raise "Unknown operator #{name}"
end

Instance Method Details

#<<(image_operator) ⇒ Object



22
23
24
# File 'lib/image_vise/pipeline.rb', line 22

def <<(image_operator)
  @ops << image_operator; self
end

#apply!(magick_image) ⇒ Object



47
48
49
# File 'lib/image_vise/pipeline.rb', line 47

def apply!(magick_image)
  @ops.each{|e| e.apply!(magick_image) }
end

#each(&b) ⇒ Object



51
52
53
# File 'lib/image_vise/pipeline.rb', line 51

def each(&b)
  @ops.each(&b)
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/image_vise/pipeline.rb', line 26

def empty?
  @ops.empty?
end

#respond_to_missing?(method_name, *a) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/image_vise/pipeline.rb', line 35

def respond_to_missing?(method_name, *a)
  ImageVise.defined_operators.include?(method_name.to_s)
end

#to_paramsObject



39
40
41
42
43
44
45
# File 'lib/image_vise/pipeline.rb', line 39

def to_params
  @ops.map do |operator|
    operator_name = ImageVise.operator_name_for(operator)
    operator_params = operator.respond_to?(:to_h) ? operator.to_h : {}
    [operator_name, operator_params]
  end
end