Class: ActiveModel::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute/user_provided_default.rb

Overview

:nodoc:

Direct Known Subclasses

ActiveRecord::Relation::QueryAttribute

Defined Under Namespace

Classes: UserProvidedDefault

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value_before_type_cast, type, original_attribute = nil, value = nil) ⇒ Attribute

This method should not be called directly. Use #from_database or #from_user



33
34
35
36
37
38
39
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 33

def initialize(name, value_before_type_cast, type, original_attribute = nil, value = nil)
  @name = name
  @value_before_type_cast = value_before_type_cast
  @type = type
  @original_attribute = original_attribute
  @value = value unless value.nil?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 29

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 29

def type
  @type
end

#value_before_type_castObject (readonly)

Returns the value of attribute value_before_type_cast.



29
30
31
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 29

def value_before_type_cast
  @value_before_type_cast
end

Class Method Details

.from_database(name, value_before_type_cast, type, value = nil) ⇒ Object



8
9
10
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 8

def from_database(name, value_before_type_cast, type, value = nil)
  FromDatabase.new(name, value_before_type_cast, type, nil, value)
end

.from_user(name, value_before_type_cast, type, original_attribute = nil) ⇒ Object



12
13
14
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 12

def from_user(name, value_before_type_cast, type, original_attribute = nil)
  FromUser.new(name, value_before_type_cast, type, original_attribute)
end

.null(name) ⇒ Object



20
21
22
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 20

def null(name)
  Null.new(name)
end

.uninitialized(name, type) ⇒ Object



24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 24

def uninitialized(name, type)
  Uninitialized.new(name, type)
end

.with_cast_value(name, value_before_type_cast, type) ⇒ Object



16
17
18
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 16

def with_cast_value(name, value_before_type_cast, type)
  WithCastValue.new(name, value_before_type_cast, type)
end

Instance Method Details

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



112
113
114
115
116
117
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 112

def ==(other)
  self.class == other.class &&
    name == other.name &&
    value_before_type_cast == other.value_before_type_cast &&
    type == other.type
end

#came_from_user?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 104

def came_from_user?
  false
end

#changed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 63

def changed?
  changed_from_assignment? || changed_in_place?
end

#changed_in_place?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 67

def changed_in_place?
  has_been_read? && type.changed_in_place?(original_value_for_database, value)
end

#encode_with(coder) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 132

def encode_with(coder)
  coder["name"] = name
  coder["value_before_type_cast"] = value_before_type_cast unless value_before_type_cast.nil?
  coder["type"] = type if type
  coder["original_attribute"] = original_attribute if original_attribute
  coder["value"] = value if defined?(@value)
end

#forgetting_assignmentObject



71
72
73
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 71

def forgetting_assignment
  with_value_from_database(value_for_database)
end

#has_been_read?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 108

def has_been_read?
  defined?(@value)
end

#hashObject



120
121
122
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 120

def hash
  [self.class, name, value_before_type_cast, type].hash
end

#init_with(coder) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 124

def init_with(coder)
  @name = coder["name"]
  @value_before_type_cast = coder["value_before_type_cast"]
  @type = coder["type"]
  @original_attribute = coder["original_attribute"]
  @value = coder["value"] if coder.map.key?("value")
end

#initialized?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 100

def initialized?
  true
end

#original_valueObject



47
48
49
50
51
52
53
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 47

def original_value
  if assigned?
    original_attribute.original_value
  else
    type_cast(value_before_type_cast)
  end
end

#original_value_for_databaseObject



140
141
142
143
144
145
146
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 140

def original_value_for_database
  if assigned?
    original_attribute.original_value_for_database
  else
    _original_value_for_database
  end
end

#serializable?(&block) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 59

def serializable?(&block)
  type.serializable?(value, &block)
end

#type_castObject

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 96

def type_cast(*)
  raise NotImplementedError
end

#valueObject



41
42
43
44
45
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 41

def value
  # `defined?` is cheaper than `||=` when we get back falsy values
  @value = type_cast(value_before_type_cast) unless defined?(@value)
  @value
end

#value_for_databaseObject



55
56
57
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 55

def value_for_database
  type.serialize(value)
end

#with_cast_value(value) ⇒ Object



84
85
86
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 84

def with_cast_value(value)
  self.class.with_cast_value(name, value, type)
end

#with_type(type) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 88

def with_type(type)
  if changed_in_place?
    with_value_from_user(value).with_type(type)
  else
    self.class.new(name, value_before_type_cast, type, original_attribute)
  end
end

#with_value_from_database(value) ⇒ Object



80
81
82
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 80

def with_value_from_database(value)
  self.class.from_database(name, value, type)
end

#with_value_from_user(value) ⇒ Object



75
76
77
78
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attribute.rb', line 75

def with_value_from_user(value)
  type.assert_valid_value(value)
  self.class.from_user(name, value, type, original_attribute || self)
end