Class: Saper::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/saper/core/recipe.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, options = {}) ⇒ Saper::Recipe

Returns a new instance of Saper::Recipe


40
41
42
43
44
45
46
47
# File 'lib/saper/core/recipe.rb', line 40

def initialize(id = nil, options = {})
  @id      = id || SecureRandom.hex
  @actions = []
  @options = options
  if block_given?
    yield self
  end
end

Instance Attribute Details

#actionsObject (readonly)

Returns list of recipe actions.


35
36
37
# File 'lib/saper/core/recipe.rb', line 35

def actions
  @actions
end

#idObject (readonly)

Returns unique ID of the recipe.


32
33
34
# File 'lib/saper/core/recipe.rb', line 32

def id
  @id
end

Class Method Details

.parse(id = nil, name = nil, &block) ⇒ Saper::Recipe

Parses block and returns a Recipe instance.


27
28
29
# File 'lib/saper/core/recipe.rb', line 27

def self.parse(id = nil, name = nil, &block)
  DSL::Recipe.parse(id, name, &block)
end

.unserialize(data, namespace = nil, &block) ⇒ Saper::Recipe

Returns a new instance of Saper::Recipe


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/saper/core/recipe.rb', line 7

def self.unserialize(data, namespace = nil, &block)
  if data.is_a?(Array)
    return data.map { |item| unserialize(item, namespace) }
  end
  unless data.is_a?(Hash)
    raise(Errors::InvalidRecipe, data)
  end
  new(data[:id], :name => data[:name]) do |recipe|
    unless data[:actions].nil?
      recipe.push *Action.unserialize(data[:actions], namespace)
    end
    if block_given?
      yield recipe
    end
  end
end

Instance Method Details

#<<(action) ⇒ self

Adds a new action to the end of the recipe and returns self.


97
98
99
100
101
102
103
104
# File 'lib/saper/core/recipe.rb', line 97

def <<(action)
  if action.is_a?(Action)
    @actions.push(action)
  else
    raise Errors::ActionExpected, action
  end
  self
end

#acceptsArray<Symbol>

Sets (one) or returns (all) acceptable input types.


71
72
73
# File 'lib/saper/core/recipe.rb', line 71

def accepts
  Action::INPUT_TYPES.select { |type| accepts?(type) }
end

#accepts?(type) ⇒ Boolean

Returns `true` if recipe accepts specified type as input.


58
59
60
61
62
63
64
65
66
# File 'lib/saper/core/recipe.rb', line 58

def accepts?(type)
  actions.each do |action|
    unless action.accepts?(type)
      return false
    end
    type = action.output_for(type)
  end
  true
end

#empty?Boolean

Returns `true` if recipe contains no actions.


132
133
134
# File 'lib/saper/core/recipe.rb', line 132

def empty?
  actions.empty?
end

#multiple?Boolean

Returns `true` if recipe produces multiple results.


138
139
140
# File 'lib/saper/core/recipe.rb', line 138

def multiple?
  empty? ? false : actions.any?(&:multiple?)
end

#nameString?

Returns recipe name.


51
52
53
# File 'lib/saper/core/recipe.rb', line 51

def name
  @options[:name].nil? ? nil : @options[:name].to_s
end

#output_for(type) ⇒ Object


87
88
89
90
91
92
# File 'lib/saper/core/recipe.rb', line 87

def output_for(type)
  actions.each do |action|
    type = action.output_for(type)
  end
  type
end

#push(*actions) ⇒ self

Adds new actions to the end of the recipe and returns self.


109
110
111
112
113
114
# File 'lib/saper/core/recipe.rb', line 109

def push(*actions)
  actions.compact.each do |action|
    self << action
  end
  self
end

#requiresArray<Symbol>

Returns a list of data types required as input.


77
78
79
# File 'lib/saper/core/recipe.rb', line 77

def requires
  accepts.include?(:nothing) ? [] : accepts
end

#requires?Boolean

Returns `true` if recipe requires some input.


83
84
85
# File 'lib/saper/core/recipe.rb', line 83

def requires?
  !requires.empty?
end

#run(input = nil, runtime = nil) ⇒ Saper::Result

Runs recipe and returns Result instance.


120
121
122
123
124
125
126
127
128
# File 'lib/saper/core/recipe.rb', line 120

def run(input = nil, runtime = nil)
  unless runtime.is_a?(Runtime)
    runtime = Runtime.new(input.is_a?(Hash) ? input : {}, runtime || {})
  end
  runtime.logger.executing_recipe(self)
  stack = Stack.new(runtime, input)
  enum  = enum_for(:each_stack, stack)
  Result.new(enum)
end

#serializeHash

Returns a serialized representation of this Recipe.


144
145
146
# File 'lib/saper/core/recipe.rb', line 144

def serialize
  { :id => id, :name => name, :actions => @actions.map(&:serialize) }
end

#to_json(*args) ⇒ String

Returns a JSON representation of this recipe.


156
157
158
# File 'lib/saper/core/recipe.rb', line 156

def to_json(*args)
  serialize.to_json(*args)
end

#to_stringString

Returns a string representation of this recipe.


150
151
152
# File 'lib/saper/core/recipe.rb', line 150

def to_string
  "recipe %s do\n%s\nend" % [id.inspect, actions.map(&:to_string).join("\n")]
end