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

Parameters:

  • id (String) (defaults to: nil)


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.

Parameters:

  • id (Symbol) (defaults to: nil)

    recipe ID

Returns:



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

Parameters:

  • data (Hash)

Returns:



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(InvalidRecipe, data)
  end
  new(data[:id], :name => data[:name], :namespace => namespace) 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.

Parameters:

Returns:

  • (self)


76
77
78
79
80
81
82
83
# File 'lib/saper/core/recipe.rb', line 76

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

#empty?Boolean

Returns ‘true` if recipe contains no actions.

Returns:

  • (Boolean)


105
106
107
# File 'lib/saper/core/recipe.rb', line 105

def empty?
  actions.empty?
end

#input_requiredArray<Symbol>

Returns a list of data types required as input.

Returns:

  • (Array<Symbol>)


63
64
65
# File 'lib/saper/core/recipe.rb', line 63

def input_required
  empty? ? [] : actions.first.requires
end

#input_required?Boolean

Returns ‘true` if recipe requires some input.

Returns:

  • (Boolean)


69
70
71
# File 'lib/saper/core/recipe.rb', line 69

def input_required?
  !input_required.empty?
end

#multiple?Boolean

Returns ‘true` if recipe produces multiple results.

Returns:

  • (Boolean)


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

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

#nameString?

Returns recipe name.

Returns:

  • (String, nil)


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

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

#namespaceNamespace

Returns Saper::Namespace instance.

Returns:



57
58
59
# File 'lib/saper/core/recipe.rb', line 57

def namespace
  @options[:namespace].is_a?(Namespace) ? @options[:namespace] : nil
end

#push(*actions) ⇒ self

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

Parameters:

Returns:

  • (self)


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

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

#run(input = nil, options = {}) ⇒ Saper::Runtime

Runs recipe and returns Runtime instance.

Parameters:

  • input (object) (defaults to: nil)

    input for the first action

  • options (Hash) (defaults to: {})

    options for Runtime object

Returns:



99
100
101
# File 'lib/saper/core/recipe.rb', line 99

def run(input = nil, options = {})
  Runtime.new(actions, input, { :namespace => namespace }.merge(options))
end

#serializeHash

Returns a serialized representation of this Recipe.

Returns:

  • (Hash)


117
118
119
# File 'lib/saper/core/recipe.rb', line 117

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

#to_json(*args) ⇒ String

Returns a JSON representation of this recipe.

Returns:

  • (String)


129
130
131
# File 'lib/saper/core/recipe.rb', line 129

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

#to_stringString

Returns a string representation of this recipe.

Returns:

  • (String)


123
124
125
# File 'lib/saper/core/recipe.rb', line 123

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