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, NullField
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 = 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
74
75
76
77
78
79
80
81
|
# File 'lib/db_schema/definitions/field/base.rb', line 74
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
87
88
89
|
# File 'lib/db_schema/definitions/field/base.rb', line 87
def default_attribute_values
@default_attribute_values ||= {}
end
|
.register(*types) ⇒ Object
68
69
70
71
72
|
# File 'lib/db_schema/definitions/field/base.rb', line 68
def register(*types)
types.each do |type|
Field.registry[type] = self
end
end
|
.type ⇒ Object
91
92
93
|
# File 'lib/db_schema/definitions/field/base.rb', line 91
def type
Field.registry.key(self)
end
|
.valid_attributes ⇒ Object
83
84
85
|
# File 'lib/db_schema/definitions/field/base.rb', line 83
def valid_attributes
@valid_attributes ||= []
end
|
Instance Method Details
28
29
30
|
# File 'lib/db_schema/definitions/field/base.rb', line 28
def array?
false
end
|
#attributes ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/db_schema/definitions/field/base.rb', line 43
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
|
32
33
34
|
# File 'lib/db_schema/definitions/field/base.rb', line 32
def custom?
false
end
|
#default_is_expression? ⇒ Boolean
24
25
26
|
# File 'lib/db_schema/definitions/field/base.rb', line 24
def default_is_expression?
default.is_a?(Symbol)
end
|
20
21
22
|
# File 'lib/db_schema/definitions/field/base.rb', line 20
def null?
!primary_key? && @null
end
|
#options ⇒ Object
36
37
38
39
40
41
|
# File 'lib/db_schema/definitions/field/base.rb', line 36
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
|
#type ⇒ Object
55
56
57
|
# File 'lib/db_schema/definitions/field/base.rb', line 55
def type
self.class.type
end
|
#with_attribute(attr_name, attr_value) ⇒ Object
63
64
65
|
# File 'lib/db_schema/definitions/field/base.rb', line 63
def with_attribute(attr_name, attr_value)
Field.build(name, type, **options, primary_key: primary_key?, attr_name => attr_value)
end
|
#with_type(new_type) ⇒ Object
59
60
61
|
# File 'lib/db_schema/definitions/field/base.rb', line 59
def with_type(new_type)
Field.build(name, new_type, **options, primary_key: primary_key?)
end
|