Module: Foobara::Entity::Concerns::Attributes

Includes:
Concern
Included in:
Foobara::Entity
Defined in:
foobara-0.0.110/projects/entity/src/concerns/attributes.rb

Defined Under Namespace

Classes: UnexpectedPrimaryKeyChangeError

Instance Method Summary collapse

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Instance Method Details

#read_attribute(attribute_name) ⇒ Object



107
108
109
110
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 107

def read_attribute(attribute_name)
  load_if_necessary!(attribute_name)
  super
end

#read_attribute!(attribute_name) ⇒ Object



112
113
114
115
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 112

def read_attribute!(attribute_name)
  load_if_necessary!(attribute_name)
  super
end

#values_at(data_path) ⇒ Object



162
163
164
165
166
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 162

def values_at(data_path)
  data_path = DataPath.new(data_path) unless data_path.is_a?(DataPath)

  data_path.values_at(self)
end

#with_changed_attribute_callbacks(attribute_names) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 117

def with_changed_attribute_callbacks(attribute_names)
  # TODO: clean up methods to use this flag instead of calling each other
  if @callbacks_disabled
    yield
    return
  end

  attribute_names = Util.array(attribute_names)

  old_is_dirty = dirty? # TODO: don't bother with this check unless there are relevant callbacks
  old_is_valid = valid? # TODO: don't bother with this check unless there are relevant callbacks

  old_values = attribute_names.map { |attribute_name| read_attribute(attribute_name) }

  yield

  new_values = attribute_names.map { |attribute_name| read_attribute(attribute_name) }

  attribute_changed = false

  old_values.each.with_index do |old_value, index|
    new_value = new_values[index]

    if new_value != old_value
      attribute_changed = true
      fire(:attribute_changed, attribute_name: attribute_names[index], old_value:, new_value:)
    end
  end

  if attribute_changed
    new_is_dirty = dirty?

    if old_is_dirty != new_is_dirty
      old_is_dirty ? fire(:undirtied) : fire(:dirtied)
    end

    new_is_valid = valid?

    if old_is_valid != new_is_valid
      # TODO: don't bother with this check unless there are relevant callbacks
      new_is_valid ? fire(:uninvalidated) : fire(:invalidated)
    end
  end
end

#write_attribute(attribute_name, value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 15

def write_attribute(attribute_name, value)
  verify_not_hard_deleted!

  with_changed_attribute_callbacks(attribute_name) do
    load_if_necessary!(attribute_name)

    attribute_name = attribute_name.to_sym

    if attribute_name == primary_key_attribute
      if value.nil?
        # :nocov:
        raise "Primary key cannot be set to a blank value"
        # :nocov:
      end

      if value.is_a?(::String) && value.empty?
        # :nocov:
        raise "Primary key cannot be set to a blank value"
        # :nocov:
      end

      if value.is_a?(::Symbol) && value.to_s.empty?
        # :nocov:
        raise "Primary key cannot be set to a blank value"
        # :nocov:
      end

      write_attribute!(attribute_name, value)
    else
      attribute_name = attribute_name.to_sym
      # This is a bit of a problem when creating invalid models from attributes...
      # feels like we should do the best we can instead of not casting it at all. That is, better to have
      # an invalid Model instance than a hash when a Model is expected.
      # How do we go about doing that?
      outcome = cast_attribute(attribute_name, value)
      attributes[attribute_name] = outcome.success? ? outcome.result : value
    end
  end
end

#write_attribute!(attribute_name, value) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 61

def write_attribute!(attribute_name, value)
  verify_not_hard_deleted!

  with_changed_attribute_callbacks(attribute_name) do
    load_if_necessary!(attribute_name)

    attribute_name = attribute_name.to_sym

    if attribute_name == primary_key_attribute && primary_key
      outcome = cast_attribute(attribute_name, value)

      if outcome.success?
        value = outcome.result
      end

      if value != primary_key
        raise UnexpectedPrimaryKeyChangeError,
              "Primary key already set to #{primary_key}. Can't change to #{value}. " \
              "Use attributes[:#{attribute_name}] = #{value.inspect} " \
              "instead if you really want to change the primary key."
      end
    end

    attribute_name = attribute_name.to_sym
    attributes[attribute_name] = cast_attribute!(attribute_name, value)
  end
end

#write_attribute_without_callbacks(attribute_name, value) ⇒ Object



9
10
11
12
13
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 9

def write_attribute_without_callbacks(attribute_name, value)
  without_callbacks do
    write_attribute(attribute_name, value)
  end
end

#write_attribute_without_callbacks!(attribute_name, value) ⇒ Object



55
56
57
58
59
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 55

def write_attribute_without_callbacks!(attribute_name, value)
  without_callbacks do
    write_attribute!(attribute_name, value)
  end
end

#write_attributes(attributes) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 95

def write_attributes(attributes)
  verify_not_hard_deleted!

  with_changed_attribute_callbacks(attributes.keys) do
    load_if_necessary!(attributes)

    attributes.each_pair do |attribute_name, value|
      write_attribute_without_callbacks(attribute_name, value)
    end
  end
end

#write_attributes_without_callbacks(attributes) ⇒ Object



89
90
91
92
93
# File 'foobara-0.0.110/projects/entity/src/concerns/attributes.rb', line 89

def write_attributes_without_callbacks(attributes)
  without_callbacks do
    write_attributes(attributes)
  end
end