Class: Saper::Namespace
- Inherits:
-
Object
- Object
- Saper::Namespace
- Defined in:
- lib/saper/core/namespace.rb
Overview
Namespace is a parsing / storage utility for recipes that were meant to work as a group. It must be initiated for some actions to work properly (e.g. run_chain).
Class Method Summary collapse
-
.parse(string = nil, &block) ⇒ Saper::Namespace
Parses namespace specified as a string or a block.
-
.unserialize(data, &block) ⇒ Saper::Namespace
Returns a new instance of Saper::Namespace.
Instance Method Summary collapse
-
#[](id) ⇒ Saper::Recipe
Returns recipe with specified ID.
-
#[]=(id, recipe) ⇒ Saper::Recipe
Caches recipe instance.
-
#find(id) ⇒ Saper::Recipe
Searches for and returns a recipe.
-
#initialize ⇒ Saper::Namespace
constructor
Returns a new Namespace instance.
-
#run(name = nil, input = nil, options = {}) ⇒ Saper::Runtime
Runs an recipe and returns resulting Saper:Runtime.
-
#serialize ⇒ Hash
Returns a serialized representation of this Namespace.
-
#size ⇒ Integer
Returns the number of recipes within this namespace.
-
#to_json(*args) ⇒ String
Returns a JSON representation of this recipe.
-
#try(id) ⇒ Saper::Recipe?
Returns recipe with specified ID or fails silently if not found.
Constructor Details
#initialize ⇒ Saper::Namespace
Returns a new Namespace instance
60 61 62 63 64 65 |
# File 'lib/saper/core/namespace.rb', line 60 def initialize @recipes = {} if block_given? yield self end end |
Class Method Details
.parse(string = nil, &block) ⇒ Saper::Namespace
Parses namespace specified as a string or a block.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/saper/core/namespace.rb', line 27 def self.parse(string = nil, &block) instance = DSL.new if string.is_a?(String) instance.instance_eval(string) end if block_given? instance.instance_eval(&block) end instance.namespace end |
.unserialize(data, &block) ⇒ Saper::Namespace
Returns a new instance of Saper::Namespace
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/saper/core/namespace.rb', line 41 def self.unserialize(data, &block) unless data.is_a?(Hash) raise(InvalidNamespace, data) end unless data[:recipes].is_a?(Array) raise(InvalidNamespace, data) end new do |namespace| data[:recipes].each do |recipe| namespace << Recipe.unserialize(recipe, namespace) end if block_given? yield namespace end end end |
Instance Method Details
#[](id) ⇒ Saper::Recipe
Returns recipe with specified ID.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/saper/core/namespace.rb', line 90 def [](id) id = id.to_s if @recipes.key?(id) value = @recipes[id] else value = find(id) end if value.is_a?(Recipe) return value end if value.respond_to?(:to_recipe) return @recipes[id] = value.to_recipe end raise RecipeNotFound, id end |
#[]=(id, recipe) ⇒ Saper::Recipe
Caches recipe instance.
110 111 112 113 114 115 116 117 |
# File 'lib/saper/core/namespace.rb', line 110 def []=(id, recipe) unless recipe.is_a?(Saper::Recipe) unless recipe.respond_to?(:to_recipe) raise ActionExpected end end @recipes[id.to_s] = recipe end |
#find(id) ⇒ Saper::Recipe
Searches for and returns a recipe.
121 122 123 |
# File 'lib/saper/core/namespace.rb', line 121 def find(id) raise RecipeNotFound, id end |
#run(name = nil, input = nil, options = {}) ⇒ Saper::Runtime
Runs an recipe and returns resulting Saper:Runtime.
72 73 74 |
# File 'lib/saper/core/namespace.rb', line 72 def run(name = nil, input = nil, ={}) self[name].run(input, ) end |
#serialize ⇒ Hash
Returns a serialized representation of this Namespace.
133 134 135 |
# File 'lib/saper/core/namespace.rb', line 133 def serialize { :recipes => @recipes.values.map(&:serialize) } end |
#size ⇒ Integer
Returns the number of recipes within this namespace.
127 128 129 |
# File 'lib/saper/core/namespace.rb', line 127 def size @recipes.size end |
#to_json(*args) ⇒ String
Returns a JSON representation of this recipe.
139 140 141 |
# File 'lib/saper/core/namespace.rb', line 139 def to_json(*args) serialize.to_json(*args) end |
#try(id) ⇒ Saper::Recipe?
Returns recipe with specified ID or fails silently if not found.
79 80 81 82 83 84 85 |
# File 'lib/saper/core/namespace.rb', line 79 def try(id) begin self[id] rescue nil end end |