Class: Tabledata::Dsls::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/tabledata/dsls/table_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier = nil, table_name = nil, &block) ⇒ TableDefinition


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tabledata/dsls/table_definition.rb', line 13

def initialize(identifier=nil, table_name=nil, &block)
  @identifier       = identifier
  @table_name       = table_name
  @column_defaults  = {}
  @current_index    = -1
  @occupied_indices = {}
  @columns          = []
  instance_eval(&block)

  @columns.sort_by!(&:index)
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns


11
12
13
# File 'lib/tabledata/dsls/table_definition.rb', line 11

def columns
  @columns
end

#identifierObject (readonly)

Returns the value of attribute identifier


10
11
12
# File 'lib/tabledata/dsls/table_definition.rb', line 10

def identifier
  @identifier
end

Instance Method Details

#binary(*args) ⇒ Object


84
85
86
# File 'lib/tabledata/dsls/table_definition.rb', line 84

def binary(*args)
  define_column :binary, *args
end

#boolean(*args) ⇒ Object


80
81
82
# File 'lib/tabledata/dsls/table_definition.rb', line 80

def boolean(*args)
  define_column :boolean, *args
end

#column_defaults(value) ⇒ Object


29
30
31
# File 'lib/tabledata/dsls/table_definition.rb', line 29

def column_defaults(value)
  @column_defaults = value
end

#date(*args) ⇒ Object


72
73
74
# File 'lib/tabledata/dsls/table_definition.rb', line 72

def date(*args)
  define_column :date, *args
end

#datetime(*args) ⇒ Object


76
77
78
# File 'lib/tabledata/dsls/table_definition.rb', line 76

def datetime(*args)
  define_column :datetime, *args
end

#define_column(type, accessor, *args) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tabledata/dsls/table_definition.rb', line 33

def define_column(type, accessor, *args)
  header              = args.shift if args.first.is_a?(String)
  options             = args.last.is_a?(Hash) ? @column_defaults.merge(args.pop.dup) : @column_defaults.dup
  default             = options.delete(:default) { nil }
  allow_nil           = options.delete(:nil) { true }
  adaptor             = options.delete(:adapt)
  validator           = options.delete(:validate)
  pre_validator       = options.delete(:pre_validate)
  empty_string_is_nil = options.delete(:empty_string_is_nil) { false }
  strip               = options.delete(:strip) { false }
  index               = options.delete(:index) { next_index }
  column              = ColumnDefinition.new(index, accessor, header, type, allow_nil, default, strip, empty_string_is_nil, pre_validator, adaptor, validator, options)

  @occupied_indices[index] = true
  @columns << column

  column
end

#definitionObject


25
26
27
# File 'lib/tabledata/dsls/table_definition.rb', line 25

def definition
  Tabledata::TableDefinition.new(@identifier, @table_name, @columns)
end

#integer(*args) ⇒ Object


68
69
70
# File 'lib/tabledata/dsls/table_definition.rb', line 68

def integer(*args)
  define_column :integer, *args
end

#next_indexObject


56
57
58
59
60
61
62
# File 'lib/tabledata/dsls/table_definition.rb', line 56

def next_index
  begin
    @current_index += 1
  end while @occupied_indices[@current_index]

  @current_index
end

#skip_columns(n = 1) ⇒ Object


52
53
54
# File 'lib/tabledata/dsls/table_definition.rb', line 52

def skip_columns(n=1)
  @current_index += n
end

#string(*args) ⇒ Object


64
65
66
# File 'lib/tabledata/dsls/table_definition.rb', line 64

def string(*args)
  define_column :string, *args
end

#validate_rowObject


88
89
# File 'lib/tabledata/dsls/table_definition.rb', line 88

def validate_row
end

#validate_tableObject


91
92
# File 'lib/tabledata/dsls/table_definition.rb', line 91

def validate_table
end