Class: Chawan::Nodes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chawan/nodes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ Nodes

Returns a new instance of Nodes.



5
6
7
# File 'lib/chawan/nodes.rb', line 5

def initialize(nodes)
  @nodes = nodes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object (private)



64
65
66
67
68
69
70
71
# File 'lib/chawan/nodes.rb', line 64

def method_missing(name, *arguments, &block)
  if self.class.gateway_interfaces.include?(name.to_s)
    self.class.define_gateway_interface(name)
    __send__(name)
  else
    nodes.__send__(name, *arguments, &block)
  end
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



3
4
5
# File 'lib/chawan/nodes.rb', line 3

def nodes
  @nodes
end

Class Method Details

.define_gateway_interface(name) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/chawan/nodes.rb', line 55

def self.define_gateway_interface(name)
  class_eval %{
    def #{name}
      Nodes.new(nodes.select{|n| n.__send__("#{name}?")})
    end
  }
end

.gateway_interfacesObject



51
52
53
# File 'lib/chawan/nodes.rb', line 51

def self.gateway_interfaces
  Chawan::Node::GatewayInterface.instance_methods.map(&:to_s).grep(/\?$/){$`}
end

Instance Method Details

#==(other) ⇒ Object



9
10
11
# File 'lib/chawan/nodes.rb', line 9

def ==(other)
  self.class == other.class and map(&:to_s) == other.map(&:to_s)
end

#compact(pattern = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chawan/nodes.rb', line 37

def compact(pattern = nil)
  array = []
  each do |node|
    if array.last and (pattern ?
                       (pattern === node.category && pattern === array.last.category) :
                       (array.last.category == node.category))
      array.last.word << node.word.to_s
    else
      array << Node.new(node.vals.map(&:dup), node.keys)
    end
  end
  self.class.new(array)
end

#each(&block) ⇒ Object



18
19
20
# File 'lib/chawan/nodes.rb', line 18

def each(&block)
  nodes.__send__(:each, &block)
end

#grep(pattern, &block) ⇒ Object

GatewayInterface



29
30
31
32
33
34
35
# File 'lib/chawan/nodes.rb', line 29

def grep(pattern, &block)
  if block
    nodes.__send__(regexp, &block)
  else
    Nodes.new(nodes.select{|n| pattern === n.category})
  end
end

#inspectObject



22
23
24
# File 'lib/chawan/nodes.rb', line 22

def inspect
  "[%s]" % map(&:inspect).join(', ')
end