Module: Mobility::Plugins::Default
- Extended by:
- Mobility::Plugin
- Defined in:
- lib/mobility/plugins/default.rb
Overview
Defines value or proc to fall through to if return value from getter would otherwise be nil. This plugin is disabled by default but will be enabled if any value is passed as the default
option key.
If default is a Proc
, it will be called with the context of the model, and passed arguments:
-
the attribute name (a String)
-
the locale (a Symbol)
-
hash of options passed in to accessor
The proc can accept zero to three arguments (see examples below)
Defined Under Namespace
Modules: BackendMethods
Class Method Summary collapse
-
.[](default_value, locale:, accessor_options:, model:, attribute:) ⇒ Object
Generate a default value for given parameters.
Methods included from Mobility::Plugin
configure, configure_default, default, dependencies, dependencies_satisfied?, included, included_hook, initialize_hook, requires
Class Method Details
.[](default_value, locale:, accessor_options:, model:, attribute:) ⇒ Object
Generate a default value for given parameters.
79 80 81 82 83 84 |
# File 'lib/mobility/plugins/default.rb', line 79 def self.[](default_value, locale:, accessor_options:, model:, attribute:) return default_value unless default_value.is_a?(Proc) args = [attribute, locale, ] args = args.first(default_value.arity) unless default_value.arity < 0 model.instance_exec(*args, &default_value) end |