Class: Abaci::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/abaci/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Column

Returns a new instance of Column.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/abaci/column.rb', line 5

def initialize(name)
  components = name.to_s.strip.split(/\s+/)
  @name = components.shift
  type = (components.shift || '').downcase
  @type = if type == 'd'
            :decimal
          elsif type == 'i'
            :integer
          elsif type == 'b'
            :boolean
          elsif type == 'l'
            :list
          else
            :string
          end
  if type =~ /r\(\w+\)/
    @references = Nomen[type[2..-2]] || raise("Cannot find #{type}")
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


3
4
5
# File 'lib/abaci/column.rb', line 3

def name
  @name
end

#referencesObject (readonly)

Returns the value of attribute references


3
4
5
# File 'lib/abaci/column.rb', line 3

def references
  @references
end

#typeObject (readonly)

Returns the value of attribute type


3
4
5
# File 'lib/abaci/column.rb', line 3

def type
  @type
end

Instance Method Details

#cast(value) ⇒ Object

Cast value from string


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/abaci/column.rb', line 26

def cast(value)
  return nil if value.blank?
  if @type == :decimal
    value.to_d
  elsif @type == :integer
    value.to_i
  elsif @type == :boolean
    %w(true yes ok t 1).include?(value.downcase)
  elsif @type == :list
    value.split(/[[:space:]]*\,[[:space:]]*/)
  else
    value
  end
end