Class: Tabledata::Dsls::TableDefinition
- Inherits:
-
Object
- Object
- Tabledata::Dsls::TableDefinition
- Defined in:
- lib/tabledata/dsls/table_definition.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
Instance Method Summary collapse
- #binary(*args) ⇒ Object
- #boolean(*args) ⇒ Object
- #column_defaults(value) ⇒ Object
- #date(*args) ⇒ Object
- #datetime(*args) ⇒ Object
- #define_column(type, accessor, *args) ⇒ Object
- #definition ⇒ Object
-
#initialize(identifier = nil, table_name = nil, &block) ⇒ TableDefinition
constructor
A new instance of TableDefinition.
- #integer(*args) ⇒ Object
- #next_index ⇒ Object
- #skip_columns(n = 1) ⇒ Object
- #string(*args) ⇒ Object
- #validate_row ⇒ Object
- #validate_table ⇒ Object
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns
11 12 13 |
# File 'lib/tabledata/dsls/table_definition.rb', line 11 def columns @columns end |
#identifier ⇒ Object (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) = args.last.is_a?(Hash) ? @column_defaults.merge(args.pop.dup) : @column_defaults.dup default = .delete(:default) { nil } allow_nil = .delete(:nil) { true } adaptor = .delete(:adapt) validator = .delete(:validate) pre_validator = .delete(:pre_validate) empty_string_is_nil = .delete(:empty_string_is_nil) { false } strip = .delete(:strip) { false } index = .delete(:index) { next_index } column = ColumnDefinition.new(index, accessor, header, type, allow_nil, default, strip, empty_string_is_nil, pre_validator, adaptor, validator, ) @occupied_indices[index] = true @columns << column column end |
#definition ⇒ Object
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_index ⇒ Object
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_row ⇒ Object
88 89 |
# File 'lib/tabledata/dsls/table_definition.rb', line 88 def validate_row end |
#validate_table ⇒ Object
91 92 |
# File 'lib/tabledata/dsls/table_definition.rb', line 91 def validate_table end |