Class: ActiveRecordSchemaScrapper::Attributes

Inherits:
Object
  • Object
show all
Extended by:
DeprecatedRegisterDefaultKeywords
Includes:
Enumerable
Defined in:
lib/active_record_schema_scrapper/attributes.rb

Defined Under Namespace

Modules: DeprecatedRegisterDefaultKeywords

Constant Summary collapse

DEFAULT_REGISTERED_TYPES =
{ type: :integer,                                            replacement_type: Integer },
  { type: :float,                                              replacement_type: Float },
  { type: :decimal,                                            replacement_type: BigDecimal },
  { type: Proc.new { |t| [:timestamp, :time].include?(t) },    replacement_type: Time },
  { type: :datetime,                                           replacement_type: DateTime },
  { type: :date,                                               replacement_type: Date },
  { type: Proc.new { |t| %i{text string binary}.include?(t) }, replacement_type: String },
  { type: :boolean,                                            replacement_type: Axiom::Types::Boolean },
  { type: :hstore,                                             replacement_type: Hash },
].freeze
DEFAULT_REGISTERED_DEFAULTS =
[
  { default: Proc.new { |d| d == 't' }, replacement_default: true, type: :boolean },
  { default: Proc.new { |d| d == 'f' }, replacement_default: false, type: :boolean },
  { default: Proc.new { |d| d == '1' }, replacement_default: true, type: :boolean },
  { default: Proc.new { |d| d == '0' }, replacement_default: false, type: :boolean }
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeprecatedRegisterDefaultKeywords

register_default

Constructor Details

#initialize(model:) ⇒ Attributes

Returns a new instance of Attributes.



84
85
86
87
# File 'lib/active_record_schema_scrapper/attributes.rb', line 84

def initialize(model:)
  @model  = model
  @errors = []
end

Class Method Details

.register_default(default:, replacement_default:, cast_type: :not_given, type: :not_given) ⇒ Object

Parameters:

  • default (String)

    original default value from schema

  • replacement_default (Object)

    the replacement value

  • cast_type (Object#===, Proc#===) (defaults to: :not_given)

    to be compared to the db schema returned value

  • type (Symbol) (defaults to: :not_given)

    matches the type from the schema



68
69
70
# File 'lib/active_record_schema_scrapper/attributes.rb', line 68

def register_default(default:, replacement_default:, cast_type: :not_given, type: :not_given)
  registered_defaults << { default: default, replacement_default: replacement_default, cast_type: cast_type, type: type }
end

.register_type(name:, klass:, cast_type: :not_given) ⇒ Object

Parameters:

  • name (Symbol)

    original type from schema

  • klass (Object, Virtus::Attribute)

    a ruby type used to coerce values

  • cast_type (Object#===, Proc#===) (defaults to: :not_given)

    to be compared to the db schema returned value



26
27
28
# File 'lib/active_record_schema_scrapper/attributes.rb', line 26

def register_type(name:, klass:, cast_type: :not_given)
  registered_types << { type: name, replacement_type: klass, cast_type: cast_type }
end

.registered_defaultsObject



72
73
74
# File 'lib/active_record_schema_scrapper/attributes.rb', line 72

def registered_defaults
  @registered_defaults ||= DEFAULT_REGISTERED_DEFAULTS.dup
end

.registered_typesObject



30
31
32
# File 'lib/active_record_schema_scrapper/attributes.rb', line 30

def registered_types
  @registered_types ||= DEFAULT_REGISTERED_TYPES.dup
end

.reset_registered_defaultsObject



76
77
78
# File 'lib/active_record_schema_scrapper/attributes.rb', line 76

def reset_registered_defaults
  @registered_defaults = DEFAULT_REGISTERED_DEFAULTS.dup
end

.reset_registered_typesObject



34
35
36
# File 'lib/active_record_schema_scrapper/attributes.rb', line 34

def reset_registered_types
  @registered_types = DEFAULT_REGISTERED_TYPES.dup
end

Instance Method Details

#eachObject



91
92
93
# File 'lib/active_record_schema_scrapper/attributes.rb', line 91

def each
  call.each { |attr| yield(attr) }
end

#errorsObject



99
100
101
102
# File 'lib/active_record_schema_scrapper/attributes.rb', line 99

def errors
  to_a
  @errors
end

#to_aObject



95
96
97
# File 'lib/active_record_schema_scrapper/attributes.rb', line 95

def to_a
  @to_a ||= map { |v| v }
end