Sha256: 6da4e2717bf2f5bb781ba616c70d2cddf0d349c962c527195e1a80433183f341

Contents?: true

Size: 1.26 KB

Versions: 39

Compression:

Stored size: 1.26 KB

Contents

# This takes the preferrable methods and adds some
# syntatic sugar to access the preferences
#
# class App < Configuration
#   preference :color, :string
# end
#
# a = App.new
#
# setters:
# a.color = :blue
# a[:color] = :blue
# a.set :color = :blue
# a.preferred_color = :blue
#
# getters:
# a.color
# a[:color]
# a.get :color
# a.preferred_color
#
#
module Spree::Preferences
  class Configuration
    include Spree::Preferences::Preferable

    def configure
      yield(self) if block_given?
    end

    def preferences
      ScopedStore.new(self.class.name.underscore)
    end

    def reset
      preferences.each do |name, _value|
        set_preference name, preference_default(name)
      end
    end

    alias [] get_preference
    alias []= set_preference

    alias get get_preference

    def set(*args)
      options = args.extract_options!
      options.each do |name, value|
        set_preference name, value
      end

      set_preference args[0], args[1] if args.size == 2
    end

    def method_missing(method, *args)
      name = method.to_s.delete('=')
      if has_preference? name
        if method.to_s =~ /=$/
          set_preference(name, args.first)
        else
          get_preference name
        end
      else
        super
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
spree_core-3.7.14.1 app/models/spree/preferences/configuration.rb
spree_core-3.7.14 app/models/spree/preferences/configuration.rb
spree_core-3.7.13 app/models/spree/preferences/configuration.rb
spree_core-3.7.12 app/models/spree/preferences/configuration.rb
spree_core-3.7.11 app/models/spree/preferences/configuration.rb
spree_core-3.4.6 app/models/spree/preferences/configuration.rb
spree_core-3.7.10 app/models/spree/preferences/configuration.rb
spree_core-3.7.9 app/models/spree/preferences/configuration.rb
spree_core-3.7.8 app/models/spree/preferences/configuration.rb
spree_core-3.7.7 app/models/spree/preferences/configuration.rb
spree_core-3.7.6 app/models/spree/preferences/configuration.rb
spree_core-3.7.5 app/models/spree/preferences/configuration.rb
spree_core-3.7.4 app/models/spree/preferences/configuration.rb
spree_core-3.7.3 app/models/spree/preferences/configuration.rb
spree_core-3.7.2 app/models/spree/preferences/configuration.rb
spree_core-3.7.1 app/models/spree/preferences/configuration.rb
spree_core-3.7.0 app/models/spree/preferences/configuration.rb
spree_core-3.7.0.rc3 app/models/spree/preferences/configuration.rb
spree_core-3.7.0.rc2 app/models/spree/preferences/configuration.rb
spree_core-3.7.0.rc1 app/models/spree/preferences/configuration.rb