Class: DbSchema::Definitions::Field::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/db_schema/definitions/field/base.rb

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

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/db_schema/definitions/field/base.rb', line 6

def default
  @default
end

#nameObject (readonly)

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_valuesObject



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

.typeObject



91
92
93
# File 'lib/db_schema/definitions/field/base.rb', line 91

def type
  Field.registry.key(self)
end

.valid_attributesObject



83
84
85
# File 'lib/db_schema/definitions/field/base.rb', line 83

def valid_attributes
  @valid_attributes ||= []
end

Instance Method Details

#array?Boolean

Returns:



28
29
30
# File 'lib/db_schema/definitions/field/base.rb', line 28

def array?
  false
end

#attributesObject



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

#custom?Boolean

Returns:



32
33
34
# File 'lib/db_schema/definitions/field/base.rb', line 32

def custom?
  false
end

#default_is_expression?Boolean

Returns:



24
25
26
# File 'lib/db_schema/definitions/field/base.rb', line 24

def default_is_expression?
  default.is_a?(Symbol)
end

#null?Boolean

Returns:



20
21
22
# File 'lib/db_schema/definitions/field/base.rb', line 20

def null?
  !primary_key? && @null
end

#optionsObject



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

Returns:



16
17
18
# File 'lib/db_schema/definitions/field/base.rb', line 16

def primary_key?
  @primary_key
end

#typeObject



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