hashed_attributes 0.0.1 → 0.0.2
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/.gitignore +1 -0
- data/Rakefile +13 -0
- data/lib/hashed_attributes.rb +10 -13
- data/lib/hashed_attributes/version.rb +1 -1
- data/test/hashed_attributes_test.rb +46 -0
- data/test/helper.rb +31 -0
- metadata +9 -6
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,2 +1,15 @@
|
|
1
1
|
require 'bundler'
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
# Run the test with 'rake' or 'rake test'
|
7
|
+
desc 'Default: run hashed_attributes unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Test the hashed_attributes gem.'
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib' << 'test'
|
13
|
+
t.pattern = 'test/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
data/lib/hashed_attributes.rb
CHANGED
@@ -8,21 +8,15 @@ module HashedAttributes
|
|
8
8
|
def hashed_attributes name, *options
|
9
9
|
include InstanceMethods
|
10
10
|
|
11
|
-
serialize name
|
11
|
+
serialize name, Hash
|
12
12
|
|
13
|
-
options.each
|
14
|
-
define_method
|
15
|
-
|
16
|
-
|
13
|
+
options.each { |key|
|
14
|
+
define_method(key) { get_hashed_attribute_for(key) }
|
15
|
+
define_method("#{key}=") { |value| set_hashed_attribute_for(key,value) }
|
16
|
+
}
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
define_method "hashed_attributes_column" do
|
24
|
-
name
|
25
|
-
end
|
18
|
+
define_method("hashed_attributes_column") { name }
|
19
|
+
define_method("hashed_attributes_keys") { options }
|
26
20
|
|
27
21
|
self.after_initialize do
|
28
22
|
initialize_hashed_attributes
|
@@ -31,6 +25,8 @@ module HashedAttributes
|
|
31
25
|
end
|
32
26
|
|
33
27
|
module InstanceMethods
|
28
|
+
protected
|
29
|
+
|
34
30
|
def get_hashed_attribute_for(key)
|
35
31
|
self[hashed_attributes_column][key]
|
36
32
|
end
|
@@ -46,4 +42,5 @@ module HashedAttributes
|
|
46
42
|
end
|
47
43
|
|
48
44
|
end
|
45
|
+
|
49
46
|
ActiveRecord::Base.send(:include, HashedAttributes)
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'helper.rb'
|
2
|
+
|
3
|
+
class HashedAttributesTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
setup_database
|
6
|
+
@person = Person.new(:name=>"Sean Behan", :theme=>"Molokai", :plan => "Pro", :favorite_color=>"Orange")
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
teardown_database
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_that_active_record_has_been_extended
|
14
|
+
assert_equal ActiveRecord::Base.methods.include?("hashed_attributes"), true
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_that_object_instantiation_is_normal
|
18
|
+
assert_kind_of Person, Person.new(:name=>"sean", :theme=>"blue")
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_hashed_attributes_is_an_empty_hash_on_object_initialization
|
22
|
+
assert_equal Person.new.preferences, {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_that_hashed_attributes_are_persisted_along_with_model
|
26
|
+
Person.create(:theme=>"Molokai")
|
27
|
+
assert_equal Person.last.theme, "Molokai"
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_that_hashed_attributes_getter_and_setter_methods_are_using_the_hash
|
31
|
+
assert_equal @person.preferences[:theme], @person.theme
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_that_hashed_attributes_keys_match_the_preferences_hash
|
35
|
+
assert_equal @person.hashed_attributes_keys, @person.preferences.keys
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_hashed_attributes_arguments_list
|
39
|
+
assert_equal @person.hashed_attributes_keys, [:theme, :plan, :favorite_color]
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_that_all_hashed_attributes_values_have_been_set_correctly
|
43
|
+
assert_equal @person.preferences.values, ["Molokai", "Pro", "Orange"]
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
ENV["RAILS_ENV"] ||= "test"
|
4
|
+
|
5
|
+
require 'bundler'
|
6
|
+
require 'test/unit'
|
7
|
+
require 'active_record'
|
8
|
+
require "#{File.dirname(__FILE__)}/../lib/hashed_attributes"
|
9
|
+
|
10
|
+
|
11
|
+
ActiveRecord::Schema.verbose = false
|
12
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
13
|
+
|
14
|
+
def setup_database
|
15
|
+
ActiveRecord::Schema.define(:version => 1) do
|
16
|
+
create_table :people do |t|
|
17
|
+
t.string :name
|
18
|
+
t.text :preferences
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown_database
|
24
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
25
|
+
ActiveRecord::Base.connection.drop_table(table)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Person < ActiveRecord::Base
|
30
|
+
hashed_attributes :preferences, :theme, :plan, :favorite_color
|
31
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashed_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sean Behan
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-06-
|
18
|
+
date: 2011-06-18 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -36,6 +36,8 @@ files:
|
|
36
36
|
- hashed_attributes.gemspec
|
37
37
|
- lib/hashed_attributes.rb
|
38
38
|
- lib/hashed_attributes/version.rb
|
39
|
+
- test/hashed_attributes_test.rb
|
40
|
+
- test/helper.rb
|
39
41
|
has_rdoc: true
|
40
42
|
homepage: https://github.com/bseanvt/hashed_attributes
|
41
43
|
licenses: []
|
@@ -70,5 +72,6 @@ rubygems_version: 1.3.7
|
|
70
72
|
signing_key:
|
71
73
|
specification_version: 3
|
72
74
|
summary: Key/value store...
|
73
|
-
test_files:
|
74
|
-
|
75
|
+
test_files:
|
76
|
+
- test/hashed_attributes_test.rb
|
77
|
+
- test/helper.rb
|