Class: Plugin

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/bowl/app/libs/plugins.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plugin

Returns a new instance of Plugin.

Raises:

  • (IOError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bowl/app/libs/plugins.rb', line 30

def initialize(name)
  @name = name
  @meta = {}
  @options = Hash.new(false)
  @fields = Hash.new{|h,k|
    h[k] = {
      :name => nil,
      :index => lambda{ @fields.keys.size }.call,
      :type => String,
      :default => nil,
      :value => nil
    }
  }
  __code = nil
  open(self.class.list[name]) do |f|
    __code = f.read
  end
  raise IOError, "cannot read plug-in file \"#{name}\"" unless __code
  instance_eval __code
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



28
29
30
# File 'lib/bowl/app/libs/plugins.rb', line 28

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/bowl/app/libs/plugins.rb', line 28

def name
  @name
end

Class Method Details

.dirsObject



3
4
5
# File 'lib/bowl/app/libs/plugins.rb', line 3

def self.dirs
  @@dirs
end

.listObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bowl/app/libs/plugins.rb', line 12

def self.list
  @@list ||=
    (
     h = {}
     self.dirs.each do |d|
       FileUtils.mkdir_p d unless File.exists? d
       Dir.glob(File.expand_path '*.rb', d).each do |rb|
         name = rb.scan(/([^\/]+)\.rb$/)[0][0]
         h[name] = rb
       end
     end
     h
     )
end

Instance Method Details

#call(&block) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bowl/app/libs/plugins.rb', line 72

def call(&block)
  raise ArgumentError, 'block not given' unless block_given?
  this = self
  CometIO.on "plugin_#{name}" do |data, session_id|
    this.fields.each do |name, field|
      data[name] = field[:default] if field[:default] and data[name].to_s.empty?
    end
    this.instance_variables.each do |name|
      name = name.to_s.gsub(/^@/,'')
      next if ['meta','fields'].include? name
      data[name] = this.instance_eval "@#{name}"
    end
    h = Hashie::Mash.new(data)
    if this.get :callback
      def h.callback(data)
        CometIO.push __callback, data if __callback.to_s.size > 0
      end
    end
    h.instance_eval &block
  end
end

#data(&block) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
# File 'lib/bowl/app/libs/plugins.rb', line 67

def data(&block)
  raise ArgumentError, 'block not given' unless block_given?
  instance_eval &block
end

#field(name, params = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/bowl/app/libs/plugins.rb', line 94

def field(name, params={})
  if Hashie::Mash.instance_methods.include? name.to_sym
    raise ArgumentError, "field \"#{name}\" is reserved."
  end
  fields[name][:name] = name
  params.each do |k,v|
    fields[name][k] = v
  end
end

#get(k) ⇒ Object



63
64
65
# File 'lib/bowl/app/libs/plugins.rb', line 63

def get(k)
  @options[k.to_sym]
end

#meta(meta = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/bowl/app/libs/plugins.rb', line 51

def meta(meta=nil)
  if meta.kind_of? Hash
    @meta = meta
  else
    return @meta
  end
end

#set(k, v) ⇒ Object



59
60
61
# File 'lib/bowl/app/libs/plugins.rb', line 59

def set(k,v)
  @options[k.to_sym] = v
end