Class: Blur::Script

Inherits:
Module
  • Object
show all
Includes:
Logging
Defined in:
library/blur/script.rb,
library/blur/script/cache.rb,
library/blur/script/messageparsing.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: MessageParsing Classes: Cache

Constant Summary

Emissions =
[:connection_ready, :topic_change, :user_rename, :message,
:private_message, :user_entered, :user_left, :user_quit,
:user_kicked, :topic, :user_mode, :channel_mode]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Script) initialize(path)

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'library/blur/script.rb', line 36

def initialize path
  @__path = path
  @__evaluated = false
  @__emissions = []
  
  if evaluate and @__evaluated
    cache.load if Cache.exists? @__name

    Emissions.each do |emission|
      @__emissions.push emission if respond_to? emission
    end
    
    __send__ :loaded if respond_to? :loaded
  end
end

Instance Attribute Details

- (Object) __author

The author of the script.

Returns:

  • the author of the script.



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

def __author
  @__author
end

- (Network::Client) __client

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

Returns:

  • (Network::Client)

    the client delegate.



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

def __client
  @__client
end

- (Array) __emissions

A list of handled emissions.

Returns:

  • (Array)

    a list of handled emissions.



30
31
32
# File 'library/blur/script.rb', line 30

def __emissions
  @__emissions
end

- (Object) __name

The name of the script.

Returns:

  • the name of the script.



19
20
21
# File 'library/blur/script.rb', line 19

def __name
  @__name
end

- (Object) __path

The path in which the script remains.

Returns:

  • the path in which the script remains.



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

def __path
  @__path
end

- (Object) __version

The version of the script.

Returns:

  • the version of the script.



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

def __version
  @__version
end

Instance Method Details

- (Object) cache

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



86
87
88
# File 'library/blur/script.rb', line 86

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

- (Boolean) evaluated?

Check to see if the script has been evaluated.

Returns:

  • (Boolean)


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

def evaluated?; @__evaluated end

- (Object) inspect

Convert it to a debug-friendly format.



91
92
93
# File 'library/blur/script.rb', line 91

def inspect
  File.basename @__path
end

- (Script) script(name)

Access another script with name name.

Returns:

  • (Script)

    the script with the name name, or nil.



81
82
83
# File 'library/blur/script.rb', line 81

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

- (Object) Script(name, version = [1,0], author = nil, &block)

Make it a DSL-way of writing a script.

Examples:

Script :example do
  def connection_ready network
    # …
  end
end


60
61
62
63
64
65
66
67
68
# File 'library/blur/script.rb', line 60

def Script name, version = [1,0], author = nil, &block
  @__name    = name
  @__author  = author
  @__version = version
  
  instance_eval &block
  
  true
end

- (Object) unload!

Unload the script and save the cache, if present.



71
72
73
74
75
76
# File 'library/blur/script.rb', line 71

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

  @__cache = nil
end