Class: SerializeAttributes::Store

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

Overview

SerializeAttributes::Store is the individual store, keyed by name. You can get a reference to the store by calling ‘Model.serialized_attributes_store(column_name)`.

Defined Under Namespace

Classes: ArrayWrapper, AttributeSet, StoreColumnWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, column_name, &block) ⇒ Store

:nodoc:



7
8
9
10
11
12
13
14
15
16
# File 'lib/serialize_attributes/store.rb', line 7

def initialize(model_class, column_name, &block) # :nodoc:
  @model_class = model_class
  @column_name = column_name
  @attribute_types = {}
  @defaults = {}

  instance_exec(&block)
  wrap_store_column
  [self, @attribute_types, @defaults].each(&:freeze)
end

Instance Attribute Details

#attribute_typesObject (readonly)

Returns the value of attribute attribute_types.



18
19
20
# File 'lib/serialize_attributes/store.rb', line 18

def attribute_types
  @attribute_types
end

Instance Method Details

#attribute_names(type: nil, array: nil) ⇒ Object

Get a list of the attributes managed by this store. Pass an optional ‘type` argument to filter attributes by their type.

 Model.serialized_attributes_store(:settings).attribute_names
 => [:user_name, :subscribed, :subscriptions]

 Model.serialized_attributes_store(:settings).attribute_names(type: :string)
 => [:user_name, :subscriptions]

Model.serialized_attributes_store(:settings).attribute_names(type: :string, array: true)
=> [:subscriptions]

Model.serialized_attributes_store(:settings).attribute_names(type: :string, array: false)
=> [:user_name]


36
37
38
39
40
41
42
43
44
# File 'lib/serialize_attributes/store.rb', line 36

def attribute_names(type: nil, array: nil)
  attributes = @attribute_types
  attributes = @attribute_types.select { |_, v| v.is_a?(ArrayWrapper) == array } unless array.nil?
  if type
    attributes_for_type(attributes, type)
  else
    attributes
  end.keys.map(&:to_sym)
end

#cast(name, value) ⇒ Object

Cast a stored attribute against a given name into an ActiveModel::Attribute::FromUser object (the cast value can be got using ‘#value`).

Model.serialized_attributes_store(:settings).cast(:user_name, 42).value
=> "42"


62
63
64
65
66
# File 'lib/serialize_attributes/store.rb', line 62

def cast(name, value)
  type = @attributes_types.fetch(name.to_s)

  ActiveModel::Attribute.from_user(name, value, type)
end

#default(name) ⇒ Object

Retrieve the default value for a given block.

Model.serialized_attributes_store(:settings).default(:subscribed)
#=> false


87
88
89
# File 'lib/serialize_attributes/store.rb', line 87

def default(name)
  @defaults[name.to_s]
end

#deserialize(name, value) ⇒ Object

Deserialize a stored attribute using the value from the database (or elsewhere) into an ActiveModel::Attribute::FromDatabase object (the cast value can be got using ‘#value`).

Model.serialized_attributes_store(:settings).deserialize(:subscribed, "0").value
=> false


74
75
76
77
78
79
80
81
# File 'lib/serialize_attributes/store.rb', line 74

def deserialize(name, value)
  type = @attribute_types[name.to_s]
  if type.nil?
    raise "The attribute #{name} is not defined in serialize_attribute method in the #{@model_class} class."
  end

  ActiveModel::Attribute.from_database(name, value, type)
end

#enum_options(name) ⇒ Object

Get a list of enumerated options for the column ‘name` in this store.

Model.serialized_attributes_store(:settings).enum_options(:enumy)
=> [nil, "placed", "confirmed"]

Raises:

  • (ArgumentError)


50
51
52
53
54
55
# File 'lib/serialize_attributes/store.rb', line 50

def enum_options(name)
  type = @attribute_types.fetch(name.to_s)
  raise ArgumentError, "`#{name}` attribute is not an enum type" unless type.respond_to?(:options)

  type.options
end