Class: Torque::PostgreSQL::Attributes::Base

Inherits:
Object
  • Object
show all
Extended by:
SimpleEnum::ClassMethods
Includes:
ActiveModel::Attributes, ActiveModel::Attributes::Normalization, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Validations::Callbacks, ActiveRecord::Encryption::EncryptableRecord, ActiveRecord::Store, SimpleEnum
Defined in:
lib/torque/postgresql/attributes/base.rb,
sig/torque/postgresql/attributes/base.rbs

Overview

The common ground between the classes that back a column with a set of typed properties, like structs and composite types. It carries enough of the ActiveRecord attribute surface for its own features, and for the ActiveRecord concerns that build on top of it, to work

Direct Known Subclasses

Composite, Struct

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleEnum::ClassMethods

defined_enums, defined_enums=, enum

Methods included from SimpleEnum

#defined_enums

Class Method Details

.columns_hash ⇒ Hash[String, column]

These classes are not backed by a table, so nothing here has the extra information that a column would provide

Returns:

  • (Hash[String, column])


30
31
32
# File 'lib/torque/postgresql/attributes/base.rb', line 30

def columns_hash
  {}
end

.encrypts(*names, **options) ⇒ Object

Encrypt individual attributes, so their values are stored encrypted inside the column that holds them

Parameters:

  • names (column_ref)
  • options (Object)

Returns:

  • (Object)


36
37
38
39
40
41
42
43
44
45
# File 'lib/torque/postgresql/attributes/base.rb', line 36

def encrypts(*names, **options)
  names.each do |name|
    raise ArgumentError, <<~MSG.squish unless attribute_names.include?(name.to_s)
      Unable to encrypt "#{name}" because it is not a declared
      attribute of #{self.name}.
    MSG
  end

  super
end

.filter_attributes ⇒ Array[Symbol | String | Regexp | Proc]

Returns:

  • (Array[Symbol | String | Regexp | Proc])


17
# File 'sig/torque/postgresql/attributes/base.rbs', line 17

def self.filter_attributes: () -> Array[Symbol | String | Regexp | Proc]

.filter_attributes= ⇒ Array[Symbol | String | Regexp | Proc]

Parameters:

  • value (Array[Symbol | String | Regexp | Proc])

Returns:

  • (Array[Symbol | String | Regexp | Proc])


18
# File 'sig/torque/postgresql/attributes/base.rbs', line 18

def self.filter_attributes=: (Array[Symbol | String | Regexp | Proc] value) -> Array[Symbol | String | Regexp | Proc]

.inspection_filter ⇒ ActiveSupport::ParameterFilter

Returns:

  • (ActiveSupport::ParameterFilter)


47
48
49
# File 'lib/torque/postgresql/attributes/base.rb', line 47

def inspection_filter
  ActiveSupport::ParameterFilter.new(filter_attributes)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


66
67
68
# File 'lib/torque/postgresql/attributes/base.rb', line 66

def ==(other)
  other.class == self.class && other.attributes == attributes
end

#[](key) ⇒ Object

Plain access to any property, declared or not

Parameters:

  • key (column_ref)

Returns:

  • (Object)


57
58
59
60
# File 'lib/torque/postgresql/attributes/base.rb', line 57

def [](key)
  key = key.to_s
  @attributes.key?(key) ? @attributes.fetch_value(key) : nil
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (column_ref)
  • value (Object)

Returns:

  • (Object)


62
63
64
# File 'lib/torque/postgresql/attributes/base.rb', line 62

def []=(key, value)
  assign_attributes(key => value)
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/torque/postgresql/attributes/base.rb', line 71

def empty?
  @attributes.keys.empty?
end

#hash ⇒ Integer

Returns:

  • (Integer)


75
76
77
# File 'lib/torque/postgresql/attributes/base.rb', line 75

def hash
  [self.class, attributes].hash
end

#inspect ⇒ String

Returns:

  • (String)


52
53
54
# File 'lib/torque/postgresql/attributes/base.rb', line 52

def inspect
  self.class.inspection_filter.filter(to_h).inspect
end

#read_attribute_before_type_cast(name) ⇒ Object

Parameters:

  • name (column_ref)

Returns:

  • (Object)


87
88
89
# File 'lib/torque/postgresql/attributes/base.rb', line 87

def read_attribute_before_type_cast(name)
  @attributes[name.to_s].value_before_type_cast
end

#read_attribute_for_database(name) ⇒ Object

Parameters:

  • name (column_ref)

Returns:

  • (Object)


91
92
93
# File 'lib/torque/postgresql/attributes/base.rb', line 91

def read_attribute_for_database(name)
  @attributes[name.to_s].value_for_database
end

#to_h ⇒ Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


79
80
81
# File 'lib/torque/postgresql/attributes/base.rb', line 79

def to_h
  attributes.symbolize_keys
end

#type_for_attribute(name) {|arg0| ... } ⇒ type_caster

Parameters:

  • name (column_ref)

Yields:

Yield Parameters:

  • arg0

Yield Returns:

  • (Object)

Returns:

  • (type_caster)


83
84
85
# File 'lib/torque/postgresql/attributes/base.rb', line 83

def type_for_attribute(name, &block)
  self.class.type_for_attribute(name, &block)
end