Class: State
- Inherits:
-
Object
- Object
- State
- Defined in:
- lib/bad_word_detector/state.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#library ⇒ Object
readonly
Returns the value of attribute library.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Class Method Summary collapse
Instance Method Summary collapse
- #==(sec) ⇒ Object
- #append(rule) ⇒ Object
- #failure? ⇒ Boolean
-
#initialize(path, lib) ⇒ State
constructor
A new instance of State.
- #success? ⇒ Boolean
Constructor Details
#initialize(path, lib) ⇒ State
Returns a new instance of State.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/bad_word_detector/state.rb', line 4 def initialize(path, lib) @library = lib @path = path @text = '' @length = 0 @weight = 1 path.each_index do |index| # puts item.inspect rule = path[index] @text += rule.symbol @length += rule.length @weight *= rule.weight end end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
2 3 4 |
# File 'lib/bad_word_detector/state.rb', line 2 def length @length end |
#library ⇒ Object (readonly)
Returns the value of attribute library.
2 3 4 |
# File 'lib/bad_word_detector/state.rb', line 2 def library @library end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
2 3 4 |
# File 'lib/bad_word_detector/state.rb', line 2 def text @text end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
2 3 4 |
# File 'lib/bad_word_detector/state.rb', line 2 def weight @weight end |
Class Method Details
.get_library(symbol, library) ⇒ Object
43 44 45 46 |
# File 'lib/bad_word_detector/state.rb', line 43 def get_library(symbol, library) symbol = symbol || "" library[symbol] end |
Instance Method Details
#==(sec) ⇒ Object
38 39 40 |
# File 'lib/bad_word_detector/state.rb', line 38 def ==(sec) self.text == sec.text && self.length == sec.length && self.weight == sec.weight && self.library == sec.library end |
#append(rule) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/bad_word_detector/state.rb', line 20 def append(rule) last_char_index = @path.rindex {|r| r.symbol != '' } rule = if last_char_index and @path[last_char_index].symbol == rule.symbol and rule.symbol != '' Rule.new rule.char, '', 1 else rule end State.new @path + [rule], State.get_library(rule ? rule.symbol : '', @library) end |
#failure? ⇒ Boolean
30 31 32 |
# File 'lib/bad_word_detector/state.rb', line 30 def failure? not @library end |
#success? ⇒ Boolean
34 35 36 |
# File 'lib/bad_word_detector/state.rb', line 34 def success? @library.value end |