Class: Blur::Script

Inherits:
Object
  • Object
show all
Includes:
Evaluable, DSL, Logging
Defined in:
library/blur/script.rb,
library/blur/script/dsl.rb,
library/blur/script/cache.rb,
library/blur/script/commands.rb

Overview

TODO:

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.

See Also:

Defined Under Namespace

Modules: Commands, DSL Classes: Cache

Constant Summary

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]
@@__extensions =

A list of extensions.

[]

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from DSL

#Author, #Description, #Version, #author, #description, included, #name, #version

Methods included from Evaluable

#evaluate_source_file

Constructor Details

- (Script) initialize(path)

Instantiates a script and evaluates the contents which remain in path.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'library/blur/script.rb', line 54

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

- (Network::Client) __client

Can be used inside the script to act with the client itself.

Returns:

  • (Network::Client)

    the client delegate.



25
26
27
# File 'library/blur/script.rb', line 25

def __client
  @__client
end

- (Array) __emissions

Returns a list of handled emissions.

Returns:

  • (Array)

    a list of handled emissions.



27
28
29
# File 'library/blur/script.rb', line 27

def __emissions
  @__emissions
end

- (Object) __path

Returns the path in which the script remains.

Returns:

  • the path in which the script remains.



22
23
24
# File 'library/blur/script.rb', line 22

def __path
  @__path
end

Class Method Details

+ (Object) load_extensions!

Find and evaluate script extensions.



33
34
35
36
37
38
39
40
41
42
43
# File 'library/blur/script.rb', line 33

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

+ (Object) unload_extensions!

“Unload” all script extensions.



46
47
48
# File 'library/blur/script.rb', line 46

def self.unload_extensions!
  @@__extensions.clear
end

Instance Method Details

- (Object) cache

Get the cache, if none, instantiate a new cache.



130
131
132
# File 'library/blur/script.rb', line 130

def cache
  @__cache ||= Cache.new self
end

- (Boolean) evaluated?

Check to see if the script has been evaluated.

Returns:

  • (Boolean)


51
# File 'library/blur/script.rb', line 51

def evaluated?; @__evaluated end

- (Object) inspect

Convert it to a debug-friendly format.



135
136
137
# File 'library/blur/script.rb', line 135

def inspect
  File.basename @__path
end

- (Object) Script(name, options = {}, &block)

Make it a DSL-way of writing a script.

Examples:

Script :example do
  def connection_ready network
    # …
  end
end


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'library/blur/script.rb', line 79

def Script name, options = {}, &block
  @__name = name

  extensions = options[:using] || options[:uses] 

  # Automatically used extensions.
  if extensions
    extensions.each {|extension_name| using extension_name }
  end

  # Automatically included modules.
  if options[:includes]
    options[:includes].each{|module_name| self.extend module_name }
  end
  
  instance_eval &block
  
  true
end

- (Script) script(name)

Access another script with name name.

Returns:

  • (Script)

    the script with the name name, or nil.



125
126
127
# File 'library/blur/script.rb', line 125

def script name
  @__client.scripts.find { |script| script.__name == name }
end

- (Object) unload!

Unload the script and save the cache, if present.



115
116
117
118
119
120
# File 'library/blur/script.rb', line 115

def unload!
  cache.save if @__cache
  __send__ :unloaded if respond_to? :unloaded

  @__cache = nil
end

- (Object) using(*extension_names)

Add script extension and define a method with the same name as the extension.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'library/blur/script.rb', line 101

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.metaclass.send :define_method, :#{extension_name}" do
        return extension
      end
    else
      raise ExtensionNotFoundError, "Extension not found: #{extension_name}"
    end
  end
end