Class: RecordLoader::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/record_loader/attribute.rb

Overview

Used by the generator to help guide the generation of the initial yaml files

Constant Summary collapse

BASE_TIME =
Time.parse('2021-10-08 13:00:40 +0100')
BASE_DATE =
Date.parse('2021-10-08')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, default) ⇒ Attribute

Create a new attribute

Parameters:

  • name (String)

    The name of the attribute, takes the column name

  • type (Symbol)

    The attribute type (eg. :string) as returned by the rails connection adapter

  • default (Object)

    The default value of the column



28
29
30
31
32
# File 'lib/record_loader/attribute.rb', line 28

def initialize(name, type, default)
  @name = name
  @type = type
  @default = default
end

Instance Attribute Details

#defaultObject (readonly)

Returns The default of the attribute.

Returns:

  • (Object)

    The default of the attribute



19
20
21
# File 'lib/record_loader/attribute.rb', line 19

def default
  @default
end

#nameString (readonly)

Returns The name of the attribute, takes the column name.

Returns:

  • (String)

    The name of the attribute, takes the column name



13
14
15
# File 'lib/record_loader/attribute.rb', line 13

def name
  @name
end

#typeSymbol (readonly)

Returns The attribute type (eg. :string) as returned by the rails connection adapter.

Returns:

  • (Symbol)

    The attribute type (eg. :string) as returned by the rails connection adapter



16
17
18
# File 'lib/record_loader/attribute.rb', line 16

def type
  @type
end

Instance Method Details

#ruby_value(index) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/record_loader/attribute.rb', line 42

def ruby_value(index)
  case type
  when :datetime, :timestamp then "Time.parse('#{value(index)}')"
  when :date then "Date.parse('#{value(index)}')"
  else
    value(index).inspect
  end
end

#value(index) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/record_loader/attribute.rb', line 34

def value(index)
  if @default.nil?
    value_for_type(index)
  else
    @default
  end
end