column_prefix 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +10 -0
- data/column_prefix.gemspec +21 -0
- data/lib/column_prefix.rb +16 -0
- data/lib/column_prefix/version.rb +3 -0
- data/test/test_column_prefix.rb +18 -0
- data/test/test_helper.rb +30 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mike Robinson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Column Prefix
|
2
|
+
|
3
|
+
Extends ActiveResource with column prefix option. Column Prefix will then alias all columns with the same column name, minus the prefix.
|
4
|
+
|
5
|
+
Please note that if an unprefixed name would override an existing attribute or method it will not be aliased. However, you can still access the attribute via its prefixed version.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'column_prefix'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install column_prefix
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
In your model, declare the @column\_prefix@ you would like to alias. For the prefix "entry\_":
|
24
|
+
|
25
|
+
class Entry < ActiveRecord::Base
|
26
|
+
column_prefix "entry_"
|
27
|
+
end
|
28
|
+
|
29
|
+
You can then access attributes via their simpler name. For example, the column "entry\_title" can be accessed via @e.title@ or @e.entry\_title@ (where @e@ is an instance of @Entry@).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/column_prefix/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Mike Robinson"]
|
6
|
+
gem.email = ["mike@studiolift.com"]
|
7
|
+
gem.description = %q{Extends ActiveResource with column prefix option, handy for legacy databases}
|
8
|
+
gem.summary = %q{Extends ActiveResource with column prefix option}
|
9
|
+
gem.homepage = "http://github.com/studiolift/column_prefix"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "column_prefix"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ColumnPrefix::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency('activerecord', '>= 3.2.7')
|
19
|
+
|
20
|
+
gem.add_development_dependency('sqlite3')
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "column_prefix/version"
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
module ColumnPrefix
|
5
|
+
# Alias prefixed attributes
|
6
|
+
def column_prefix(prefix)
|
7
|
+
column_names.each do |name|
|
8
|
+
new_name = name.sub(prefix, '')
|
9
|
+
if !self.respond_to? new_name
|
10
|
+
alias_attribute new_name.to_sym, name.to_sym
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Base.send :extend, ColumnPrefix
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
class ColumnPrefixTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@entry = Entry.last
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_attributes_match
|
9
|
+
assert_equal @entry.title, @entry.entry_title
|
10
|
+
@entry.title = "Edited title"
|
11
|
+
assert_equal @entry.title, @entry.entry_title
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_method_clash
|
15
|
+
assert_equal "None of your business", @entry.author
|
16
|
+
assert_equal "Mike Robinson", @entry.entry_author
|
17
|
+
end
|
18
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'column_prefix'
|
2
|
+
require 'sqlite3'
|
3
|
+
|
4
|
+
# Configure test DB
|
5
|
+
db = SQLite3::Database.new 'test.db'
|
6
|
+
db.execute <<SQL
|
7
|
+
CREATE TABLE IF NOT EXISTS entries (
|
8
|
+
entry_id INTEGER PRIMARY KEY,
|
9
|
+
entry_title VARCHAR(255),
|
10
|
+
entry_text VARCHAR(255),
|
11
|
+
entry_author VARCHAR(255)
|
12
|
+
);
|
13
|
+
SQL
|
14
|
+
|
15
|
+
# Example model
|
16
|
+
class Entry < ActiveRecord::Base
|
17
|
+
establish_connection(
|
18
|
+
:adapter => "sqlite3",
|
19
|
+
:database => "test.db"
|
20
|
+
)
|
21
|
+
|
22
|
+
column_prefix "entry_"
|
23
|
+
|
24
|
+
def author
|
25
|
+
"None of your business"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Sample data
|
30
|
+
Entry.create({:title => "The first entry", :text => "Lorem ipsum dolor sit amet", :author => "Mike Robinson"})
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: column_prefix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Robinson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.7
|
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: 3.2.7
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
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
|
+
description: Extends ActiveResource with column prefix option, handy for legacy databases
|
47
|
+
email:
|
48
|
+
- mike@studiolift.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- column_prefix.gemspec
|
59
|
+
- lib/column_prefix.rb
|
60
|
+
- lib/column_prefix/version.rb
|
61
|
+
- test/test_column_prefix.rb
|
62
|
+
- test/test_helper.rb
|
63
|
+
homepage: http://github.com/studiolift/column_prefix
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Extends ActiveResource with column prefix option
|
87
|
+
test_files:
|
88
|
+
- test/test_column_prefix.rb
|
89
|
+
- test/test_helper.rb
|