Module: Poefy::PoemBase

Included in:
Poem
Defined in:
lib/poefy/poem_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#corpusObject (readonly)

Returns the value of attribute corpus.



12
13
14
# File 'lib/poefy/poem_base.rb', line 12

def corpus
  @corpus
end

#localObject (readonly)

Returns the value of attribute local.



12
13
14
# File 'lib/poefy/poem_base.rb', line 12

def local
  @local
end

#overwriteObject (readonly)

Returns the value of attribute overwrite.



12
13
14
# File 'lib/poefy/poem_base.rb', line 12

def overwrite
  @overwrite
end

Instance Method Details

#closeObject

Close the database.



39
40
41
# File 'lib/poefy/poem_base.rb', line 39

def close
  @corpus.close
end

#initialize(db_name, options = {}) ⇒ Object



14
15
16
17
# File 'lib/poefy/poem_base.rb', line 14

def initialize db_name, options = {}
  handle_options options
  @corpus = Poefy::Database.new db_name, @local
end

#make_database(input, description = nil, overwrite = @overwrite) ⇒ Object

Make a database using the given lines.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/poefy/poem_base.rb', line 20

def make_database input, description = nil, overwrite = @overwrite
  lines = validate_lines input
  lines.map! do |line|
    line.force_encoding('utf-8')
        .gsub("\u00A0", ' ')
        .strip
  end
  @corpus.close if @corpus
  if overwrite
    @corpus.make_new! lines, description
  else
    @corpus.make_new lines, description
  end
end

#make_database!(input, description = nil) ⇒ Object



34
35
36
# File 'lib/poefy/poem_base.rb', line 34

def make_database! input, description = nil
  make_database input, description, true
end

#validate_lines(input) ⇒ Object

Validate the lines. Arg could be a filename,

newline delimited string, or array of lines.


45
46
47
48
49
50
51
52
# File 'lib/poefy/poem_base.rb', line 45

def validate_lines input

  # If the input is a file, then read it.
  lines = File.exists?(input.to_s) ? File.read(input) : input

  # If lines is not an array, assume string and split on newlines.
  lines.respond_to?(:each) ? lines : lines.split("\n")
end