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, null: true, default: nil, **attributes) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
# File 'lib/db_schema/definitions/field/base.rb', line 8

def initialize(name, null: true, default: nil, **attributes)
  @name       = name
  @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



77
78
79
80
81
82
83
84
# File 'lib/db_schema/definitions/field/base.rb', line 77

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



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

def default_attribute_values
  @default_attribute_values ||= {}
end

.register(*types) ⇒ Object



71
72
73
74
75
# File 'lib/db_schema/definitions/field/base.rb', line 71

def register(*types)
  types.each do |type|
    Field.registry[type] = self
  end
end

.typeObject



94
95
96
# File 'lib/db_schema/definitions/field/base.rb', line 94

def type
  Field.registry.key(self)
end

.valid_attributesObject



86
87
88
# File 'lib/db_schema/definitions/field/base.rb', line 86

def valid_attributes
  @valid_attributes ||= []
end

Instance Method Details

#array?Boolean

Returns:



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

def array?
  false
end

#attributesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/db_schema/definitions/field/base.rb', line 38

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:



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

def custom?
  false
end

#default_is_expression?Boolean

Returns:



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

def default_is_expression?
  default.is_a?(Symbol)
end

#null?Boolean

Returns:



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

def null?
  @null
end

#optionsObject



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

def options
  attributes.tap do |options|
    options[:null] = false unless null?
    options[:default] = default unless default.nil?
  end
end

#typeObject



50
51
52
# File 'lib/db_schema/definitions/field/base.rb', line 50

def type
  self.class.type
end

#with_attribute(attr_name, attr_value) ⇒ Object



58
59
60
# File 'lib/db_schema/definitions/field/base.rb', line 58

def with_attribute(attr_name, attr_value)
  Field.build(name, type, **options, attr_name => attr_value)
end

#with_default(new_default) ⇒ Object



66
67
68
# File 'lib/db_schema/definitions/field/base.rb', line 66

def with_default(new_default)
  Field.build(name, type, **options, default: new_default)
end

#with_null(null) ⇒ Object



62
63
64
# File 'lib/db_schema/definitions/field/base.rb', line 62

def with_null(null)
  Field.build(name, type, **options, null: null)
end

#with_type(new_type) ⇒ Object



54
55
56
# File 'lib/db_schema/definitions/field/base.rb', line 54

def with_type(new_type)
  Field.build(name, new_type, **options)
end