Class: StringImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/turbine-core/importers/string_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ StringImporter

Returns a new instance of StringImporter.



5
6
7
8
9
10
11
# File 'lib/turbine-core/importers/string_importer.rb', line 5

def initialize(type)
  @type = type
  @result = []
  @remainder = ''
  @primary = nil
  @heading = nil
end

Instance Attribute Details

#remainderObject

Returns the value of attribute remainder.



3
4
5
# File 'lib/turbine-core/importers/string_importer.rb', line 3

def remainder
  @remainder
end

#resultObject

Returns the value of attribute result.



3
4
5
# File 'lib/turbine-core/importers/string_importer.rb', line 3

def result
  @result
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/turbine-core/importers/string_importer.rb', line 3

def type
  @type
end

Instance Method Details

#detect_pair(line) ⇒ Object

of parse_pairs



44
45
46
# File 'lib/turbine-core/importers/string_importer.rb', line 44

def detect_pair(line)
  line.strip.match(/(^[A-Za-z0-9_]+):(.+)/)
end

#eval_heading_fieldObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/turbine-core/importers/string_importer.rb', line 59

def eval_heading_field
  # check for an <h1> at the beginning of the primary and if it's there pull it out for the heading
  possible_hone = remainder.match(/(.+)\n=+\n*|^# (.+)\w*\n*/)
  
  caps = []
  caps = possible_hone.captures.compact unless possible_hone.blank?
  
  # if it's there, then parse it out of the remainder
  if !possible_hone.blank? and caps.length == 1
    @heading = caps.first
    
    ### Remove heading from text
    @primary = @primary.
      strip.
      gsub(/^#{possible_hone.captures.first}\n=+\n*/, '').
      gsub(/^# #{possible_hone.captures.first}\w*\n*/, '').
      strip
  end#of if
end

#eval_primary_fieldObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/turbine-core/importers/string_importer.rb', line 48

def eval_primary_field
  unless @remainder.blank? || @type.primary_field.blank?
    @primary = @remainder
    
    eval_heading_field unless @type.heading_field.blank?
    
    save_primary
    save_heading
  end
end

#import(text) ⇒ Object



13
14
15
16
17
18
# File 'lib/turbine-core/importers/string_importer.rb', line 13

def import(text)
  pull_pairs(text)
  eval_primary_field
  
  @result
end

#pull_pairs(text) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/turbine-core/importers/string_importer.rb', line 20

def pull_pairs(text)
  last_pair_found = false

  text.each_line do |line|
  
    unless last_pair_found
      possible_pair = detect_pair(line)
      if possible_pair and possible_pair.captures.length == 2
        key = possible_pair.captures[0].downcase.to_sym
        value = possible_pair.captures[1]
        number_of_lines = value.count("\n") + 1
        @result << { key => value.strip }
        next # don't put this in the remainder
      else
        last_pair_found = true
      end#of if
    end
  
    @remainder << line # keep this around, it might be useful
  end#of each_line
  
  @remainder.strip!
end

#save_headingObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/turbine-core/importers/string_importer.rb', line 96

def save_heading
  unless @heading.blank?
    # see if a pair already exists for the primary_field
    existing_heading = @result.select { |pair| pair.keys.first == @type.heading_field }
    existing_heading = existing_heading.first
  
    # get rid of the primary_field if it's already there
    @result.delete(existing_heading)
  
    # if there was any existing, prepend it to remanding
    @heading = existing_heading.to_s + @heading unless existing_heading.blank?
  
    # append a new pair for the fields
    @result << { @type.heading_field => @heading } unless @heading.blank?
  end
end

#save_primaryObject

of eval_heading_field



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/turbine-core/importers/string_importer.rb', line 79

def save_primary
  unless @primary.blank?
    # see if a pair already exists for the primary_field
    existing_primary = @result.select { |pair| pair.keys.first == @type.primary_field }
    existing_primary = existing_primary.first
  
    # get rid of the primary_field if it's already there
    @result.delete(existing_primary)
  
    # if there was any existing, prepend it to remanding
    @primary = existing_primary.to_s + @primary unless existing_primary.blank?
  
    # append a new pair for the fields
    @result << { @type.primary_field => @primary } unless @primary.blank?
  end
end