Class: IMW::Recordizer::StringSliceRecordizer

Inherits:
Object
  • Object
show all
Defined in:
lib/imw/recordizer/string_slice_recordizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ranges) ⇒ StringSliceRecordizer

Returns a new instance of StringSliceRecordizer.



7
8
9
# File 'lib/imw/recordizer/string_slice_recordizer.rb', line 7

def initialize ranges
  @schema = ranges
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/imw/recordizer/string_slice_recordizer.rb', line 5

def schema
  @schema
end

Instance Method Details

#recordize(line) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/imw/recordizer/string_slice_recordizer.rb', line 11

def recordize line
  format = schema
  case format
  when Array then slice_by_array(line, format)
  when Hash  then slice_by_hash(line, format)
  end
end

#slice_by_array(string, format) ⇒ Object



23
24
25
# File 'lib/imw/recordizer/string_slice_recordizer.rb', line 23

def slice_by_array string, format
  format.map { |range| slice_range(string, range) }
end

#slice_by_hash(string, format) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/imw/recordizer/string_slice_recordizer.rb', line 27

def slice_by_hash string, format
  format.inject({}) do |hsh, (key, val)|
    case val
    when Range then hsh[key] = slice_range(string, val)
    when Hash  then hsh[key] = slice_by_hash(string, val)
    end
    hsh
  end
end

#slice_range(string, range) ⇒ Object



19
20
21
# File 'lib/imw/recordizer/string_slice_recordizer.rb', line 19

def slice_range string, range
  string.slice(range).strip
end