Class: QDA::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/weft/category.rb

Direct Known Subclasses

Backend::SQLite::CategoryTreeNode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_name, a_parent = nil, a_memo = '') ⇒ Category

Returns a new instance of Category.



15
16
17
18
19
20
21
22
# File 'lib/weft/category.rb', line 15

def initialize(a_name, a_parent = nil, a_memo = '')
  @children = []
  self.name = a_name
     self.memo = a_memo
  self.parent = a_parent
  # @codes    = QDA::CodingTable.new()
  @codes = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/weft/category.rb', line 5

def children
  @children
end

#codesObject (readonly)

Returns the value of attribute codes.



5
6
7
# File 'lib/weft/category.rb', line 5

def codes
  @codes
end

#dbidObject

Returns the value of attribute dbid.



6
7
8
# File 'lib/weft/category.rb', line 6

def dbid
  @dbid
end

#memoObject

Returns the value of attribute memo.



6
7
8
# File 'lib/weft/category.rb', line 6

def memo
  @memo
end

#metaObject

Returns the value of attribute meta.



6
7
8
# File 'lib/weft/category.rb', line 6

def meta
  @meta
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/weft/category.rb', line 5

def name
  @name
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/weft/category.rb', line 5

def parent
  @parent
end

#readerObject (readonly)

Returns the value of attribute reader.



5
6
7
# File 'lib/weft/category.rb', line 5

def reader
  @reader
end

#textObject

Returns the value of attribute text.



6
7
8
# File 'lib/weft/category.rb', line 6

def text
  @text
end

Class Method Details

.parse_path(path) ⇒ Object



8
9
10
11
12
13
# File 'lib/weft/category.rb', line 8

def Category.parse_path(path)
  bits = path.scan(/(?:[^\/]|\/\/)+/)
  bits.map! { | x | x.gsub(/\/\//, '/') }
  bits.unshift('') if path =~ /\A\/(?!\/)/
  bits
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/weft/category.rb', line 43

def ==(other)
  if other.respond_to?(:dbid)
    if self.dbid.nil? and other.dbid.nil?
      return self.name == other.name && self.parent == other.parent
    else
      return self.dbid == other.dbid && self.parent == other.parent
    end
  elsif other.nil?
    return false
  else
    raise "No comparison of Category with #{other.inspect}"
  end        
end

#[](idx) ⇒ Object

returns all the children who match idx



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/weft/category.rb', line 79

def [](idx)
  case idx
    when Fixnum, Range
      return @children[idx]
    when Regexp
      return @children.find { | c | c.name =~ idx}
    when String
      patt = /\A#{Regexp.escape(idx)}\z/i
      return @children.find { | c | c.name =~ patt }
    else
      raise ArgumentError.new("Bad index #{idx}")
  end
end

#ancestorsObject



128
129
130
131
132
# File 'lib/weft/category.rb', line 128

def ancestors()
  ancestor, ancestors = self, []
  ancestors.push(ancestor) while ancestor = ancestor.parent
  ancestors
end

#code(docid, offset, length) ⇒ Object

to that document. docid should be the database id of the document to be retrieved (a string)



170
171
172
173
174
# File 'lib/weft/category.rb', line 170

def code(docid, offset, length)
     codetable_init()
     new_code = QDA::Code.new(docid, offset, length)
     @codes.add(new_code)
end

#codetable=(codetable) ⇒ Object



159
160
161
# File 'lib/weft/category.rb', line 159

def codetable=(codetable)
  @codes = codetable
end

#codetable_initObject



163
164
165
# File 'lib/weft/category.rb', line 163

def codetable_init()
  @codes ||= QDA::CodingTable.new()
end

#delete(target) ⇒ Object



93
94
95
# File 'lib/weft/category.rb', line 93

def delete(target)
  @children.delete(target)
end

#descendantsObject



134
135
136
# File 'lib/weft/category.rb', line 134

def descendants()
  @children.map() { | c | [ c, c.descendants ] }.flatten
end

#each_childObject



119
120
121
# File 'lib/weft/category.rb', line 119

def each_child
  children.each { | c | yield c }
end

#escape_nameObject



39
40
41
# File 'lib/weft/category.rb', line 39

def escape_name()
  name.gsub(/\//, '//')
end

#find(path) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/weft/category.rb', line 138

def find(path)
  points = Category.parse_path(path)
  scope = @children
  points.delete('')
  while point = points.shift
    scope = scope.find_all { | x | x.name =~ /^#{point}/ }
    scope.map! { | x | x.children }.flatten! unless points.empty?
  end
  scope
end

#is_ancestor_of?(cat) ⇒ Boolean

returns true if cat is a Category located below self

Returns:

  • (Boolean)


150
151
152
# File 'lib/weft/category.rb', line 150

def is_ancestor_of?(cat)
  descendants.include?(cat)
end

#is_descendant_of?(cat) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/weft/category.rb', line 155

def is_descendant_of?(cat)
  ancestors.include?(cat)
end

#num_of_charsObject



115
116
117
# File 'lib/weft/category.rb', line 115

def num_of_chars
  @codes.num_of_chars
end

#num_of_codesObject



111
112
113
# File 'lib/weft/category.rb', line 111

def num_of_codes
  @codes.num_of_codes
end

#num_of_docsObject



107
108
109
# File 'lib/weft/category.rb', line 107

def num_of_docs
  @codes.num_of_docs
end

#pathObject



122
123
124
125
126
# File 'lib/weft/category.rb', line 122

def path()
  ancestors.inject("/" << escape_name) do | path, anc |
    "/" << anc.escape_name << path
  end
end

#to_sObject Also known as: inspect



183
184
185
# File 'lib/weft/category.rb', line 183

def to_s()
  "<Category '#{path}' [#{dbid}]>"
end

#uncode(docid, offset, length) ⇒ Object



176
177
178
179
180
181
# File 'lib/weft/category.rb', line 176

def uncode(docid, offset, length)
  @codes ||= QDA::CodingTable.new()
	  # raise "docid should be an integer > 0, is #{docid}" if docid == 0
  c = Code.new(docid, offset, length)
  @codes.subtract(c)
end

#unique_name(name) ⇒ Object

number of separate documents coded by this category



98
99
100
101
102
103
104
105
# File 'lib/weft/category.rb', line 98

def unique_name(name)
  while self[ name ]
    puts "#{name} exists"
    name.sub!(/(?:\s*\((\d+)\))?$/) { " (#{$1.to_i.succ})" }
    puts "trying #{name}"
  end
  name
end