Class: DbSchema::Definitions::Field::Base
- Inherits:
-
Object
- Object
- DbSchema::Definitions::Field::Base
show all
- Defined in:
- lib/db_schema/definitions/field/base.rb
Direct Known Subclasses
Array, BigInt, Bit, Boolean, Box, Bytea, Char, Chkpass, Cidr, Circle, Citext, Cube, Custom, Date, DateRange, DoublePrecision, EAN13, Hstore, ISBN, ISBN13, ISMN, ISMN13, ISSN, ISSN13, Inet, Int4Range, Int8Range, Integer, Interval, JSON, JSONB, Line, Lseg, Ltree, MacAddr, Money, NumRange, Numeric, Path, Point, Polygon, Real, Seg, SmallInt, Text, Time, Timestamp, Timestamptz, Timetz, TsQuery, TsRange, TsTzRange, TsVector, UPC, UUID, Varbit, Varchar
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, primary_key: false, null: true, default: nil, **attributes) ⇒ Base
Returns a new instance of Base.
8
9
10
11
12
13
14
|
# File 'lib/db_schema/definitions/field/base.rb', line 8
def initialize(name, primary_key: false, null: true, default: nil, **attributes)
@name = name
@primary_key = primary_key
@null = null
@default = process_default(default)
@attributes = attributes
end
|
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
6
7
8
|
# File 'lib/db_schema/definitions/field/base.rb', line 6
def default
@default
end
|
#name ⇒ Object
Returns the value of attribute name.
6
7
8
|
# File 'lib/db_schema/definitions/field/base.rb', line 6
def name
@name
end
|
Class Method Details
.attributes(*attr_names, **attributes_with_defaults) ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/db_schema/definitions/field/base.rb', line 66
def attributes(*attr_names, **attributes_with_defaults)
valid_attributes.push(*attr_names)
attributes_with_defaults.each do |attr_name, default_value|
valid_attributes.push(attr_name)
default_attribute_values[attr_name] = default_value
end
end
|
.default_attribute_values ⇒ Object
79
80
81
|
# File 'lib/db_schema/definitions/field/base.rb', line 79
def default_attribute_values
@default_attribute_values ||= {}
end
|
.register(*types) ⇒ Object
60
61
62
63
64
|
# File 'lib/db_schema/definitions/field/base.rb', line 60
def register(*types)
types.each do |type|
Field.registry[type] = self
end
end
|
.type ⇒ Object
83
84
85
|
# File 'lib/db_schema/definitions/field/base.rb', line 83
def type
Field.registry.key(self)
end
|
.valid_attributes ⇒ Object
75
76
77
|
# File 'lib/db_schema/definitions/field/base.rb', line 75
def valid_attributes
@valid_attributes ||= []
end
|
Instance Method Details
#attributes ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/db_schema/definitions/field/base.rb', line 31
def attributes
self.class.valid_attributes.reduce({}) do |hash, attr_name|
if attr_value = @attributes[attr_name]
hash.merge(attr_name => attr_value)
elsif default_value = self.class.default_attribute_values[attr_name]
hash.merge(attr_name => default_value)
else
hash
end
end
end
|
#custom_type? ⇒ Boolean
43
44
45
|
# File 'lib/db_schema/definitions/field/base.rb', line 43
def custom_type?
false
end
|
20
21
22
|
# File 'lib/db_schema/definitions/field/base.rb', line 20
def null?
!primary_key? && @null
end
|
#options ⇒ Object
24
25
26
27
28
29
|
# File 'lib/db_schema/definitions/field/base.rb', line 24
def options
attributes.tap do |options|
options[:null] = false unless null?
options[:default] = default unless default.nil?
end
end
|
#primary_key? ⇒ Boolean
16
17
18
|
# File 'lib/db_schema/definitions/field/base.rb', line 16
def primary_key?
@primary_key
end
|
#process_default(default) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/db_schema/definitions/field/base.rb', line 51
def process_default(default)
if default.is_a?(Symbol)
Sequel.function(default)
else
default
end
end
|
#type ⇒ Object
47
48
49
|
# File 'lib/db_schema/definitions/field/base.rb', line 47
def type
self.class.type
end
|