defaultable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --drb
3
+ --format=nested
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in defaultable.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Defaultable
2
+ #### Defaultable allows you to create settings with defaults easily
3
+
4
+ ```
5
+ setting = Defaultable::Settings.new
6
+ setting.site_name = 'Example'
7
+ setting.site_name # => Example
8
+ ```
9
+
10
+ ## Creating your own defaultable settings class
11
+
12
+ To create a settings class, just extend Defaultable::Settings
13
+
14
+ ```
15
+ class MySetting < Defaultable::Settings
16
+ set_defaults :site_name => 'Example'
17
+ end
18
+
19
+ setting = MySetting.new
20
+ setting.site_name # => Example
21
+
22
+ setting.site_name = 'Different Name'
23
+ setting.site_name # => Different Name
24
+ ```
25
+
26
+ ## Creating your own defaultable settings from a YAML file
27
+
28
+ ```
29
+ class MySetting < Defaultable::Settings
30
+ set_defaults "#{Rails.root}/config/settings.yml"
31
+ end
32
+
33
+ class MySetting < Defaultable::Settings
34
+ set_defaults "#{Rails.root}/config/settings.yml", Rails.env
35
+ end
36
+
37
+ ```
38
+
39
+ The second parameter of ```set_defaults``` is the environment variable you'd like to use from the file.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/defaultable/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Robert Ross"]
6
+ gem.email = ["robert@maintainedauto.com"]
7
+ gem.description = %q{Defaultable is an extendable class to allow easy method chaining for settings along with defaults.}
8
+ gem.summary = %q{Settings made easy.}
9
+ gem.homepage = "https://github.com/bobbytables/defaultable"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "defaultable"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Defaultable::VERSION
17
+
18
+ gem.add_dependency("activesupport", "~> 3.1")
19
+
20
+ gem.add_development_dependency("rspec", "~> 2.7")
21
+ gem.add_development_dependency("pry")
22
+ end
@@ -0,0 +1,87 @@
1
+ require "defaultable/version"
2
+ require 'yaml'
3
+ require 'active_support/core_ext/class/attribute'
4
+
5
+ module Defaultable
6
+ class Settings
7
+ class_attribute :defaults
8
+ attr_accessor :root_key, :parent
9
+
10
+ def initialize(hash=nil, root_key=nil, parent=nil)
11
+ @table = {}
12
+
13
+ if root_key
14
+ self.root_key = root_key
15
+ end
16
+
17
+ if parent
18
+ self.parent = parent
19
+ end
20
+
21
+ if !hash.nil? && hash.kind_of?(Hash)
22
+ hash.each do |key, value|
23
+ if value.kind_of?(Hash)
24
+ send("#{key}=", Defaultable::Settings.new(value, key, self))
25
+ else
26
+ self.send("#{key}=", value)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def method_missing(message, *args, &block)
33
+ # puts "Message: #{message}", "Args: #{args}", "\n"
34
+
35
+ if !(message.to_s =~ /^(.*)=$/) && @table.has_key?(message.to_s)
36
+ @table[message.to_s]
37
+ elsif message.to_s =~ /^(.*)=$/
38
+ @table[message.to_s.gsub(/=$/, '')] = args.first
39
+ elsif !(message.to_s =~ /^(.*)=$/)
40
+ # We're looking for a default
41
+ default_settings = self.class.defaults
42
+
43
+ # If we have a parent, we need to traverse to the top of all of the method calls (root_keys), then go back down them
44
+ if self.parent
45
+ root_keys = []
46
+ current_parent = self
47
+ while current_parent.has_parent? do
48
+ root_keys << current_parent.root_key
49
+ current_parent = current_parent.parent
50
+ end
51
+
52
+ root_keys.reverse.each do |key|
53
+ default_settings = default_settings.send(key)
54
+ end
55
+
56
+ return default_settings.send(message)
57
+ # If don't have a parent, just send the message to the defaults outright
58
+ else
59
+ default_settings.send(message)
60
+ end
61
+ end
62
+ end
63
+
64
+ def as_hash
65
+ @table
66
+ end
67
+
68
+ def has_parent?
69
+ !!self.parent
70
+ end
71
+
72
+ class << self
73
+ def set_defaults(settings, env=nil)
74
+ case settings
75
+ when Hash
76
+ self.defaults = Defaultable::Settings.new(settings)
77
+ when String
78
+ yaml = YAML.load_file(settings)
79
+ yaml = yaml[env] if env
80
+ self.defaults = Defaultable::Settings.new(yaml)
81
+ else
82
+ raise ArgumentError, "You must supply a hash or a file name for default settings"
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module Defaultable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe Defaultable::Settings do
4
+ before :each do
5
+ Defaultable::Settings.set_defaults({})
6
+ end
7
+
8
+ it "should not have a parent" do
9
+ setting = Defaultable::Settings.new
10
+ setting.has_parent?.should be_false
11
+ end
12
+
13
+ it "should initialize with a hash" do
14
+ setting = Defaultable::Settings.new({:child => 'Bobert'})
15
+ setting.child.should eq('Bobert')
16
+ end
17
+
18
+ it "should be able to set nonexsistent keys" do
19
+ setting = Defaultable::Settings.new
20
+ setting.child = 'Bri Bri'
21
+ setting.child.should eq('Bri Bri')
22
+ end
23
+
24
+ it "should be able to set nonexsistent keys to another setting" do
25
+ setting = Defaultable::Settings.new
26
+ setting.child = Defaultable::Settings.new
27
+ setting.child.name = 'Mocha'
28
+ setting.child.name.should eq('Mocha')
29
+ end
30
+
31
+ it "should accept nested hashes" do
32
+ setting = Defaultable::Settings.new({:parent => {:child => 'Rob'}})
33
+ setting.parent.child.should eq('Rob')
34
+ end
35
+
36
+ it "should have default settings" do
37
+ Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
38
+ Defaultable::Settings.defaults.should be_kind_of(Defaultable::Settings)
39
+ end
40
+
41
+ it "should have a default setting for a key" do
42
+ Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
43
+
44
+ setting = Defaultable::Settings.new
45
+ setting.grandparent.should be_kind_of Defaultable::Settings
46
+ end
47
+
48
+ it "should have another key for another setting" do
49
+ Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
50
+
51
+ setting = Defaultable::Settings.new
52
+ setting.grandparent.child.grandchild1.should eq('robert')
53
+ end
54
+
55
+ it "should set a key but still have defaults" do
56
+ Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
57
+
58
+ setting = Defaultable::Settings.new({
59
+ :grandparent => {
60
+ :child => {
61
+ :grandchild3 => 'hurdur'
62
+ }
63
+ }
64
+ })
65
+
66
+ setting.grandparent.child.grandchild3.should eq('hurdur')
67
+ setting.grandparent.child.grandchild2.should eq('brian')
68
+ end
69
+
70
+ it "should be able to overwrite a default" do
71
+ Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
72
+
73
+ setting = Defaultable::Settings.new({
74
+ :grandparent => {
75
+ :child => {
76
+ :grandchild3 => 'hurdur'
77
+ }
78
+ }
79
+ })
80
+
81
+ setting.grandparent.child.grandchild3.should eq('hurdur')
82
+ setting.grandparent.child.grandchild2 = 'drpepper'
83
+ setting.grandparent.child.grandchild2.should eq('drpepper')
84
+ end
85
+
86
+ it "should be able to set a key in the middle of defaults" do
87
+ Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
88
+
89
+ setting = Defaultable::Settings.new({
90
+ :grandparent => {
91
+ :child => {
92
+ :grandchild3 => 'hurdur'
93
+ }
94
+ }
95
+ })
96
+
97
+ setting.grandparent.child.grandchild3.should eq('hurdur')
98
+ setting.grandparent.someotherkey = 'saweet'
99
+ setting.grandparent.someotherkey.should eq('saweet')
100
+ end
101
+
102
+ it "should be able to set a hash for defaults" do
103
+ Defaultable::Settings.set_defaults :child => 'sxephil'
104
+
105
+ setting = Defaultable::Settings.new
106
+ setting.child.should eq('sxephil')
107
+ end
108
+
109
+ it "should accept a filename with an environment" do
110
+ Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/env_test.yml', 'development'
111
+
112
+ setting = Defaultable::Settings.new
113
+ setting.setting_key.should eq('somevalue')
114
+ end
115
+
116
+ it "should be extendable" do
117
+ class DummySetting < Defaultable::Settings
118
+ set_defaults :movie => 'Iron Man'
119
+ end
120
+
121
+ setting = DummySetting.new
122
+ setting.movie.should eq('Iron Man')
123
+ end
124
+
125
+ describe "Hashes" do
126
+ it ".as_hash should return a hash" do
127
+ Defaultable::Settings.set_defaults :child => 'sxephil'
128
+ setting = Defaultable::Settings.new
129
+
130
+ setting.name = 'Robert'
131
+ setting.as_hash.should be_kind_of Hash
132
+ end
133
+
134
+ it ".as_hash should return a hash with the correct keys" do
135
+ Defaultable::Settings.set_defaults :child => 'sxephil'
136
+ setting = Defaultable::Settings.new
137
+
138
+ setting.name = 'Robert'
139
+ setting.as_hash['name'].should eq('Robert')
140
+ end
141
+ end
142
+ end
data/spec/env_test.yml ADDED
@@ -0,0 +1,2 @@
1
+ development:
2
+ setting_key: somevalue
@@ -0,0 +1,5 @@
1
+ require 'defaultable'
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rspec
5
+ end
data/spec/test.yml ADDED
@@ -0,0 +1,4 @@
1
+ grandparent:
2
+ child:
3
+ grandchild1: robert
4
+ grandchild2: brian
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: defaultable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Ross
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70195091793420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70195091793420
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70195091791660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.7'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70195091791660
36
+ - !ruby/object:Gem::Dependency
37
+ name: pry
38
+ requirement: &70195091790800 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70195091790800
47
+ description: Defaultable is an extendable class to allow easy method chaining for
48
+ settings along with defaults.
49
+ email:
50
+ - robert@maintainedauto.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .rspec
57
+ - Gemfile
58
+ - README.md
59
+ - Rakefile
60
+ - defaultable.gemspec
61
+ - lib/defaultable.rb
62
+ - lib/defaultable/version.rb
63
+ - spec/defaultable_spec.rb
64
+ - spec/env_test.yml
65
+ - spec/spec_helper.rb
66
+ - spec/test.yml
67
+ homepage: https://github.com/bobbytables/defaultable
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.11
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Settings made easy.
91
+ test_files:
92
+ - spec/defaultable_spec.rb
93
+ - spec/env_test.yml
94
+ - spec/spec_helper.rb
95
+ - spec/test.yml