Class: ActiveRecordSchemaScrapper::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_schema_scrapper/attribute.rb

Defined Under Namespace

Classes: DBToRubyType, DefaultValueType

Instance Method Summary collapse

Constructor Details

#initialize(type: nil, precision: nil, scale: nil, default: nil, cast_type: nil, name: nil, limit: nil, null: nil) ⇒ Attribute

Returns a new instance of Attribute.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_record_schema_scrapper/attribute.rb', line 11

def initialize(type: nil,
               precision: nil,
               scale: nil,
               default: nil,
               cast_type: nil,
               name: nil,
               limit: nil,
               null: nil)

  type    = init_type(type, cast_type)
  default = init_default(default, cast_type, type)
  super
end

Instance Method Details

#init_default(default, cast_type, type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_record_schema_scrapper/attribute.rb', line 37

def init_default(default, cast_type, type)
  return default unless default
  registered_default = Attributes.registered_defaults.detect do |reg_default, klass, reg_cast_type, reg_type|
    if (default.to_s == reg_default.to_s) && ((reg_cast_type && reg_cast_type === cast_type) || (type === reg_type))
      klass
    elsif default.to_s == reg_default.to_s
      klass
    end
  end
  (registered_default && !registered_default.empty?) ? registered_default[1] : default
end

#init_type(type, cast_type) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_record_schema_scrapper/attribute.rb', line 25

def init_type(type, cast_type)
  return type unless type
  registered_type = Attributes.registered_types.detect do |reg_type, klass, reg_cast_type|
    if type.to_sym == reg_type.to_sym && reg_cast_type && reg_cast_type === cast_type
      klass
    elsif type.to_sym == reg_type.to_sym
      klass
    end
  end
  (registered_type && !registered_type.empty?) ? registered_type[1] : type
end