Class: CWG::Sentence

Inherits:
Object
  • Object
show all
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/sentence.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) index

todo



7
8
9
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 7

def index
  @index
end

Instance Method Details

- (Object) all



10
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 10

def all               ; @sentences             ; end

- (Object) change



22
23
24
25
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 22

def change
  forward if next?
  rewind if previous?
end

- (Boolean) change?

Returns:

  • (Boolean)


17
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 17

def change?           ; next? || previous?     ; end

- (Boolean) change_or_repeat?

Returns:

  • (Boolean)


18
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 18

def change_or_repeat? ; change? || repeat?     ; end

- (Object) check_end_of_book(progress_file)



42
43
44
45
46
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 42

def check_end_of_book progress_file
  if @index >= @sentences.size
    reset_progress progress_file
  end
end

- (Object) check_sentence_navigation(chr)



88
89
90
91
92
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 88

def check_sentence_navigation chr
  @next     = true if(chr == ']')
  @previous = true if(chr == '[')
  @repeat   = true if(chr == '-')
end

- (Object) create_progress_maybe(progress_file)



31
32
33
34
35
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 31

def create_progress_maybe progress_file
  unless File.exists? progress_file
    reset_progress progress_file
  end
end

- (Object) current



19
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 19

def current           ; @sentences[@index]     ; end

- (Object) cw_chars(chr)



65
66
67
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 65

def cw_chars chr
  chr.tr('^a-z0-9\,\=\!\/\?\.', '')
end

- (Object) exclude_non_cw_chars(word)



69
70
71
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 69

def exclude_non_cw_chars word
  cw_chars(word)
end

- (Object) find_all



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 73

def find_all
  @sentences = []
  @text.gsub!(/\s+/, ' ').downcase!
  loop do
    sentence_end = @text.index('. ')
    unless sentence_end
      break
    end
    line = @text[0..sentence_end]
    line = line.split.collect{|word| exclude_non_cw_chars word}.join(' ')
    @sentences << line
    @text.replace @text[sentence_end + 2..-1]
  end
end

- (Object) forward

todo def forward ; @index += 1 ; end



14
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 14

def forward           ; @index                 ; end

- (Object) next



11
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 11

def next              ; @next = true           ; end

- (Boolean) next?

Returns:

  • (Boolean)


12
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 12

def next?             ; @next                  ; end

- (Object) next_sentence



20
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 20

def next_sentence     ; @sentences[@index + 1] ; end

- (Boolean) previous?

Returns:

  • (Boolean)


15
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 15

def previous?         ; @previous              ; end

- (Object) read_book(book)



61
62
63
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 61

def read_book book
  File.open(book, 'r') { |f| text.replace f.readlines(' ').join}
end

- (Object) read_progress(progress_file)



48
49
50
51
52
53
54
55
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 48

def read_progress progress_file
  create_progress_maybe progress_file
  File.open(progress_file, 'r') {|f| @index = f.readline.to_i}
  unless(@index && @index.class == Fixnum)
    reset_progress progress_file
  end
  check_end_of_book progress_file
end

- (Boolean) repeat?

Returns:

  • (Boolean)


16
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 16

def repeat?           ; @repeat                ; end

- (Object) reset_flags



94
95
96
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 94

def reset_flags
  @next = @previous = @repeat = nil
end

- (Object) reset_progress(progress_file)



37
38
39
40
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 37

def reset_progress progress_file
  @index = 0
  write_progress progress_file
end

- (Object) rewind



27
28
29
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 27

def rewind
  @index = @index <= 1 ? 0 : @index - 1
end

- (Object) text



9
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 9

def text              ; @text ||= String.new   ; end

- (Object) to_array



98
99
100
101
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 98

def to_array
  array = @sentences[@index].split(' ')
  array.collect {|x| x + ' '}
end

- (Object) write_progress(progress_file)



57
58
59
# File '/Users/martyn/cw/cw_clone/lib/cw/sentence.rb', line 57

def write_progress progress_file
  File.open(progress_file, 'w') {|f| f.puts @index.to_s}
end