attr_symbol 0.0.1
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.
- data/.document +5 -0
- data/.rvmrc +1 -0
- data/LICENSE +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/attr_symbol.rb +24 -0
- data/test/helper.rb +29 -0
- data/test/test_attr_symbol.rb +43 -0
- metadata +104 -0
data/.document
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.8.7-p299@attr_symbol
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Justin Wienckowski
|
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,23 @@
|
|
1
|
+
= attr_symbol
|
2
|
+
|
3
|
+
Allows certain attributes in ActiveRecord models to be handled as symbols. These attributes are
|
4
|
+
stored in string columns in the underlying data store.
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
class Thing < ActiveRecord::Base
|
9
|
+
attr_symbol :a
|
10
|
+
end
|
11
|
+
|
12
|
+
# Stores the string 'foo' in the field 'a'
|
13
|
+
thing = Thing.create!(:a => :foo)
|
14
|
+
|
15
|
+
thing = Thing.find(1)
|
16
|
+
thing.a ### Returns :foo
|
17
|
+
|
18
|
+
thing.a = 'urk'
|
19
|
+
thing.a ### Returns :urk
|
20
|
+
|
21
|
+
== Copyright
|
22
|
+
|
23
|
+
Copyright (c) 2010 Justin Wienckowski. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "attr_symbol"
|
8
|
+
gem.summary = "Declare one or more ActiveRecord attributes stored in string columns to be returned as symbols."
|
9
|
+
gem.description = "Attributes marked with the attr_symbol method can be set using strings or symbols, but when accessed will always return symbols."
|
10
|
+
gem.email = "jwinky+gems@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/jwinky/attr_symbol"
|
12
|
+
gem.authors = ["Justin Wienckowski"]
|
13
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
14
|
+
gem.add_development_dependency "activerecord", ">= 0"
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "attr_symbol #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/attr_symbol.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveRecord::AttrSymbol
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def attr_symbol(*attributes)
|
10
|
+
attributes.each do |attr|
|
11
|
+
define_method(attr) do
|
12
|
+
self[attr].try(:to_sym)
|
13
|
+
end
|
14
|
+
|
15
|
+
define_method(:"#{attr}=") do |value|
|
16
|
+
self[attr] = value.try(:to_s)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveRecord::Base.send(:include, ActiveRecord::AttrSymbol)
|
data/test/helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
require 'attr_symbol'
|
9
|
+
|
10
|
+
class Test::Unit::TestCase
|
11
|
+
end
|
12
|
+
|
13
|
+
# So we can test without having a database.
|
14
|
+
# From http://stackoverflow.com/questions/937429/activerecordbase-without-table-rails
|
15
|
+
class ActiveRecord::Tableless < ActiveRecord::Base
|
16
|
+
def self.columns
|
17
|
+
@columns ||= [];
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
21
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
|
22
|
+
sql_type.to_s, null)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Override the save method to prevent exceptions.
|
26
|
+
def save(validate = true)
|
27
|
+
validate ? valid? : true
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Thing < ActiveRecord::Tableless
|
4
|
+
column :foo, :string
|
5
|
+
attr_symbol :foo
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
class TestAttrSymbol < Test::Unit::TestCase
|
10
|
+
context "Setting attribute 'foo'" do
|
11
|
+
subject { Thing.new }
|
12
|
+
|
13
|
+
context "to a string value" do
|
14
|
+
setup { subject.foo = "bar" }
|
15
|
+
should("store the string 'bar' in the attribute") do
|
16
|
+
assert_equal "bar", subject[:foo]
|
17
|
+
end
|
18
|
+
should("return the symbol :bar from the accessor") do
|
19
|
+
assert_equal :bar, subject.foo
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "to a symbol" do
|
24
|
+
setup { subject.foo = :bar }
|
25
|
+
should("store the string 'bar' in the attribute") do
|
26
|
+
assert_equal "bar", subject[:foo]
|
27
|
+
end
|
28
|
+
should("return the symbol :bar from the accessor") do
|
29
|
+
assert_equal :bar, subject.foo
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "to nil" do
|
34
|
+
setup { subject.foo = nil }
|
35
|
+
should("store nil in the attribute") do
|
36
|
+
assert_equal nil, subject[:foo]
|
37
|
+
end
|
38
|
+
should("return nil from the accessor") do
|
39
|
+
assert_equal nil, subject.foo
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_symbol
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Justin Wienckowski
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-08 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: shoulda
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: activerecord
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Attributes marked with the attr_symbol method can be set using strings or symbols, but when accessed will always return symbols.
|
50
|
+
email: jwinky+gems@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- LICENSE
|
57
|
+
- README.rdoc
|
58
|
+
files:
|
59
|
+
- .document
|
60
|
+
- .rvmrc
|
61
|
+
- LICENSE
|
62
|
+
- README.rdoc
|
63
|
+
- Rakefile
|
64
|
+
- VERSION
|
65
|
+
- lib/attr_symbol.rb
|
66
|
+
- test/helper.rb
|
67
|
+
- test/test_attr_symbol.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: http://github.com/jwinky/attr_symbol
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.3.7
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Declare one or more ActiveRecord attributes stored in string columns to be returned as symbols.
|
102
|
+
test_files:
|
103
|
+
- test/helper.rb
|
104
|
+
- test/test_attr_symbol.rb
|