Class: Bumbleworks::ProcessDefinition
- Inherits:
-
Object
- Object
- Bumbleworks::ProcessDefinition
- Defined in:
- lib/bumbleworks/process_definition.rb
Defined Under Namespace
Classes: DuplicatesInDirectory, FileNotFound, Invalid, NotFound
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Class Method Summary collapse
-
.create!(opts) ⇒ Object
Instantiates a new ProcessDefinition, then ‘#save`s it.
-
.create_all_from_directory!(directory, opts = {}) ⇒ Object
For every *.rb file in the given directory (recursive), creates a new ProcessDefinition, reading the file’s contents for the definition string.
-
.define(*args, &block) ⇒ Object
This method provides a way to define a process definition directly, without having to create it as a string definition or a tree.
-
.find_by_name(search_key) ⇒ Object
Given a key, will instantiate a new ProcessDefinition populated with the tree found in the dashboard variables at that key.
Instance Method Summary collapse
-
#build_tree! ⇒ Object
Uses the TreeBuilder to construct a tree from the given definition.
-
#initialize(opts = {}) ⇒ ProcessDefinition
constructor
Initialize a new ProcessDefinition, supplying a name (required), and definition or a tree (one of which is required).
-
#load_definition_from_file(path) ⇒ Object
A definition can be loaded directly from a file (this is the easiest way to do it, after the .create_all_from_directory! method).
-
#save! ⇒ Object
Validates first, then adds the tree (builds it if necessary) to the dashboard’s variables.
-
#validate! ⇒ Object
Validates the ProcessDefinition by checking existence and uniqueness of name, existence of one of definition or tree, and validity of definition.
Constructor Details
#initialize(opts = {}) ⇒ ProcessDefinition
Initialize a new ProcessDefinition, supplying a name (required), and definition or a tree (one of which is required). The definition should be a string with a Bumbleworks.define_process block, and the tree should be an actual Ruote tree.
18 19 20 21 22 |
# File 'lib/bumbleworks/process_definition.rb', line 18 def initialize(opts = {}) @name = opts[:name] @definition = opts[:definition] @tree = opts[:tree] end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
10 11 12 |
# File 'lib/bumbleworks/process_definition.rb', line 10 def definition @definition end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/bumbleworks/process_definition.rb', line 10 def name @name end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
10 11 12 |
# File 'lib/bumbleworks/process_definition.rb', line 10 def tree @tree end |
Class Method Details
.create!(opts) ⇒ Object
Instantiates a new ProcessDefinition, then ‘#save`s it.
110 111 112 113 114 |
# File 'lib/bumbleworks/process_definition.rb', line 110 def create!(opts) pdef = new(opts) pdef.save! pdef end |
.create_all_from_directory!(directory, opts = {}) ⇒ Object
For every *.rb file in the given directory (recursive), creates a new ProcessDefinition, reading the file’s contents for the definition string. If the :skip_invalid option is specified, all invalid definitions are skipped, and everything else is loaded. Otherwise, the first invalid definition found triggers a rollback and raises the exception.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/bumbleworks/process_definition.rb', line 124 def create_all_from_directory!(directory, opts = {}) added_names = [] definition_files = Bumbleworks::Support.all_files(directory) if definition_files.values.uniq.count != definition_files.count raise DuplicatesInDirectory, "Definitions directory contains duplicate filenames" end definition_files.each do |path, basename| puts "Registering process definition #{basename} from file #{path}" if opts[:verbose] == true begin create!(:name => basename, :definition => File.read(path)) added_names << basename rescue Invalid raise unless opts[:skip_invalid] == true end end rescue Invalid added_names.each do |name| Bumbleworks.dashboard.variables[name] = nil end raise end |
.define(*args, &block) ⇒ Object
This method provides a way to define a process definition directly, without having to create it as a string definition or a tree. It takes a block identical to Ruote.define’s block, normalizes the definition’s name, and ‘#create!`s a ProcessDefinition with the resulting tree.
101 102 103 104 105 |
# File 'lib/bumbleworks/process_definition.rb', line 101 def define(*args, &block) tree_builder = Bumbleworks::TreeBuilder.from_definition(*args, &block) tree_builder.build! create!(:tree => tree_builder.tree, :name => tree_builder.name) end |
.find_by_name(search_key) ⇒ Object
Given a key, will instantiate a new ProcessDefinition populated with the tree found in the dashboard variables at that key. If the key isn’t found, an exception is thrown.
87 88 89 90 91 92 93 |
# File 'lib/bumbleworks/process_definition.rb', line 87 def find_by_name(search_key) if saved_tree = Bumbleworks.dashboard.variables[search_key] new(:name => search_key, :tree => saved_tree) else raise NotFound, "No definition by the name of \"#{search_key}\" has been registered yet" end end |
Instance Method Details
#build_tree! ⇒ Object
Uses the TreeBuilder to construct a tree from the given definition. Captures any tree building exceptions and reraises them as Bumbleworks::ProcessDefinition::Invalid exceptions.
58 59 60 61 62 63 64 65 |
# File 'lib/bumbleworks/process_definition.rb', line 58 def build_tree! return nil unless @definition @tree = Bumbleworks::TreeBuilder.new( :name => name, :definition => definition ).build! rescue Bumbleworks::TreeBuilder::InvalidTree => e raise Invalid, e. end |
#load_definition_from_file(path) ⇒ Object
A definition can be loaded directly from a file (this is the easiest way to do it, after the .create_all_from_directory! method). Simply reads the file at the given path, and set this instance’s definition to the contents of that file.
73 74 75 76 77 78 79 |
# File 'lib/bumbleworks/process_definition.rb', line 73 def load_definition_from_file(path) if File.exists?(path) @definition = File.read(path) else raise FileNotFound, "No file found at #{path}" end end |
#save! ⇒ Object
Validates first, then adds the tree (builds it if necessary) to the dashboard’s variables.
47 48 49 50 51 |
# File 'lib/bumbleworks/process_definition.rb', line 47 def save! validate! Bumbleworks.dashboard.variables[@name] = @tree || build_tree! self end |
#validate! ⇒ Object
Validates the ProcessDefinition by checking existence and uniqueness of name, existence of one of definition or tree, and validity of definition. Raises a Bumbleworks::ProcessDefinition::Invalid exception if errors are found, otherwise returns true
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bumbleworks/process_definition.rb', line 30 def validate! errors = [] errors << "Name must be specified" unless @name errors << "Definition or tree must be specified" unless @definition || @tree begin build_tree! rescue Invalid errors << "Definition is not a valid process definition" end raise Invalid, "Validation failed: #{errors.join(', ')}" unless errors.empty? true end |