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

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, 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

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

Class Method Details

.columns_hashObject

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



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



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

Instance Method Details

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



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

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

#[](key) ⇒ Object

Plain access to any property, declared or not



49
50
51
52
# File 'lib/torque/postgresql/attributes/base.rb', line 49

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

#[]=(key, value) ⇒ Object



54
55
56
# File 'lib/torque/postgresql/attributes/base.rb', line 54

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @attributes.keys.empty?
end

#hashObject



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

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

#inspectObject



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

def inspect
  entries = attributes.map { |key, value| "#{key}: #{value.inspect}" }
  "#<#{self.class.name} #{entries.join(', ')}>"
end

#read_attribute_before_type_cast(name) ⇒ Object



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

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

#read_attribute_for_database(name) ⇒ Object



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

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

#to_hObject



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

def to_h
  attributes.symbolize_keys
end

#type_for_attribute(name, &block) ⇒ Object



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

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