Class: Condition::Reader::ConvertSheet

Inherits:
Object
  • Object
show all
Defined in:
lib/condition/reader/convert_sheet.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ ConvertSheet

Returns a new instance of ConvertSheet.



8
9
10
11
# File 'lib/condition/reader/convert_sheet.rb', line 8

def initialize(redis)
  @redis = redis
  @reader = Condition::Reader::RooReader.new
end

Instance Method Details

#convert(path, sheet_index, name) ⇒ Object



13
14
15
# File 'lib/condition/reader/convert_sheet.rb', line 13

def convert(path, sheet_index, name)
  @redis.set(name, JSON.generate(@reader.read_sheet(path, sheet_index)))
end

#convert_dir(path, with_dir_name: true, file_name: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/condition/reader/convert_sheet.rb', line 29

def convert_dir(path, with_dir_name: true, file_name: nil)
  basename = File.basename(path)
  Dir::entries(path).each do |f|
    next if "." == f || ".." == f || /^\.~lock\.[^.]+\.ods.$/ =~ f
    if !file_name.nil?
      next if f != file_name
    end
    prefix = with_dir_name ? basename : nil
    convert_file("#{path}/#{f}", prefix: prefix)
  end
end

#convert_file(path, prefix: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/condition/reader/convert_sheet.rb', line 17

def convert_file(path, prefix: nil)
  name = File.basename(path, ".*")
  ss =  Roo::Spreadsheet.open(path)
  ss.sheets.each do |it|
    ss.default_sheet = it
    blocks = @reader.read(ss)
    key = "#{name}_#{it}"
    key = "#{prefix}_#{key}" if !prefix.nil?
    @redis.set(key, JSON.generate(blocks))
  end
end