Class: Saper::Recipe
- Inherits:
-
Object
- Object
- Saper::Recipe
- Defined in:
- lib/saper/core/recipe.rb
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns list of recipe actions.
-
#id ⇒ Object
readonly
Returns unique ID of the recipe.
Class Method Summary collapse
-
.parse(id = nil, name = nil, &block) ⇒ Saper::Recipe
Parses block and returns a Recipe instance.
-
.unserialize(data, namespace = nil, &block) ⇒ Saper::Recipe
Returns a new instance of Saper::Recipe.
Instance Method Summary collapse
-
#<<(action) ⇒ self
Adds a new action to the end of the recipe and returns self.
-
#empty? ⇒ Boolean
Returns ‘true` if recipe contains no actions.
-
#initialize(id = nil, options = {}) ⇒ Saper::Recipe
constructor
Returns a new instance of Saper::Recipe.
-
#input_required ⇒ Array<Symbol>
Returns a list of data types required as input.
-
#input_required? ⇒ Boolean
Returns ‘true` if recipe requires some input.
-
#multiple? ⇒ Boolean
Returns ‘true` if recipe produces multiple results.
-
#name ⇒ String?
Returns recipe name.
-
#namespace ⇒ Namespace
Returns Saper::Namespace instance.
-
#push(*actions) ⇒ self
Adds new actions to the end of the recipe and returns self.
-
#run(input = nil, options = {}) ⇒ Saper::Runtime
Runs recipe and returns Runtime instance.
-
#serialize ⇒ Hash
Returns a serialized representation of this Recipe.
-
#to_json(*args) ⇒ String
Returns a JSON representation of this recipe.
-
#to_string ⇒ String
Returns a string representation of this recipe.
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, = {}) @id = id || SecureRandom.hex @actions = [] @options = if block_given? yield self end end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns list of recipe actions.
35 36 37 |
# File 'lib/saper/core/recipe.rb', line 35 def actions @actions end |
#id ⇒ Object (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(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.
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.
105 106 107 |
# File 'lib/saper/core/recipe.rb', line 105 def empty? actions.empty? end |
#input_required ⇒ Array<Symbol>
Returns a list of data types required as input.
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.
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.
111 112 113 |
# File 'lib/saper/core/recipe.rb', line 111 def multiple? empty? ? false : actions.any?(&:multiple?) end |
#name ⇒ String?
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 |
#namespace ⇒ Namespace
Returns Saper::Namespace instance.
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.
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.
99 100 101 |
# File 'lib/saper/core/recipe.rb', line 99 def run(input = nil, = {}) Runtime.new(actions, input, { :namespace => namespace }.merge()) end |
#serialize ⇒ Hash
Returns a serialized representation of this Recipe.
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.
129 130 131 |
# File 'lib/saper/core/recipe.rb', line 129 def to_json(*args) serialize.to_json(*args) end |
#to_string ⇒ String
Returns a string representation of this recipe.
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 |