Class: Codabel::Column

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, path, type, options) ⇒ Column

Returns a new instance of Column.



3
4
5
6
7
8
# File 'lib/codabel/column.rb', line 3

def initialize(range, path, type, options)
  @range = range
  @path = Array(path)
  @type = type
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/codabel/column.rb', line 12

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/codabel/column.rb', line 10

def path
  @path
end

#rangeObject (readonly)

Returns the value of attribute range.



9
10
11
# File 'lib/codabel/column.rb', line 9

def range
  @range
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/codabel/column.rb', line 11

def type
  @type
end

Instance Method Details

#lengthObject



14
15
16
# File 'lib/codabel/column.rb', line 14

def length
  range.size
end

#path_starts_with?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/codabel/column.rb', line 24

def path_starts_with?(prefix)
  prefix = Array(prefix)
  @path[0...prefix.size] == prefix
end

#specifics?(data) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/codabel/column.rb', line 29

def specifics?(data)
  return false if path.empty?

  !!data.dig(*path)
end

#to_coda(record) ⇒ Object



18
19
20
21
22
# File 'lib/codabel/column.rb', line 18

def to_coda(record)
  value = record.data.dig(*path) unless path.empty?
  value = options[:default] if value.nil?
  type.to_coda(value, length)
end