module_ext 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .rvmrc
5
+ *.sw[a|o|p]
6
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in module_ext.gemspec
4
+ gemspec
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ module_ext (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ module_ext!
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Ryan Cook and Quick Left, Inc.
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.
21
+
@@ -0,0 +1,4 @@
1
+ #ModuleExt
2
+
3
+ Convenience module extensions. Currently only monkey-patches the Module
4
+ class to allow for the use of mattr_* functions.
@@ -0,0 +1,12 @@
1
+ require 'rake/testtask'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ desc 'Gotta run those tests'
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = true
11
+ end
12
+
@@ -0,0 +1,6 @@
1
+ module ModuleExt
2
+
3
+ require 'module_ext/attribute_accessors'
4
+
5
+ end
6
+
@@ -0,0 +1,61 @@
1
+ class Module
2
+
3
+ def mattr_reader(*syms)
4
+ options = ( syms.last.is_a?(Hash) ) ? syms.pop : {}
5
+ syms.each do |sym|
6
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
7
+ @@#{sym} = nil unless defined? @@#{sym}
8
+
9
+ def self.#{sym}
10
+ @@#{sym}
11
+ end
12
+ EOS
13
+
14
+ unless options[:instance_reader] == false
15
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
16
+ def #{sym}
17
+ @@#{sym}
18
+ end
19
+ EOS
20
+ end
21
+ end
22
+ end
23
+
24
+ def mattr_writer(*syms)
25
+ options = ( syms.last.is_a?(Hash) ) ? syms.pop : {}
26
+ syms.each do |sym|
27
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
28
+ def self.#{sym}=(obj)
29
+ @@#{sym} = obj
30
+ end
31
+ EOS
32
+
33
+ unless options[:instance_writer] == false
34
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
35
+ def #{sym}=(obj)
36
+ @@#{sym} = obj
37
+ end
38
+ EOS
39
+ end
40
+ end
41
+ end
42
+
43
+ # Extends the module object with module and instance accessors for class attributes,
44
+ # just like the native attr* accessors for instance attributes.
45
+ #
46
+ # module AppConfiguration
47
+ # mattr_accessor :google_api_key
48
+ # self.google_api_key = "123456789"
49
+ #
50
+ # mattr_accessor :paypal_url
51
+ # self.paypal_url = "www.sandbox.paypal.com"
52
+ # end
53
+ #
54
+ # AppConfiguration.google_api_key = "overriding the api key!"
55
+ def mattr_accessor(*syms)
56
+ mattr_reader(*syms)
57
+ mattr_writer(*syms)
58
+ end
59
+
60
+ end # class Module
61
+
@@ -0,0 +1,3 @@
1
+ module ModuleExt
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "module_ext/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "module_ext"
7
+ s.version = ModuleExt::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ryan Cook"]
10
+ s.email = ["ryan@quickleft.com"]
11
+ s.homepage = "https://github.com/cookrn/module_ext"
12
+ s.summary = %q{Convenience module extensions.}
13
+ s.description = %q{Convenience module extensions.}
14
+
15
+ s.rubyforge_project = "module_ext"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,41 @@
1
+ require 'test/unit'
2
+ require 'module_ext'
3
+
4
+ class ModuleAttributeAccessorTest < Test::Unit::TestCase
5
+ def setup
6
+ m = @module = Module.new do
7
+ mattr_accessor :foo
8
+ mattr_accessor :bar, :instance_writer => false
9
+ mattr_reader :shaq, :instance_reader => false
10
+ end
11
+ @class = Class.new
12
+ @class.instance_eval { include m }
13
+ @object = @class.new
14
+ end
15
+
16
+ def test_should_use_mattr_default
17
+ assert_nil @module.foo
18
+ assert_nil @object.foo
19
+ end
20
+
21
+ def test_should_set_mattr_value
22
+ @module.foo = :test
23
+ assert_equal :test, @object.foo
24
+
25
+ @object.foo = :test2
26
+ assert_equal :test2, @module.foo
27
+ end
28
+
29
+ def test_should_not_create_instance_writer
30
+ assert_respond_to @module, :foo
31
+ assert_respond_to @module, :foo=
32
+ assert_respond_to @object, :bar
33
+ assert !@object.respond_to?(:bar=)
34
+ end
35
+
36
+ def test_should_not_create_instance_reader
37
+ assert_respond_to @module, :shaq
38
+ assert !@object.respond_to?(:shaq)
39
+ end
40
+ end
41
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: module_ext
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Ryan Cook
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-08 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Convenience module extensions.
23
+ email:
24
+ - ryan@quickleft.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - lib/module_ext.rb
39
+ - lib/module_ext/attribute_accessors.rb
40
+ - lib/module_ext/version.rb
41
+ - module_ext.gemspec
42
+ - test/attribute_accessor_test.rb
43
+ has_rdoc: true
44
+ homepage: https://github.com/cookrn/module_ext
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project: module_ext
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Convenience module extensions.
77
+ test_files:
78
+ - test/attribute_accessor_test.rb