pkwde-has_set 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/has_set.gemspec +1 -1
- data/lib/has_set.rb +7 -6
- data/test/has_set_test.rb +14 -5
- metadata +1 -1
data/has_set.gemspec
CHANGED
data/lib/has_set.rb
CHANGED
@@ -5,7 +5,7 @@ require 'activesupport'
|
|
5
5
|
require 'activerecord'
|
6
6
|
|
7
7
|
module HasSet
|
8
|
-
VERSION = '0.0.
|
8
|
+
VERSION = '0.0.2'
|
9
9
|
|
10
10
|
module ClassMethods
|
11
11
|
|
@@ -18,10 +18,6 @@ module HasSet
|
|
18
18
|
|
19
19
|
set_column = options.has_key?(:column_name) ? options[:column_name].to_s : "#{set_name}_bitfield"
|
20
20
|
|
21
|
-
unless self.columns_hash[set_column.to_s].type == :integer
|
22
|
-
raise ActiveRecord::ActiveRecordError, "The column '#{set_column}' to store the the set must be of type integer, because we store them in a bitfield."
|
23
|
-
end
|
24
|
-
|
25
21
|
begin
|
26
22
|
enum_class = options.has_key?(:enum_class) ? options[:enum_class] : Object.const_get(set_name.to_s.camelcase)
|
27
23
|
rescue NameError => ne
|
@@ -32,6 +28,12 @@ module HasSet
|
|
32
28
|
if set_elements.blank?
|
33
29
|
self[set_column] = 0
|
34
30
|
else
|
31
|
+
if set_elements.kind_of? String
|
32
|
+
set_elements = set_elements.split(",").collect do |element|
|
33
|
+
element.strip!
|
34
|
+
enum_class.const_get(element)
|
35
|
+
end
|
36
|
+
end
|
35
37
|
[set_elements].flatten.each do |element|
|
36
38
|
unless element.kind_of? enum_class
|
37
39
|
raise ArgumentError, "You must provide an element of the #{enum_class} Enumeration. You provided an element of the class #{element.class}."
|
@@ -68,7 +70,6 @@ module HasSet
|
|
68
70
|
end
|
69
71
|
end
|
70
72
|
end
|
71
|
-
|
72
73
|
end
|
73
74
|
end
|
74
75
|
|
data/test/has_set_test.rb
CHANGED
@@ -30,7 +30,6 @@ class HasSetTest < Test::Unit::TestCase
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_should_throw_argument_error_if_there_is_no_enumeratable_class_to_take_the_set_entries_from
|
33
|
-
Person.expects(:columns_hash).returns({"hobbies_bitfield" => stub(:type => :integer)})
|
34
33
|
assert_raise(NameError) { Person.has_set :hobbies }
|
35
34
|
end
|
36
35
|
|
@@ -53,15 +52,25 @@ class HasSetTest < Test::Unit::TestCase
|
|
53
52
|
assert !person.interest_dating?, "Person should not be interested in dating."
|
54
53
|
end
|
55
54
|
|
56
|
-
def test_should_raise_active_record_error_if_the_type_of_the_bitfiel_column_is_not_integer
|
57
|
-
assert_raise(ActiveRecord::ActiveRecordError) { Punk.has_set :bad_habits }
|
58
|
-
end
|
59
|
-
|
60
55
|
def test_should_get_all_set_elements_of_a_certain_object
|
61
56
|
person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating, Interests::Shopping])
|
62
57
|
assert_equal [Interests::Dating, Interests::Shopping], person.interests
|
63
58
|
end
|
64
59
|
|
60
|
+
def test_should_set_elements_by_string_names
|
61
|
+
person = Person.new(:fullname => "Jessie Summers", :interests => "Dating, Shopping")
|
62
|
+
assert_equal [Interests::Dating, Interests::Shopping], person.interests
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_should_set_elements_by_string_name
|
66
|
+
person = Person.new(:fullname => "Jessie Summers", :interests => "Dating")
|
67
|
+
assert_equal [Interests::Dating], person.interests
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_raise_exception_if_strings_cannot_be_constantized
|
71
|
+
assert_raise(NameError) { Person.new(:fullname => "Jessie Summers", :interests => "Dadding, Shopping") }
|
72
|
+
end
|
73
|
+
|
65
74
|
def test_should_provide_the_name_of_the_bitfield_column
|
66
75
|
party = Party.new(:location => "Beach House", :drinks => [Drinks::Beer, Drinks::CubaLibre])
|
67
76
|
assert party.save, "Party should save!"
|