Class: SVF::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/svf/cell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition, start = nil) ⇒ Cell

Returns a new instance of Cell.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/svf/cell.rb', line 5

def initialize(name, definition, start = nil)
  @name = name.to_sym
  if definition.is_a? String
    definition = { type: definition }
  elsif definition.is_a? Hash
    definition.symbolize_keys!
  else
    return nil
  end
  if definition[:type] =~ /\-/
    type, format = definition[:type].split('-')[0..1]
    definition = { type: type.to_sym }
    if format =~ /^\d+$/
      definition[:length] = format.to_i
    elsif type.to_sym == :float && format.match(/^\d+\.\d+$/)
      definition[:format] = format
      definition[:length] = format.split('.').inject(1) { |s, e| s += e.to_i }
    end
  else
    definition[:type] = definition[:type].to_sym
  end
  @type = definition.delete(:type)
  if @type == :boolean
    definition[:length] = 1
  elsif @type == :date
    definition[:length] = 8
  end
  @length = definition.delete(:length)
  raise definition.inspect if @length.nil?
  @format = definition.delete(:format)
  @options = definition.delete(:options) || {}
  @default = definition.delete(:default)
  @start = start
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default


3
4
5
# File 'lib/svf/cell.rb', line 3

def default
  @default
end

#formatObject (readonly)

Returns the value of attribute format


3
4
5
# File 'lib/svf/cell.rb', line 3

def format
  @format
end

#lengthObject (readonly)

Returns the value of attribute length


3
4
5
# File 'lib/svf/cell.rb', line 3

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name


3
4
5
# File 'lib/svf/cell.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


3
4
5
# File 'lib/svf/cell.rb', line 3

def options
  @options
end

#startObject (readonly)

Returns the value of attribute start


3
4
5
# File 'lib/svf/cell.rb', line 3

def start
  @start
end

#typeObject (readonly)

Returns the value of attribute type


3
4
5
# File 'lib/svf/cell.rb', line 3

def type
  @type
end

Instance Method Details

#format_value(variable) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/svf/cell.rb', line 71

def format_value(variable)
  if type == :boolean
    value = "(#{variable} ? '1' : '0')"
  elsif type == :date
    value = "(#{variable}.nil? ? '#{' ' * 8}' : #{variable}.strftime('%d%m%Y'))"
  elsif type == :integer
    value = "#{variable}.to_i.to_s.rjust(#{length})[0..#{length - 1}]"
  elsif type == :float
    size = format.split('.')
    value = "(#{variable}.to_i.to_s + ',' + ((#{variable} - #{variable}.to_i)*#{10**size[1].to_i}).to_i.abs.to_s.rjust(#{size[1].to_i}, '0')).rjust(#{length})[0..#{length - 1}]"
  elsif type == :string
    value = "#{variable}.to_s.rjust(#{length})[0..#{length - 1}]"
  end
  value
end

#inspectObject


44
45
46
# File 'lib/svf/cell.rb', line 44

def inspect
  "#{name}(#{type}/#{length})"
end

#parse_value(line = 'line', start = nil) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/svf/cell.rb', line 48

def parse_value(line = 'line', start = nil)
  start ||= @start
  value = "#{line}[#{start}..#{start + length - 1}]"
  if type == :boolean
    value = "(#{value} == '1' ? true : false)"
  elsif type == :date
    value = "(#{value}.blank? ? nil : Date.civil(#{line}[#{start + 4}..#{start + 7}].to_i, #{line}[#{start + 2}..#{start + 3}].to_i, #{line}[#{start + 0}..#{start + 1}].to_i))"
  elsif type == :integer
    value = "#{value}.to_s.strip.to_i"
  elsif type == :float
    # size = self.format.split(".")[0].to_i
    # value = "(#{line}[#{start}..#{start+size-1}]+'.'+#{line}[#{start+size+1}..#{start+self.length-1}]).to_d"
    value = "#{value}.to_s.tr(',', '.').to_d"
  elsif type == :string
    value = "#{value}.to_s.strip.encode('UTF-8')"
  end
  value
end

#required?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/svf/cell.rb', line 40

def required?
  false
end

#stopObject


67
68
69
# File 'lib/svf/cell.rb', line 67

def stop
  @start + length - 1
end