begetter 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +60 -0
- data/Rakefile +6 -0
- data/begetter.gemspec +21 -0
- data/bin/console +7 -0
- data/lib/begetter.rb +19 -0
- data/lib/begetter/error.rb +4 -0
- data/lib/begetter/getter.rb +15 -0
- data/lib/begetter/version.rb +3 -0
- metadata +99 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 2c8f4c999af87cc86a42e93ee8f0c20057648404
|
|
4
|
+
data.tar.gz: a23e7c5e52402fc33dc2c72323b9fb4ef6be3ab7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: af1837dd72ddafa6033e2927e5058005f9f0f96776fbd1ae41fbe5bbb443d737e8668c5fa0b29dfc39a9d05e7ce73722aebf94a63d6657d4cafa1fbb6d990d5d
|
|
7
|
+
data.tar.gz: 9690be1b97a0892e0320fc8cacab0f796283c0d755d7b137458ec746c4db7789888f7c240c32567081ef865b18ce474ae23b9ebe1031a325facdb8e7e1ac03fb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2015 Jessica Zahnd
|
|
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.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Begetter [](https://travis-ci.org/jzahnd/begetter)
|
|
2
|
+
|
|
3
|
+
Begetter is a teeny tiny little gem that creates a class constant from a string based name. It is inspired by ActiveSupport's `constantize` but with-out the ActiveSupport girth.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'begetter'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install begetter
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
Require Begetter:
|
|
23
|
+
```ruby
|
|
24
|
+
require 'begetter'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Call `Begetter.get` with the name of the class you wish to reference:
|
|
28
|
+
```ruby
|
|
29
|
+
class FooBar; end
|
|
30
|
+
|
|
31
|
+
klass_name = 'FooBar'
|
|
32
|
+
klass = Begetter.get(klass_name)
|
|
33
|
+
|
|
34
|
+
foo_bar = klass.new
|
|
35
|
+
#=> FooBar
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Classes with-in modules work as well:
|
|
39
|
+
```ruby
|
|
40
|
+
module Foo
|
|
41
|
+
class Bar; end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
klass_name = 'Foo::Bar'
|
|
45
|
+
klass = Begetter.get(klass_name)
|
|
46
|
+
|
|
47
|
+
bar = klass.new
|
|
48
|
+
#=> Foo::Bar
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
That's it!
|
|
52
|
+
|
|
53
|
+
## Why?
|
|
54
|
+
|
|
55
|
+
Do we really need an entire gem to do this? After no less than 3 projects that had heavily dynamic architecture, I found myself building this functionality as a module into each one. There have to to be others who will find this moderately useful.
|
|
56
|
+
|
|
57
|
+
## Contributing
|
|
58
|
+
|
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jzahnd/begetter.
|
|
60
|
+
|
data/Rakefile
ADDED
data/begetter.gemspec
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'begetter/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'begetter'
|
|
8
|
+
spec.version = Begetter::VERSION
|
|
9
|
+
spec.licenses = ['MIT']
|
|
10
|
+
spec.authors = ['Jessica Zahnd']
|
|
11
|
+
spec.email = ['jessica@jessicazahnd.com']
|
|
12
|
+
spec.summary = 'Begetter creates a reference to a class constant from a string.'
|
|
13
|
+
spec.homepage = 'http://github.com/jzahnd/begetter'
|
|
14
|
+
|
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
|
16
|
+
spec.require_paths = ['lib']
|
|
17
|
+
|
|
18
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
|
19
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
20
|
+
spec.add_development_dependency 'rspec', '~> 3.3.0'
|
|
21
|
+
end
|
data/bin/console
ADDED
data/lib/begetter.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'begetter/version'
|
|
2
|
+
require 'begetter/error'
|
|
3
|
+
require 'begetter/getter'
|
|
4
|
+
|
|
5
|
+
module Begetter
|
|
6
|
+
# Get a reference to a Class from a string
|
|
7
|
+
#
|
|
8
|
+
# @param [String] klass_name name of the class being referenced
|
|
9
|
+
#
|
|
10
|
+
# @return [Class] Constant reference to klass_name
|
|
11
|
+
#
|
|
12
|
+
# @example Create a reference to FooBar
|
|
13
|
+
# class FooBar; end
|
|
14
|
+
# klass = Begegger.get('FooBar')
|
|
15
|
+
# => FooBar
|
|
16
|
+
def self.get(klass_name)
|
|
17
|
+
Getter.get(klass_name)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Begetter
|
|
2
|
+
class Getter
|
|
3
|
+
class << self
|
|
4
|
+
# generate a class based on a string name and raise
|
|
5
|
+
# Begetter::ClassNotDeclared if the class doesnt exist in the
|
|
6
|
+
# current scope
|
|
7
|
+
def get(klass_name)
|
|
8
|
+
Object.const_get(klass_name)
|
|
9
|
+
rescue NameError
|
|
10
|
+
raise Begetter::ClassNotDeclared,
|
|
11
|
+
"#{klass_name} does not exist. Did you declare it?"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: begetter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jessica Zahnd
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-11-11 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.10'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.10'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.3.0
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 3.3.0
|
|
55
|
+
description:
|
|
56
|
+
email:
|
|
57
|
+
- jessica@jessicazahnd.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- ".rspec"
|
|
64
|
+
- ".travis.yml"
|
|
65
|
+
- Gemfile
|
|
66
|
+
- LICENSE
|
|
67
|
+
- README.md
|
|
68
|
+
- Rakefile
|
|
69
|
+
- begetter.gemspec
|
|
70
|
+
- bin/console
|
|
71
|
+
- lib/begetter.rb
|
|
72
|
+
- lib/begetter/error.rb
|
|
73
|
+
- lib/begetter/getter.rb
|
|
74
|
+
- lib/begetter/version.rb
|
|
75
|
+
homepage: http://github.com/jzahnd/begetter
|
|
76
|
+
licenses:
|
|
77
|
+
- MIT
|
|
78
|
+
metadata: {}
|
|
79
|
+
post_install_message:
|
|
80
|
+
rdoc_options: []
|
|
81
|
+
require_paths:
|
|
82
|
+
- lib
|
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - ">="
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '0'
|
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: '0'
|
|
93
|
+
requirements: []
|
|
94
|
+
rubyforge_project:
|
|
95
|
+
rubygems_version: 2.4.5
|
|
96
|
+
signing_key:
|
|
97
|
+
specification_version: 4
|
|
98
|
+
summary: Begetter creates a reference to a class constant from a string.
|
|
99
|
+
test_files: []
|