Sha256: a5da75155c2f21c62b2302fec47656dc268840117eaa368024104a56ca0e8045

Contents?: true

Size: 1.3 KB

Versions: 52

Compression:

Stored size: 1.3 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 preference_cache_key(name)
      [self.class.name, name].join('::').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

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

    def method_missing(method, *args)
      name = method.to_s.gsub('=', '')
      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

52 entries across 52 versions & 2 rubygems

Version Path
spree_core-2.0.13 app/models/spree/preferences/configuration.rb
spree_core-2.0.12 app/models/spree/preferences/configuration.rb
spree_core-2.0.11 app/models/spree/preferences/configuration.rb
spree_core-2.0.10 app/models/spree/preferences/configuration.rb
spree_core-2.0.9 app/models/spree/preferences/configuration.rb
spree_core-1.3.5 app/models/spree/preferences/configuration.rb
spree_core-2.0.8 app/models/spree/preferences/configuration.rb
spree_core-2.0.7 app/models/spree/preferences/configuration.rb
spree_core-2.0.6 app/models/spree/preferences/configuration.rb
spree_core-1.3.4 app/models/spree/preferences/configuration.rb
spree_core-2.0.5 app/models/spree/preferences/configuration.rb
spree_core-2.0.4 app/models/spree/preferences/configuration.rb
spree_core-2.0.3 app/models/spree/preferences/configuration.rb
spree_core-1.3.3 app/models/spree/preferences/configuration.rb
spree_core-1.2.5 app/models/spree/preferences/configuration.rb
spree_core-2.0.2 app/models/spree/preferences/configuration.rb
spree_core-2.0.1 app/models/spree/preferences/configuration.rb
spree_core-2.0.0 app/models/spree/preferences/configuration.rb
spree_core-2.0.0.rc1 app/models/spree/preferences/configuration.rb
spree_core-1.1.6 app/models/spree/preferences/configuration.rb