Giter Site home page Giter Site logo

rpheath / acts_as_configurable Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 90 KB

Rails plugin: supports dynamic configuration for any class or model (does not depend on ActiveRecord).

Home Page: http://rpheath.com/posts/357-rails-plugin-acts-as-configurable

License: MIT License

Ruby 100.00%

acts_as_configurable's Introduction

ActsAsConfigurable

Rails plugin that adds dynamic (non-database) configuration settings to any model or class.

Examples

Basic usage:


class User < ActiveRecord::Base
  acts_as_configurable
  
  configuration do |config|
    config.passwords.min_length = 6
    config.passwords.max_length = 12  
  end
end

And then to access that configuration, simply call:


$> User.configuration
$> => {:passwords => { :min_length => 6, :max_length => 12 }}

Or just keep going down the chain to get to the specifics:


$> User.configuration.passwords
$> => {:min_length => 6, :max_length => 12}
$> User.configuration.passwords.min_length
$> => 6

Now, granted that a “configuration” class method is common enough that there may be a case
where you have a conflict. Well, you can also configure that part:


class User < ActiveRecord::Base
  acts_as_configurable :with => :settings
  
  settings do |setting|
    setting.passwords.min_length = 6
    setting.passwords.max_length = 12
  end
end

Then just use “settings” in place of “configuration” when accessing the data.


$> User.settings.passwords
$> => {:min_length => 6, :max_length => 12}

Nested configuration

You can also nest configuration settings to keep things grouped nicely.


class User < ActiveRecord::Base
  acts_as_configurable
  
  configuration do |config|
    config.passwords do |p|
      p.min_length = 6
      p.max_length = 12
    end
    config.accounts do |a|
      # ...
    end
  end
end

Dependencies

This plugin mixes directly into Object, so there’s no dependency on another library. While the
examples above are all ActiveRecord models, they don’t have to be. Any class can act as configurable.

Actually, ActiveSupport is required (thanks to class_inheritable_accessor), but since this is a
plugin for a Rails application, there shouldn’t be any issues there.

Credits

Chris Scharf is the original author of this “on-the-fly” configuration.
His original implementation was intended for application wide configuration settings. You can find
his post about it here.

With my version, you can have the convenience of his configuration, but exclusive to the class in
which it’s contained. Plus it’s now a plugin.

© 2008 Ryan Heath and Chris Scharf, released under the MIT license

acts_as_configurable's People

Contributors

rpheath avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mpapis

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.