Class: Blur::Script
- Inherits:
-
Object
- Object
- Blur::Script
- Defined in:
- library/blur/script.rb,
library/blur/script/dsl.rb,
library/blur/script/cache.rb,
library/blur/script/commands.rb
Overview
add examples in the documentation
The Script
class is used for encapsulating dynamically loaded ruby scripts.
The #Script method is then used to shape the DSL-language to make writing Blur scripts a breeze.
Defined Under Namespace
Modules: Commands, DSL Classes: Cache
Constant Summary collapse
- ExtensionNotFoundError =
Class.new StandardError
- Emissions =
[:connection_ready, :topic_change, :user_rename, :message, :private_message, :user_entered, :user_left, :user_quit, :user_kicked, :topic, :user_mode, :channel_mode, :channel_created, :channel_who_reply]
- @@__extensions =
A list of extensions.
[]
Instance Attribute Summary collapse
-
#__client ⇒ Network::Client
Can be used inside the script to act with the client itself.
-
#__emissions ⇒ Array
A list of handled emissions.
-
#__path ⇒ Object
The path in which the script remains.
Class Method Summary collapse
-
.load_extensions! ⇒ Object
Find and evaluate script extensions.
-
.unload_extensions! ⇒ Object
“Unload” all script extensions.
Instance Method Summary collapse
-
#cache ⇒ Object
Get the cache, if none, instantiate a new cache.
-
#evaluated? ⇒ Boolean
Check to see if the script has been evaluated.
-
#initialize(path) ⇒ Script
constructor
Instantiates a script and evaluates the contents which remain in
path
. -
#inspect ⇒ Object
Convert it to a debug-friendly format.
-
#Script(name, options = {}, &block) ⇒ Object
Make it a DSL-way of writing a script.
-
#script(name) ⇒ Script
Access another script with name
name
. -
#unload! ⇒ Object
Unload the script and save the cache, if present.
-
#using(*extension_names) ⇒ Object
Add script extension and define a method with the same name as the extension.
Methods included from DSL
#Author, #Description, #Version, #author, #description, included, #name, #version
Methods included from Evaluable
Constructor Details
#initialize(path) ⇒ Script
Instantiates a script and evaluates the contents which remain in path
.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'library/blur/script.rb', line 55 def initialize path @__path = path @__evaluated = false @__emissions = [] if evaluate_source_file path cache.load if Cache.exists? @__name Emissions.each do |emission| @__emissions.push emission if respond_to? emission end __send__ :loaded if respond_to? :loaded __send__ :module_init if respond_to? :module_init end end |
Instance Attribute Details
#__client ⇒ Network::Client
Can be used inside the script to act with the client itself.
26 27 28 |
# File 'library/blur/script.rb', line 26 def __client @__client end |
#__emissions ⇒ Array
Returns a list of handled emissions.
28 29 30 |
# File 'library/blur/script.rb', line 28 def __emissions @__emissions end |
#__path ⇒ Object
Returns the path in which the script remains.
23 24 25 |
# File 'library/blur/script.rb', line 23 def __path @__path end |
Class Method Details
.load_extensions! ⇒ Object
Find and evaluate script extensions.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'library/blur/script.rb', line 34 def self.load_extensions! root_path = File.dirname $0 Dir.glob("#{root_path}/extensions/*.rb").each do |path| extension = Extension.new path extension.__client = self extension.extension_loaded if extension.respond_to? :extension_loaded @@__extensions << extension end end |
.unload_extensions! ⇒ Object
“Unload” all script extensions.
47 48 49 |
# File 'library/blur/script.rb', line 47 def self.unload_extensions! @@__extensions.clear end |
Instance Method Details
#cache ⇒ Object
Get the cache, if none, instantiate a new cache.
131 132 133 |
# File 'library/blur/script.rb', line 131 def cache @__cache ||= Cache.new self end |
#evaluated? ⇒ Boolean
Check to see if the script has been evaluated.
52 |
# File 'library/blur/script.rb', line 52 def evaluated?; @__evaluated end |
#inspect ⇒ Object
Convert it to a debug-friendly format.
136 137 138 |
# File 'library/blur/script.rb', line 136 def inspect File.basename @__path end |
#Script(name, options = {}, &block) ⇒ Object
Make it a DSL-way of writing a script.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'library/blur/script.rb', line 80 def Script name, = {}, &block @__name = name extensions = [:using] || [:uses] # Automatically used extensions. if extensions extensions.each {|extension_name| using extension_name } end # Automatically included modules. if [:includes] [:includes].each{|module_name| self.extend module_name } end instance_eval &block true end |
#script(name) ⇒ Script
Access another script with name name
.
126 127 128 |
# File 'library/blur/script.rb', line 126 def script name @__client.scripts.find { |script| script.__name == name } end |
#unload! ⇒ Object
Unload the script and save the cache, if present.
116 117 118 119 120 121 |
# File 'library/blur/script.rb', line 116 def unload! cache.save if @__cache __send__ :unloaded if respond_to? :unloaded @__cache = nil end |
#using(*extension_names) ⇒ Object
Add script extension and define a method with the same name as the extension.
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'library/blur/script.rb', line 102 def using *extension_names extension_names.each do |extension_name| if extension = @@__extensions.find{|ext| ext.__name.to_s == extension_name.to_s } extension.extension_used self if extension.respond_to? :extension_used self..send :define_method, :"#{extension_name}" do return extension end else raise ExtensionNotFoundError, "Extension not found: #{extension_name}" end end end |