fun_with_configurations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.3.0"
12
+ gem "jeweler", "~> 1.8.4"
13
+ gem "debugger"
14
+ end
15
+
16
+ gem "fun_with_files"
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Bryce Anderson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = fun_with_configurations
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to fun_with_configurations
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Bryce Anderson. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "fun_with_configurations"
18
+ gem.homepage = "http://github.com/darthschmoo/fun_with_configurations"
19
+ gem.license = "MIT"
20
+ gem.summary = "A quick and feisty configuration creator (e.g. MyClass.config)"
21
+ gem.description = <<-DESC
22
+ Attach a configuration object to any object or class with code like this:
23
+ spy.install_fwc_config do
24
+ spy_id "74 Baker"
25
+ DESC
26
+ gem.email = "keeputahweird@gmail.com"
27
+ gem.authors = ["Bryce Anderson"]
28
+ # dependencies defined in Gemfile
29
+
30
+ gem.files = Dir.glob( File.join( ".", "lib", "**", "*.rb" ) ) +
31
+ Dir.glob( File.join( ".", "test", "**", "*.*" ) ) +
32
+ [ "Gemfile",
33
+ "Rakefile",
34
+ "LICENSE.txt",
35
+ "README.rdoc",
36
+ "VERSION"
37
+ ]
38
+
39
+ end
40
+ Jeweler::RubygemsDotOrgTasks.new
41
+
42
+ require 'rake/testtask'
43
+ Rake::TestTask.new(:test) do |test|
44
+ test.libs << 'lib' << 'test'
45
+ test.pattern = 'test/**/test_*.rb'
46
+ test.verbose = true
47
+ end
48
+
49
+ # require 'rcov/rcovtask'
50
+ # Rcov::RcovTask.new do |test|
51
+ # test.libs << 'test'
52
+ # test.pattern = 'test/**/test_*.rb'
53
+ # test.verbose = true
54
+ # test.rcov_opts << '--exclude "gems/*"'
55
+ # end
56
+
57
+ task :default => :test
58
+
59
+ require 'rdoc/task'
60
+ Rake::RDocTask.new do |rdoc|
61
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
62
+
63
+ rdoc.rdoc_dir = 'rdoc'
64
+ rdoc.title = "fun_with_configurations #{version}"
65
+ rdoc.rdoc_files.include('README*')
66
+ rdoc.rdoc_files.include('lib/**/*.rb')
67
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,43 @@
1
+ module FunWith
2
+ module Configurations
3
+ class KeyError < Exception
4
+ end
5
+
6
+ class Config < Hash
7
+ def initialize( &block )
8
+ self.instance_exec( &block ) if block_given?
9
+ end
10
+
11
+ def method_missing( method, *args, &block )
12
+ method = method.to_s.gsub( /=$/, '' ).to_sym
13
+
14
+ if block_given?
15
+ self[method] = Config.new unless self[method].is_a?(Config)
16
+ self[method].instance_exec( &block )
17
+ elsif args.length > 0
18
+ self[method] = args.first
19
+ else
20
+ self[method]
21
+ end
22
+ end
23
+
24
+ def []( sym )
25
+ sym = sym.to_sym if sym.is_a?(String)
26
+ self.class.key_check( sym )
27
+ super( sym )
28
+ end
29
+
30
+ def []=( sym, val )
31
+ sym = sym.to_sym if sym.is_a?(String)
32
+ self.class.key_check( sym )
33
+ super( sym, val )
34
+ end
35
+
36
+ def self.key_check( sym )
37
+ @reserved_symbols ||= Hash.instance_methods
38
+ raise KeyError.new("#{sym} is not a symbol") unless sym.is_a?(Symbol)
39
+ raise KeyError.new("#{sym} is reserved for use by Hash") if @reserved_symbols.include?( sym )
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ module FunWith
2
+ module Configurations
3
+ module Configurable
4
+ def config=( config )
5
+ @fwc_config = config
6
+ end
7
+
8
+ def config( &block )
9
+ yield @fwc_config if block_given?
10
+ @fwc_config
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,11 @@
1
+ class Object
2
+ def install_fwc_config( config = nil, &block )
3
+ extend FunWith::Configurations::Configurable
4
+ self.config = config || FunWith::Configurations::Config.new( &block )
5
+ self.config
6
+ end
7
+
8
+ def install_fwc_config_from_file( filename )
9
+ install_fwc_config( eval( File.read( filename ) ) ) # TODO: Has to be a better way than eval(). Dangerous.
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require 'fun_with_files'
2
+ require_relative File.join( "fun_with", "configurations", "config" )
3
+ require_relative File.join( "fun_with", "configurations", "configurations" )
4
+ require_relative File.join( "fun_with", "configurations", "object" )
5
+
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'fun_with_configurations'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,94 @@
1
+ require 'helper'
2
+
3
+ class TestFunWithConfigurations < Test::Unit::TestCase
4
+ should "configurize a class" do
5
+ o = Object.new
6
+ o.install_fwc_config do
7
+ malaprop :sym
8
+ blocky do
9
+ fish :guts
10
+ end
11
+ end
12
+
13
+ assert_equal :sym, o.config.malaprop
14
+ assert_equal :guts, o.config.blocky.fish
15
+ end
16
+
17
+ context "testing a configurized class" do
18
+ setup do
19
+ @obj = Object.new
20
+ @obj.install_fwc_config do
21
+ a :b
22
+ b :c
23
+ c do
24
+ d :e
25
+ e :f
26
+ f :g
27
+ end
28
+ end
29
+ end
30
+
31
+ should "verify existing configuration" do
32
+ assert_equal :b, @obj.config.a
33
+ assert_equal :g, @obj.config.c.f
34
+ end
35
+
36
+ should "blockily extend configuration" do
37
+ @obj.config.c.g do
38
+ h :i
39
+ i :j
40
+ j do
41
+ k :l
42
+ m do
43
+ n :o
44
+ end
45
+ end
46
+ end
47
+
48
+ assert_equal :i, @obj.config.c.g.h
49
+ assert_equal :o, @obj.config.c.g.j.m.n
50
+ end
51
+
52
+ should "blockily extend by overwriting existing keys" do
53
+ # replace c? Additive
54
+ @obj.config.c do
55
+ q :r
56
+ end
57
+
58
+ assert_equal :b, @obj.config.a
59
+ assert_equal :g, @obj.config.c.f
60
+ assert_equal :r, @obj.config.c.q
61
+ end
62
+
63
+ should "handle equal-style assignment niftily" do
64
+ @obj.config.c = :d
65
+ assert_equal :d, @obj.config[:c]
66
+ assert_equal nil, @obj.config[:c=]
67
+ end
68
+
69
+ should "gripe when given a string" do
70
+ assert defined?( FunWith::Configurations::KeyError )
71
+ assert_raises FunWith::Configurations::KeyError do |err|
72
+ @obj.config["c"]
73
+ end
74
+ end
75
+
76
+ should "gripe when given a reserved method as a key" do
77
+ assert defined?( FunWith::Configurations::KeyError )
78
+ assert_raises FunWith::Configurations::KeyError do |err|
79
+ @obj.config[:keys] = ["22461", "80129"]
80
+ end
81
+ end
82
+ end
83
+
84
+ should "successfully create configuration manually" do
85
+ @obj = Object.new
86
+ @obj.extend( FunWith::Configurations::Configurable )
87
+ @obj.config = FunWith::Configurations::Config.new do
88
+ betelgeuse "red"
89
+ rigel "blue-white"
90
+ end
91
+
92
+ assert_equal "blue-white", @obj.config.rigel
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fun_with_configurations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryce Anderson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fun_with_files
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.3.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.4
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.4
94
+ - !ruby/object:Gem::Dependency
95
+ name: debugger
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ! "Attach a configuration object to any object or class with code like
111
+ this:\n spy.install_fwc_config do\n spy_id \"74 Baker\"\n"
112
+ email: keeputahweird@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.rdoc
118
+ files:
119
+ - ./lib/fun_with/configurations/config.rb
120
+ - ./lib/fun_with/configurations/configurations.rb
121
+ - ./lib/fun_with/configurations/object.rb
122
+ - ./lib/fun_with_configurations.rb
123
+ - ./test/helper.rb
124
+ - ./test/test_fun_with_configurations.rb
125
+ - Gemfile
126
+ - LICENSE.txt
127
+ - README.rdoc
128
+ - Rakefile
129
+ - VERSION
130
+ homepage: http://github.com/darthschmoo/fun_with_configurations
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
144
+ - 0
145
+ hash: 2567250153676636570
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.25
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: A quick and feisty configuration creator (e.g. MyClass.config)
158
+ test_files: []