Class: SerializeAttributes::Store::StoreColumnWrapper

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/serialize_attributes/store.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(original, store) ⇒ StoreColumnWrapper

rubocop:disable Lint/MissingSuper



178
179
180
181
# File 'lib/serialize_attributes/store.rb', line 178

def initialize(original, store) # rubocop:disable Lint/MissingSuper
  __setobj__(original)
  @store = store
end

Instance Method Details

#cast(value) ⇒ Object



183
184
185
186
187
188
# File 'lib/serialize_attributes/store.rb', line 183

def cast(value)
  case value
  when Hash then deserialize(value.stringify_keys)
  else value
  end
end

#changed_in_place?(raw_original_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/serialize_attributes/store.rb', line 190

def changed_in_place?(raw_original_value, new_value)
  (deserialize(raw_original_value) != new_value) || new_value.each_value.any?(&:changed_in_place?)
end

#deserializeObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/serialize_attributes/store.rb', line 198

def deserialize(...)
  result = super
  return result unless @store && result.respond_to?(:each)

  AttributeSet.new(
    @store.attribute_types.each_with_object({}) do |(attribute, type), out|
      out[attribute] = if result.key?(attribute)
                         @store.deserialize(attribute, result[attribute])
                       else
                         ActiveModel::Attribute.from_user(attribute, @store.default(attribute), type)
                       end
    end
  )
end

#serialize(value) ⇒ Object



194
195
196
# File 'lib/serialize_attributes/store.rb', line 194

def serialize(value)
  super(value.values_for_database)
end