degu 0.0.4

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.
@@ -0,0 +1,155 @@
1
+ require 'test_helper.rb'
2
+
3
+ class HasSetTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ setup_db
7
+ end
8
+
9
+ def teardown
10
+ teardown_db
11
+ end
12
+
13
+ def test_should_have_has_set_class_method
14
+ assert_respond_to Person, :has_set
15
+ end
16
+
17
+ def test_should_provide_a_way_of_storing_enums_in_a_set_on_an_ar_model
18
+ person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating, Interests::Shopping])
19
+ assert person.save, "Person should save!"
20
+ person.reload
21
+ assert person.interest_shopping?, "Person should be interested in shopping."
22
+ assert person.interest_dating?, "Person should be interested in dating."
23
+ end
24
+
25
+ def test_should_provide_a_way_of_storing_a_single_enum_value_in_a_set_on_an_ar_model
26
+ person = Person.new(:fullname => "Jessie Summers", :interests => Interests::Dating)
27
+ assert person.save, "Person should save!"
28
+ person.reload
29
+ assert person.interest_dating?, "Person should be interested in dating."
30
+ end
31
+
32
+ def test_should_throw_argument_error_if_there_is_no_enumeratable_class_to_take_the_set_entries_from
33
+ assert_raise(NameError) { Person.has_set :hobbies }
34
+ end
35
+
36
+ def test_should_set_a_single_set_element
37
+ person = Person.new
38
+ person.interest_golf = true
39
+ assert person.save, "Person should save!"
40
+ person.reload
41
+ assert person.interest_golf?, "Person should be interested in golf."
42
+ person.interest_golf = nil
43
+ assert !person.interest_golf?, "Person should not be interested in golf."
44
+ end
45
+
46
+ def test_should_reset_all_set_entries
47
+ person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating, Interests::Shopping])
48
+ assert person.interest_shopping?, "Person should be interested in shopping."
49
+ assert person.interest_dating?, "Person should be interested in dating."
50
+ person.interests = nil
51
+ assert !person.interest_shopping?, "Person should not be interested in shopping."
52
+ assert !person.interest_dating?, "Person should not be interested in dating."
53
+ end
54
+
55
+ def test_should_get_all_set_elements_of_a_certain_object
56
+ person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating, Interests::Shopping])
57
+ assert_equal [Interests::Dating, Interests::Shopping], person.interests
58
+ end
59
+
60
+ def test_should_reset_values_if_setter_is_used
61
+ person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating])
62
+ person.interests = Interests::Shopping
63
+ assert_equal [Interests::Shopping], person.interests
64
+ end
65
+
66
+ def test_should_set_elements_by_string_names
67
+ person = Person.new(:fullname => "Jessie Summers", :interests => "Dating, Shopping")
68
+ assert_equal [Interests::Dating, Interests::Shopping], person.interests
69
+ end
70
+
71
+ def test_should_set_elements_by_string_name
72
+ person = Person.new(:fullname => "Jessie Summers", :interests => "Dating")
73
+ assert_equal [Interests::Dating], person.interests
74
+ end
75
+
76
+ def test_should_raise_exception_if_strings_cannot_be_constantized
77
+ assert_raise(ArgumentError) { Person.new(:fullname => "Jessie Summers", :interests => "Dadding, Shopping") }
78
+ end
79
+
80
+ def test_should_provide_the_name_of_the_bitfield_column
81
+ party = Party.new(:location => "Beach House", :drinks => [Drinks::Beer, Drinks::CubaLibre])
82
+ assert party.save, "Party should save!"
83
+ party.reload
84
+ assert party.drink_beer?, "Party should offer beer."
85
+ assert party.drink_beer, "Party should offer beer."
86
+ assert party.drink_cuba_libre?, "Party should offer cuba libre."
87
+ end
88
+
89
+ def test_should_have_to_s_method
90
+ party = Party.new(:location => "Beach House", :drinks => [Drinks::Beer, Drinks::CubaLibre])
91
+ assert_equal "Beer, CubaLibre", party.drinks.to_s
92
+ end
93
+
94
+ def test_should_provide_the_name_of_the_enum_class
95
+ Party.has_set :music, :enum_class => MusicStyles
96
+
97
+ party = Party.new(:location => "Penthouse", :music => [MusicStyles::Rock])
98
+ assert party.save, "Party should save!"
99
+ party.reload
100
+ assert party.music_rock?, "Party should have Rock music."
101
+ end
102
+
103
+ def test_should_allow_unset_bitset
104
+ itunes = Itunes.new
105
+ assert_nil itunes.music, itunes.inspect
106
+ assert itunes.save, "Itunes should save!"
107
+ itunes.reload
108
+ assert_nil itunes.music, itunes.inspect
109
+ end
110
+
111
+ def test_should_understand_symbol_values
112
+ itunes = Itunes.new
113
+ itunes.music = :rock, :pop
114
+ assert itunes.music_rock?, "Itunes should have Rock music."
115
+ assert itunes.music_pop?, "Itunes should have Pop music."
116
+ assert itunes.save, "Itunes should save!"
117
+ itunes.reload
118
+ assert itunes.music_rock?, "Itunes should have Rock music."
119
+ assert itunes.music_pop?, "Itunes should have Pop music."
120
+ end
121
+
122
+ def test_should_understand_comma_separated_strings
123
+ itunes = Itunes.new
124
+ itunes.music = 'Rock, pop'
125
+ assert itunes.music_rock?, "Itunes should have Rock music."
126
+ assert itunes.music_pop?, "Itunes should have Pop music."
127
+ assert itunes.save, "Itunes should save!"
128
+ itunes.reload
129
+ assert itunes.music_rock?, "Itunes should have Rock music."
130
+ assert itunes.music_pop?, "Itunes should have Pop music."
131
+ end
132
+
133
+ def test_should_validate_that_only_elements_from_the_given_enum_are_used_in_the_set
134
+ person = Person.new(:fullname => "Jessie Summers", :interests => Interests::Dating)
135
+ assert_raise(ArgumentError) { person.interests = Drinks::Beer }
136
+ end
137
+
138
+ def test_should_list_all_available_enum_elements
139
+ assert_equal ["drink_beer", "drink_cuba_libre", "drink_wine"], Party.new.available_drinks
140
+ end
141
+
142
+ def test_should_extend_enum_with_fieldname_method
143
+ assert_equal "drink_cuba_libre", Drinks::CubaLibre.field_name
144
+ assert_equal "drink_beer", Drinks::Beer.field_name
145
+ assert_equal "drink_wine", Drinks::Wine.field_name
146
+ end
147
+
148
+ def test_empty_bitfield
149
+ party = Party.new :drinks => [ :beer ]
150
+ assert_equal [ Drinks[:beer] ], party.drinks
151
+ party.drinks = []
152
+ assert_equal [], party.drinks
153
+ assert_equal 0, party.drinks_set
154
+ end
155
+ end
@@ -0,0 +1,138 @@
1
+ require 'degu'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+ require 'active_record'
5
+
6
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
7
+ ActiveRecord::Migration.verbose = false
8
+
9
+ def setup_db
10
+ ActiveRecord::Base.silence do
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table :class_with_enums do |t|
13
+ t.column :title, :string
14
+ t.column :product_type, :string
15
+ t.column :created_at, :datetime
16
+ t.column :updated_at, :datetime
17
+ end
18
+
19
+ create_table :class_without_enums do |t|
20
+ t.column :title, :string
21
+ t.column :created_at, :datetime
22
+ t.column :updated_at, :datetime
23
+ end
24
+
25
+ create_table :class_with_custom_name_enums do |t|
26
+ t.column :title, :string
27
+ t.column :product_enum, :string
28
+ t.column :created_at, :datetime
29
+ t.column :updated_at, :datetime
30
+ end
31
+
32
+ create_table :people do |t|
33
+ t.string :fullname
34
+ t.integer :interests_bitfield, :default => 0
35
+ end
36
+
37
+ create_table :punks do |t|
38
+ t.string :fullname
39
+ t.string :bad_habits_bitfield
40
+ end
41
+
42
+ create_table :parties do |t|
43
+ t.string :location
44
+ t.integer :drinks_set, :default => 0
45
+ t.integer :music_bitfield, :default => 0
46
+ end
47
+
48
+ create_table :itunes do |t|
49
+ t.string :location
50
+ t.integer :music_bitfield
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ def teardown_db
57
+ ActiveRecord::Base.connection.tables.each do |table|
58
+ ActiveRecord::Base.connection.drop_table(table)
59
+ end
60
+ end
61
+
62
+ enum :Fakes, [:NOT_DEFINIED]
63
+
64
+ enum :Product, [:Silver, :Gold, :Titanium]
65
+
66
+ enum :Interests do
67
+ attr_reader :bitfield_index
68
+
69
+ Art(0)
70
+ Golf(1)
71
+ Sleeping(2)
72
+ Drinking(3)
73
+ Dating(4)
74
+ Shopping(5)
75
+
76
+ def init(bitfield_index)
77
+ @bitfield_index = bitfield_index
78
+ end
79
+ end
80
+
81
+ enum :MusicStyles do
82
+ attr_reader :bitfield_index
83
+
84
+ Rock(0)
85
+ Pop(1)
86
+ RnB(2)
87
+
88
+ def init(bitfield_index)
89
+ @bitfield_index = bitfield_index
90
+ end
91
+ end
92
+
93
+ enum :Drinks do
94
+ attr_reader :bitfield_index
95
+
96
+ Beer(0)
97
+ Wine(1)
98
+ CubaLibre(2)
99
+
100
+ def init(bitfield_index)
101
+ @bitfield_index = bitfield_index
102
+ end
103
+ end
104
+
105
+
106
+ setup_db # Init the database for class creation
107
+
108
+ class ClassWithEnum < ActiveRecord::Base
109
+ has_enum :product
110
+ attr_reader :callback1_executed
111
+ def callback1
112
+ @callback1_executed = true
113
+ end
114
+
115
+ after_save :callback1
116
+ end
117
+
118
+ class ClassWithoutEnum < ActiveRecord::Base; end
119
+
120
+ class ClassWithCustomNameEnum < ActiveRecord::Base
121
+ has_enum :product, :column_name => :product_enum
122
+ end
123
+
124
+ class Person < ActiveRecord::Base
125
+ has_set :interests
126
+ end
127
+
128
+ class Punk < ActiveRecord::Base; end
129
+
130
+ class Party < ActiveRecord::Base
131
+ has_set :drinks, :column_name => :drinks_set
132
+ end
133
+
134
+ class Itunes < ActiveRecord::Base
135
+ has_set :music, :enum_class => MusicStyles
136
+ end
137
+
138
+ teardown_db # And drop them right afterwards
data/test_helper.rb ADDED
@@ -0,0 +1,86 @@
1
+ require File.dirname(__FILE__) + '/../lib/has_set'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
4
+ ActiveRecord::Migration.verbose = false
5
+
6
+ def setup_db
7
+ ActiveRecord::Base.silence do
8
+ ActiveRecord::Schema.define(:version => 1) do
9
+ create_table :people do |t|
10
+ t.string :fullname
11
+ t.integer :interests_bitfield, :default => 0
12
+ end
13
+
14
+ create_table :punks do |t|
15
+ t.string :fullname
16
+ t.string :bad_habits_bitfield
17
+ end
18
+
19
+ create_table :parties do |t|
20
+ t.string :location
21
+ t.integer :drinks_set, :default => 0
22
+ t.integer :music_bitfield, :default => 0
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def teardown_db
29
+ ActiveRecord::Base.connection.tables.each do |table|
30
+ ActiveRecord::Base.connection.drop_table(table)
31
+ end
32
+ end
33
+
34
+ enum :Interests do
35
+ attr_reader :bitfield_index
36
+
37
+ Art(0)
38
+ Golf(1)
39
+ Sleeping(2)
40
+ Drinking(3)
41
+ Dating(4)
42
+ Shopping(5)
43
+
44
+ def init(bitfield_index)
45
+ @bitfield_index = bitfield_index
46
+ end
47
+ end
48
+
49
+ enum :MusicStyles do
50
+ attr_reader :bitfield_index
51
+
52
+ Rock(0)
53
+ Pop(1)
54
+ RnB(2)
55
+
56
+ def init(bitfield_index)
57
+ @bitfield_index = bitfield_index
58
+ end
59
+ end
60
+
61
+ enum :Drinks do
62
+ attr_reader :bitfield_index
63
+
64
+ Beer(0)
65
+ Wine(1)
66
+ CubaLibre(2)
67
+
68
+ def init(bitfield_index)
69
+ @bitfield_index = bitfield_index
70
+ end
71
+ end
72
+
73
+ setup_db # Init the database for class creation
74
+
75
+ class Person < ActiveRecord::Base
76
+ has_set :interests
77
+ end
78
+
79
+ class Punk < ActiveRecord::Base; end
80
+
81
+ class Party < ActiveRecord::Base
82
+ has_set :drinks, :column_name => :drinks_set
83
+ end
84
+
85
+ teardown_db # And drop them right afterwards
86
+
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: degu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Florian Frank
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gem_hadar
16
+ requirement: &2160629180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.3
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2160629180
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ requirement: &2160628700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2160628700
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &2160628280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2160628280
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &2160573460 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2160573460
58
+ - !ruby/object:Gem::Dependency
59
+ name: activerecord
60
+ requirement: &2160572000 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *2160572000
69
+ description: Library that includes enums, and rails support for enums and bitfield
70
+ sets.
71
+ email: dev@pkw.de
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - README.rdoc
76
+ - lib/degu/has_enum.rb
77
+ - lib/degu/has_set.rb
78
+ - lib/degu/polite.rb
79
+ - lib/degu/renum/enumerated_value.rb
80
+ - lib/degu/renum/enumerated_value_type_factory.rb
81
+ - lib/degu/renum.rb
82
+ - lib/degu/rude.rb
83
+ - lib/degu/version.rb
84
+ - lib/degu.rb
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - README.rdoc
89
+ - Rakefile
90
+ - VERSION
91
+ - degu.gemspec
92
+ - lib/degu.rb
93
+ - lib/degu/has_enum.rb
94
+ - lib/degu/has_set.rb
95
+ - lib/degu/polite.rb
96
+ - lib/degu/renum.rb
97
+ - lib/degu/renum/enumerated_value.rb
98
+ - lib/degu/renum/enumerated_value_type_factory.rb
99
+ - lib/degu/rude.rb
100
+ - lib/degu/version.rb
101
+ - spec/renum_spec.rb
102
+ - spec/spec_helper.rb
103
+ - test/has_enum_test.rb
104
+ - test/has_set_test.rb
105
+ - test/test_helper.rb
106
+ - test_helper.rb
107
+ homepage: http://github.com/caroo/degu
108
+ licenses: []
109
+ post_install_message:
110
+ rdoc_options:
111
+ - --title
112
+ - Degu - Library for enums and bitfield sets.
113
+ - --main
114
+ - README.rdoc
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.11
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Library for enums and bitfield sets.
135
+ test_files:
136
+ - test/has_enum_test.rb
137
+ - test/has_set_test.rb