Class: ActiveRecord::TypedStore::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/typed_store/dsl.rb

Constant Summary collapse

NO_DEFAULT_GIVEN =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_name, options) {|_self| ... } ⇒ DSL

Returns a new instance of DSL.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
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
# File 'lib/active_record/typed_store/dsl.rb', line 9

def initialize(store_name, options)
  @coder = options.fetch(:coder) { default_coder(store_name) }
  @store_name = store_name
  @prefix =
    case options[:prefix]
    when String, Symbol
      "#{options[:prefix]}_"
    when true
      "#{store_name}_"
    when false, nil
      ""
    else
      raise ArgumentError, "Unexpected type for prefix option. Expected string, symbol, or boolean"
    end
  @suffix =
    case options[:suffix]
    when String, Symbol
      "_#{options[:suffix]}"
    when true
      "_#{store_name}"
    when false, nil
      ""
    else
      raise ArgumentError, "Unexpected type for suffix option. Expected string, symbol, or boolean"
    end
  @accessors = if options[:accessors] == false
    {}
  elsif options[:accessors].is_a?(Array)
    options[:accessors].each_with_object({}) do |accessor_name, hash|
      hash[accessor_name] = accessor_key_for(accessor_name)
    end
  end
  @fields = {}
  yield self
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder.



7
8
9
# File 'lib/active_record/typed_store/dsl.rb', line 7

def coder
  @coder
end

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/active_record/typed_store/dsl.rb', line 7

def fields
  @fields
end

Instance Method Details

#accessorsObject



55
56
57
58
59
# File 'lib/active_record/typed_store/dsl.rb', line 55

def accessors
  @accessors || @fields.values.select(&:accessor).each_with_object({}) do |field, hash|
    hash[field.name] = accessor_key_for(field.name)
  end
end

#default_coder(attribute_name) ⇒ Object



46
47
48
# File 'lib/active_record/typed_store/dsl.rb', line 46

def default_coder(attribute_name)
  ActiveRecord::Coders::YAMLColumn.new
end