add_symbolic_names 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +89 -0
- data/Rakefile +38 -0
- data/lib/add_symbolic_names/mixin.rb +15 -0
- data/lib/add_symbolic_names/railtie.rb +14 -0
- data/lib/add_symbolic_names/store/active_record.rb +17 -0
- data/lib/add_symbolic_names/store/yaml.rb +13 -0
- data/lib/add_symbolic_names/store.rb +11 -0
- data/lib/add_symbolic_names/version.rb +3 -0
- data/lib/add_symbolic_names.rb +9 -0
- data/test/add_symbolic_names_test.rb +22 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 85d02ac625bf040cde94a04304cf764186b7b2cd
|
4
|
+
data.tar.gz: 935762f355537a2c8396a2506cba3f6b73b00413
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 865059c39f2e856436730fb346cfb77b93679a2e77f8bdb06d6bd35e6202886919cd7388b35ffe71f4a6feff69013e74b74e819248ef3da4246619334fcb2ca0
|
7
|
+
data.tar.gz: 1cd9ac4be1f6d4596583b2a20ab90162ff48c9c8c4e42759692209042e8dd7843aff2fa8e54a4db582d88f82c2358a71dba7183c0b62aecf6b954c759711d6f3
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
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,89 @@
|
|
1
|
+
# AddSymbolicNames [![Build Status](https://travis-ci.org/avvo/add_symbolic_names.png?branch=master)](https://travis-ci.org/avvo/add_symbolic_names)
|
2
|
+
|
3
|
+
ActiveRecord plugin for creating constants from a domain table
|
4
|
+
|
5
|
+
## Motivation
|
6
|
+
|
7
|
+
You have foreign keys in your database that are actually constants.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
In your Gemfile:
|
12
|
+
|
13
|
+
```
|
14
|
+
gem 'add_symbolic_names'
|
15
|
+
```
|
16
|
+
|
17
|
+
In your ActiveRecord model:
|
18
|
+
|
19
|
+
```
|
20
|
+
class SomeDomain < ActiveRecord::Base
|
21
|
+
add_symbolic_names
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
The `add_symbolic_names` method takes an options hash with 3 possible values:
|
28
|
+
|
29
|
+
* `symbolic_name_attrib` - the name of the column/attribute to use when defining the constant name
|
30
|
+
* `value_attrib` - the name of the column/attribute to use for the value of the constant
|
31
|
+
* `store` - how to load the data
|
32
|
+
|
33
|
+
## Stores
|
34
|
+
|
35
|
+
There are 2 data stores available
|
36
|
+
|
37
|
+
### ActiveRecord
|
38
|
+
|
39
|
+
ActiveRecord is the default data store. It loads all records, iterating through and defining constants with a name of the `symbolic_name_attrib` option with a value of the `value_attrib` option.
|
40
|
+
|
41
|
+
```
|
42
|
+
…
|
43
|
+
add_symbolic_names store: :active_record,
|
44
|
+
symbolic_name_attrib: :some_column,
|
45
|
+
value_attrib: :another_column
|
46
|
+
```
|
47
|
+
|
48
|
+
### Yaml
|
49
|
+
|
50
|
+
Yaml store loads from your db/domain folder a yaml file with the pluralized, demodulized class name. (User => users.yml)
|
51
|
+
|
52
|
+
```
|
53
|
+
…
|
54
|
+
add_symbolic_names store: :yaml
|
55
|
+
```
|
56
|
+
|
57
|
+
## Example
|
58
|
+
|
59
|
+
Let's say you keep track of how you create users on your site. You have a `user_creation_methods` table that looks something like:
|
60
|
+
|
61
|
+
| id | symbolic_name |
|
62
|
+
|----|---------------|
|
63
|
+
| 1 | EMAIL |
|
64
|
+
| 2 | REGISTER |
|
65
|
+
| 3 | IMPORT |
|
66
|
+
|
67
|
+
```
|
68
|
+
class UserCreationMethod < ActiveRecord::Base
|
69
|
+
add_symbolic_names
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
You now have access to some namespaced constants:
|
74
|
+
|
75
|
+
* `UserCreationMethod::EMAIL` => 1
|
76
|
+
* `UserCreationMethod::REGISTER` => 2
|
77
|
+
* `UserCreationMethod::IMPORT` => 3
|
78
|
+
|
79
|
+
```
|
80
|
+
class User < ActiveRecord::Base
|
81
|
+
belongs_to :user_creation_method
|
82
|
+
|
83
|
+
scope :imported, -> { where(user_creation_method_id: UserCreationMethod::IMPORT) }
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
## Credit
|
88
|
+
|
89
|
+
[bvandenbos](http://github.com/bvandenbos) actually wrote this 6+ years ago. We think this could be useful for others.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'AddSymbolicNames'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module AddSymbolicNames
|
2
|
+
module Mixin
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def add_symbolic_names(opts = {})
|
8
|
+
store = Store.fetch(opts.fetch(:store) { AddSymbolicNames.default_store })
|
9
|
+
|
10
|
+
store.define_constants(self, opts)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module AddSymbolicNames
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
config.add_symbolic_names = ActiveSupport::OrderedOptions.new
|
4
|
+
config.add_symbolic_names.default_store = nil
|
5
|
+
|
6
|
+
initializer 'add_symbolic_names' do |app|
|
7
|
+
AddSymbolicNames.default_store = app.config.add_symbolic_names.default_store if app.config.add_symbolic_names.default_store
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
include AddSymbolicNames::Mixin
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module AddSymbolicNames
|
2
|
+
module Store
|
3
|
+
class ActiveRecord
|
4
|
+
def self.define_constants(klass, opts)
|
5
|
+
symbolic_name_attr = opts.fetch(:symbolic_name_attrib, :symbolic_name)
|
6
|
+
value_attr = opts.fetch(:value_attrib, :id)
|
7
|
+
|
8
|
+
klass.all.each do |record|
|
9
|
+
symbolic_name = record.send(symbolic_name_attr)
|
10
|
+
value = record.send(value_attr)
|
11
|
+
|
12
|
+
klass.const_set(symbolic_name, value) unless klass.const_defined?(symbolic_name)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module AddSymbolicNames
|
4
|
+
module Store
|
5
|
+
class Yaml
|
6
|
+
def self.define_constants(klass, opts)
|
7
|
+
YAML.load_file(File.join(Rails.root, "db/domain/#{klass.name.demodulize.tableize}.yml")).each do |datum|
|
8
|
+
klass.const_set(datum['symbolic_name'], datum['id']) unless klass.const_defined?(datum['symbolic_name'])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AddSymbolicNamesTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
test "loading from ActiveRecord" do
|
6
|
+
assert_equal 1, UserCreationMethod::EMAIL
|
7
|
+
assert_equal 2, UserCreationMethod::REGISTER
|
8
|
+
assert_equal 3, UserCreationMethod::IMPORT
|
9
|
+
end
|
10
|
+
|
11
|
+
test "loading alternate column names" do
|
12
|
+
assert_equal "Washington", State::WA
|
13
|
+
assert_equal "Oregon", State::OR
|
14
|
+
assert_equal "California", State::CA
|
15
|
+
end
|
16
|
+
|
17
|
+
test "loading from yaml" do
|
18
|
+
assert_equal 1, User::UNKNOWN
|
19
|
+
assert_equal 2, User::ADMIN
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: add_symbolic_names
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben VandenBos
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: ActiveRecord plugin for creating constants from a domain table
|
42
|
+
email: bvandenbos@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/add_symbolic_names/mixin.rb
|
48
|
+
- lib/add_symbolic_names/railtie.rb
|
49
|
+
- lib/add_symbolic_names/store/active_record.rb
|
50
|
+
- lib/add_symbolic_names/store/yaml.rb
|
51
|
+
- lib/add_symbolic_names/store.rb
|
52
|
+
- lib/add_symbolic_names/version.rb
|
53
|
+
- lib/add_symbolic_names.rb
|
54
|
+
- MIT-LICENSE
|
55
|
+
- Rakefile
|
56
|
+
- README.md
|
57
|
+
- test/add_symbolic_names_test.rb
|
58
|
+
homepage: http://github.com/avvo/add_symbolic_names
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.0.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: ActiveRecord plugin for creating constants from a domain table
|
82
|
+
test_files:
|
83
|
+
- test/add_symbolic_names_test.rb
|