Class: Saper::Namespace

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeSaper::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.

Parameters:

  • string (String) (defaults to: nil)

    list of recipes

Returns:


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

Parameters:

  • data (Hash)

Returns:


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(Errors::InvalidNamespace, data)
  end
  unless data[:recipes].is_a?(Array)
    raise(Errors::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.

Parameters:

  • id (String, Symbol)

    recipe ID

Returns:

Raises:


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 Errors::RecipeNotFound, id
end

#[]=(id, recipe) ⇒ Saper::Recipe

Caches recipe instance.

Parameters:

Returns:


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 Errors::ActionExpected
    end
  end
  @recipes[id.to_s] = recipe
end

#find(id) ⇒ Saper::Recipe

Searches for and returns a recipe.

Returns:

Raises:


121
122
123
# File 'lib/saper/core/namespace.rb', line 121

def find(id)
  raise Errors::RecipeNotFound, id
end

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

Runs an recipe and returns resulting Saper:Runtime.

Parameters:

  • input (Object) (defaults to: nil)
  • name (String, Symbol) (defaults to: nil)

    action name

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

    options for Runtime instance

Returns:


72
73
74
# File 'lib/saper/core/namespace.rb', line 72

def run(name = nil, input = nil, options ={})
  self[name].run(input, options)
end

#serializeHash

Returns a serialized representation of this Namespace.

Returns:

  • (Hash)

133
134
135
# File 'lib/saper/core/namespace.rb', line 133

def serialize
  { :recipes => @recipes.values.map(&:serialize) }
end

#sizeInteger

Returns the number of recipes within this namespace.

Returns:

  • (Integer)

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.

Returns:

  • (String)

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.

Parameters:

  • id (String, Symbol)

    recipe ID

Returns:


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