Module: Treat::Workers::Groupable

Defined in:
lib/treat/workers/groupable.rb

Constant Summary collapse

@@list =

Cache the list of workers to improve performance.

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(group) ⇒ Object

Modify the extended class.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/treat/workers/groupable.rb', line 76

def self.extended(group)

  group.module_eval do
    
    class << self
      
      # The type of the group. There are three types:
      #
      # - Transformers transform the tree of an entity.
      # - Annotators compute a value and store it in the entity.
      # - Computers compute a value and do not store it.
      attr_accessor :type
      # The default worker in the group, for language-
      # independent tasks.
      attr_accessor :default
      # The entity types which the group's workers work on.
      attr_accessor :targets
      # Presets to automatically generate functions.
      attr_accessor :presets
      # The preset option to use with preset functions.
      attr_accessor :preset_option
      # Whether to recurse within multiple targets or not.
      attr_accessor :recursive
    end
    
    self.recursive = false
    self.list
    
    # Return the method corresponding to the group.
    # This method resolves the name of the method
    # that a group should provide based on the name
    # of the group. Basically, if the group ends in
    # -ers, the verb corresponding to the group is
    # returned (tokenizers -> tokenize, inflectors ->
    # inflect). Otherwise, the name of the method
    # is the same as that of the group (encoding ->
    # encoding, tag -> tag).
    @method = nil
    def self.method
      return @method if @method
      m = self.mn.ucc.dup
      if  m[-4..-1] == 'zers'
        if type == :annotator
          m[-5..-1] = m[-6] == 'l' ? '' : 'y'
        else
          m = m[0..-3]
        end
        n = m         
      elsif m[-4..-1] == 'iers'
        m[-4..-1] = 'y'
        n = m
      elsif m[-3..-1] == 'ers'
        if ['k', 't', 'm', 'd', 
            'g', 'n', 'x', 'h'].
            include? m[-4]
          n = m[0..-4]
          if n[-1] == n[-2]
            n = n[0..-2] 
          end
        else
          n = m[0..-3]
        end
      elsif m[-3..-1] == 'ors'
        n = m[0..-4] + 'e'
      else
        n = m
      end
      @method = n.intern
    end
    
  end
  
end

Instance Method Details

#add(class_name, &block) ⇒ Object

Create a new algorithm within the group. Once the algorithm is added, it will be automatically installed on all the targets of the group.



60
61
62
63
64
65
66
67
68
69
# File 'lib/treat/workers/groupable.rb', line 60

def add(class_name, &block)
  c = class_name.cc.intern
  klass = self.const_set(c, Class.new)
  method = self.method
  @@list[self.mn.ucc] << class_name
  klass.send(:define_singleton_method,
  method) do |entity, options={}|
    block.call(entity, options)
  end
end

#const_get(const) ⇒ Object

Get constants in this module, excluding by default those defined by parent modules.



73
# File 'lib/treat/workers/groupable.rb', line 73

def const_get(const); super(const, false); end

#const_missing(const) ⇒ Object

Lazily load the worker classes in the group.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/treat/workers/groupable.rb', line 4

def const_missing(const)
  bits = self.ancestors[0].to_s.split('::')
  bits.collect! { |bit| bit.ucc }
  file = bits.join('/') + "/#{const.ucc}"
  path = Treat.paths.lib + "#{file}.rb"
  if not File.readable?(path)
    raise Treat::Exception,
    "File '#{file}.rb' corresponding to " +
    "requested worker #{self}::#{const} " +
    "does not exist."
  else
    require file
    if not self.const_defined?(const)
      raise Treat::Exception,
      "File #{file}.rb does not define " +
      "#{self}::#{const}."
    end
    const_get(const)
  end
end

#has_target?(target, strict = false) ⇒ Boolean

Boolean - does the group have the supplied class included in its targets?

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/treat/workers/groupable.rb', line 44

def has_target?(target, strict = false)
  is_target = false
  self.targets.each do |entity_type|
    t = entity_type.cc
    entity_type = Treat::Entities.const_get(t)
    if target < entity_type ||
      entity_type == target
      is_target = true; break
    end
  end
  is_target
end

#listObject

Populates once the list of the workers in the group by crawling the filesystem.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/treat/workers/groupable.rb', line 29

def list
  mod = self.mn.ucc
  if @@list[mod].nil?
    @@list[mod] = []
    dirs = Dir[Treat.paths.lib + "treat/workers/*/#{mod}/*.rb"]
    dirs.each do |file|
      @@list[mod] <<
      file.split('/')[-1][0..-4].intern
    end
  end
  @@list[mod]
end