Class: Mobility::Plugins::Default
- Inherits:
-
Module
- Object
- Module
- Mobility::Plugins::Default
- 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.
Class Method Summary collapse
-
.apply(attributes, option) ⇒ Object
Applies default plugin to attributes.
Instance Method Summary collapse
-
#initialize(default_option) ⇒ Default
constructor
A new instance of Default.
Constructor Details
#initialize(default_option) ⇒ Default
Returns a new instance of Default.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mobility/plugins/default.rb', line 61 def initialize(default_option) define_method :read do |locale, = {}| default = .has_key?(:default) ? .delete(:default) : default_option if (value = super(locale, )).nil? default.is_a?(Proc) ? default.call(model: model, attribute: attribute) : default else value end end end |
Class Method Details
.apply(attributes, option) ⇒ Object
Applies default plugin to attributes.
57 58 59 |
# File 'lib/mobility/plugins/default.rb', line 57 def self.apply(attributes, option) attributes.backend_class.include(new(option)) end |