0.0
No commit activity in last 3 years
No release in over 3 years
Defaultable is an extendable class to allow easy method chaining for settings along with defaults.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Defaultable

Defaultable allows you to create settings with defaults easily

setting = Defaultable::Settings.new
setting.site_name = 'Example'
setting.site_name # => Example

Creating your own defaultable settings class

To create a settings class, just extend Defaultable::Settings

class MySetting < Defaultable::Settings
	set_defaults :site_name => 'Example'
end

setting = MySetting.new
setting.site_name # => Example

setting.site_name = 'Different Name'
setting.site_name # => Different Name

Creating your own defaultable settings from a YAML file

class MySetting < Defaultable::Settings
	set_defaults "#{Rails.root}/config/settings.yml"
end

class MySetting < Defaultable::Settings
	set_defaults "#{Rails.root}/config/settings.yml", Rails.env
end

The second parameter of set_defaults is the environment variable you'd like to use from the file. For example "development".

Serialization

Sometimes we want to store settings for a database record in the actual schema. To do that, use the Defaultable::Serialization class. We'll use ActiveRecord::Base#serialize method in this example.

First create a model for your settings by extending Defaultable::Settings.

class UserSetting < Defaultable::Settings
  include Defaultable::Serialization
end

Now on your User model, setup the settings class on it.

class User < ActiveRecord::Base
	serialize :settings, UserSetting
end

What we just accomplished

Now we have the ability to setup user settings on creation and updating.

user = User.new
user.settings.newsletter = true
user.save


User.find(1).settings.newsletter # => true

Defaultable won't store settings that are defaults until overridden but you may use them otherwise. Meaning that it'll retain defaults on save.

Other methods you may find useful

Defaultable has some other methods you may want to use:

Defaultable::Settings#as_hash

irb(main):007:0> a.settings.as_hash
=> {"blah"=>"stuff", "foo"=>{"bar"=>"foo"}}

It returns a recursive hash of settings. It works on all levels however, such as a.settings.foo.as_hash

Defaultable::Settings#recursive_hash_assignment

irb(main):009:0> settings.recursive_hash_assignment :foo => {:bar => {:foo => 'bar'}}
=> {:foo=>{:bar=>{:foo=>"bar"}}}

irb(main):011:0> settings.foo.bar.foo
=> "bar"

And a few others, sorry for the lack of RDoc =(