Class: Mobility::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mobility/configuration.rb

Overview

Stores shared Mobility configuration referenced by all backends.

Defined Under Namespace

Classes: Options, ReservedOptionKey

Constant Summary collapse

RESERVED_OPTION_KEYS =
%i[backend model_class].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mobility/configuration.rb', line 103

def initialize
  @accessor_method = :translates
  @query_method = :i18n
  @fallbacks_generator = lambda { |fallbacks| Mobility::Fallbacks.build(fallbacks) }
  @default_accessor_locales = lambda { Mobility.available_locales }
  @default_options = Options[{
    cache:    true,
    presence: true,
    query:    true,
    default:  Plugins::OPTION_UNSET
  }]
  @plugins = %i[
    query
    cache
    dirty
    fallbacks
    presence
    default
    attribute_methods
    fallthrough_accessors
    locale_accessors
  ]
end

Instance Attribute Details

#accessor_methodSymbol

Alias for mobility_accessor (defaults to translates)

Returns:

  • (Symbol)

14
15
16
# File 'lib/mobility/configuration.rb', line 14

def accessor_method
  @accessor_method
end

#default_accessor_localesArray<Symbol>

Returns set of default accessor locles to use (defaults to I18n.available_locales)

Returns:

  • (Array<Symbol>)

94
95
96
97
98
99
100
# File 'lib/mobility/configuration.rb', line 94

def default_accessor_locales
  if @default_accessor_locales.is_a?(Proc)
    @default_accessor_locales.call
  else
    @default_accessor_locales
  end
end

#default_backendSymbol, Class

Default backend to use (can be symbol or actual backend class)

Returns:

  • (Symbol, Class)

89
90
91
# File 'lib/mobility/configuration.rb', line 89

def default_backend
  @default_backend
end

#default_optionsHash

Default set of options. These will be merged with any backend options when defining translated attributes (with translates). Default options may not include the keys 'backend' or 'model_class'.

Returns:

  • (Hash)

24
25
26
# File 'lib/mobility/configuration.rb', line 24

def default_options
  @default_options
end

#fallbacks_generator=(value) ⇒ Object (writeonly)

Assign proc which, passed a set of fallbacks, returns a default fallbacks instance. By default this is a proc which takes fallbacks and returns an instance of I18n::Locale::Fallbacks.

Parameters:

  • fallbacks (Proc)

    generator


65
66
67
# File 'lib/mobility/configuration.rb', line 65

def fallbacks_generator=(value)
  @fallbacks_generator = value
end

#pluginsArray<Symbol>

Plugins to apply. Order of plugins is important, as this becomes the order in which plugins modules are included into the backend class or attributes instance.

Returns:

  • (Array<Symbol>)

48
49
50
# File 'lib/mobility/configuration.rb', line 48

def plugins
  @plugins
end

#query_methodSymbol

Name of query scope/dataset method (defaults to i18n)

Returns:

  • (Symbol)

18
19
20
# File 'lib/mobility/configuration.rb', line 18

def query_method
  @query_method
end

Instance Method Details

#default_fallbacks(fallbacks = {}) ⇒ Object

Deprecated.

Use #new_fallbacks instead.


68
69
70
71
72
73
74
75
# File 'lib/mobility/configuration.rb', line 68

def default_fallbacks(fallbacks = {})
  warn %{
WARNING: The default_fallbacks configuration getter has been renamed
new_fallbacks to avoid confusion. The original method default_fallbacks will be
removed in the next major version of Mobility.
}
  new_fallbacks(fallbacks)
end

#default_fallbacks=(fallbacks) ⇒ Object

Deprecated.

Use #fallbacks_generator= instead.


78
79
80
81
82
83
84
85
# File 'lib/mobility/configuration.rb', line 78

def default_fallbacks=(fallbacks)
  warn %{
WARNING: The default_fallbacks= configuration setter has been renamed
fallbacks_generator= to avoid confusion. The original method
default_fallbacks= will be removed in the next major version of Mobility.
}
  self.fallbacks_generator = fallbacks
end

#new_fallbacks(fallbacks = {}) ⇒ I18n::Locale::Fallbacks

Note:

This method will call the proc defined in the variable set by the

Generate new fallbacks instance fallbacks_generator= setter, passing the first argument to its `call` method. By default the generator returns an instance of I18n::Locale::Fallbacks.

Parameters:

  • fallbacks (Hash) (defaults to: {})

    Fallbacks hash passed to generator

Returns:

  • (I18n::Locale::Fallbacks)

57
58
59
# File 'lib/mobility/configuration.rb', line 57

def new_fallbacks(fallbacks = {})
  @fallbacks_generator.call(fallbacks)
end