haz_enum 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGESET ADDED
@@ -0,0 +1,2 @@
1
+ - 0.4.0 [2001-06-28]
2
+ * you can now use :yml instead of :bitfield as field_type
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/haz_enum.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{haz_enum}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["thyphoon"]
12
- s.date = %q{2010-06-15}
12
+ s.date = %q{2010-06-28}
13
13
  s.description = %q{use has_set and has_enum in your ActiveRecord models if you want to have one (has_enum) value from a defined enumeration or more (has_set))}
14
14
  s.email = %q{andi@galaxycats.com}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ "CHANGESET",
22
23
  "LICENSE",
23
24
  "README.rdoc",
24
25
  "Rakefile",
data/lib/haz_enum/set.rb CHANGED
@@ -4,16 +4,10 @@ module HazEnum
4
4
 
5
5
  field_type = options.has_key?(:field_type) ? options[:field_type].to_s : "bitfield"
6
6
  set_column = options.has_key?(:column_name) ? options[:column_name].to_s : "#{set_name}_#{field_type}"
7
- enum_class = options.has_key?(:class_name) ? Object.const_get(options[:class_name].to_s.camelize) : Object.const_get(set_name.to_s.camelize)
7
+ enum_class = options.has_key?(:class_name) ? options[:class_name].to_s.camelize.constantize : set_name.to_s.camelize.constantize
8
8
 
9
- after_find "initialize_#{set_name}_from_bitfield"
10
- before_save "convert_#{set_name}_to_bitfield"
11
-
12
- enum_class.class_eval do
13
- def bitfield_index
14
- index + 1
15
- end
16
- end unless enum_class.method_defined?(:bitfield_index)
9
+ after_find "initialize_#{set_name}_from_#{field_type}"
10
+ before_save "convert_#{set_name}_to_#{field_type}"
17
11
 
18
12
  define_method("#{set_name}") do
19
13
  instance_variable_get("@#{set_name}")
@@ -31,28 +25,53 @@ module HazEnum
31
25
  end
32
26
 
33
27
  define_method("#{set_name}_changed?") do
34
- send("convert_#{set_name}_to_bitfield")
28
+ send("convert_#{set_name}_to_#{field_type}")
35
29
  send("#{set_column}_changed?")
36
30
  end
37
31
 
38
- define_method("convert_#{set_name}_to_bitfield") do
39
- self[set_column] = 0
40
- if send(set_name)
41
- send(set_name).each do |element|
42
- 2**element.bitfield_index & self[set_column] == 2**element.bitfield_index ? next : self[set_column] += 2**element.bitfield_index
32
+ if field_type == "bitfield"
33
+
34
+ enum_class.class_eval do
35
+ def bitfield_index
36
+ index + 1
37
+ end
38
+ end unless enum_class.method_defined?(:bitfield_index)
39
+
40
+ define_method("convert_#{set_name}_to_bitfield") do
41
+ self[set_column] = 0
42
+ if send(set_name)
43
+ send(set_name).each do |element|
44
+ 2**element.bitfield_index & self[set_column] == 2**element.bitfield_index ? next : self[set_column] += 2**element.bitfield_index
45
+ end
43
46
  end
44
47
  end
45
- end
46
48
 
47
- define_method("initialize_#{set_name}_from_bitfield") do
48
- if self[set_column]
49
- set_elements = enum_class.values.collect do |enum_element|
50
- enum_element if ((2**enum_element.bitfield_index & self[set_column]) == 2**enum_element.bitfield_index)
51
- end.compact
52
- send("#{set_name}=", set_elements)
53
- else
54
- self[set_column] = 0
49
+ define_method("initialize_#{set_name}_from_bitfield") do
50
+ if self[set_column]
51
+ set_elements = enum_class.values.collect do |enum_element|
52
+ enum_element if ((2**enum_element.bitfield_index & self[set_column]) == 2**enum_element.bitfield_index)
53
+ end.compact
54
+ send("#{set_name}=", set_elements)
55
+ else
56
+ self[set_column] = 0
57
+ end
58
+ end
59
+
60
+ elsif field_type == "yml"
61
+
62
+ define_method("convert_#{set_name}_to_yml") do
63
+ self[set_column] = YAML::dump(send(set_name))
64
+ end
65
+
66
+ define_method("initialize_#{set_name}_from_yml") do
67
+ deserialized_value = unless self[set_column].is_a?(String) && self[set_column] =~ /^---/
68
+ self[set_column]
69
+ else
70
+ (YAML::load(self[set_column]) || []) rescue self[set_column]
71
+ end
72
+ send("#{set_name}=", deserialized_value)
55
73
  end
74
+
56
75
  end
57
76
  end
58
77
  end
data/spec/set_spec.rb CHANGED
@@ -17,6 +17,13 @@ describe "HazEnum" do
17
17
  ClassWithSet.new(:roles => [Roles::User, Roles::Admin]).roles[1].should be(Roles::Admin)
18
18
  end
19
19
 
20
+ it "should be able to save values as yml" do
21
+ YmlSet.new(:roles => [Roles::User, Roles::Admin]).roles[1].should be(Roles::Admin)
22
+ yml_set = YmlSet.create(:roles => [Roles::User, Roles::Admin])
23
+ yml_set.reload
24
+ yml_set.roles[1].should be(Roles::Admin)
25
+ end
26
+
20
27
  it "should not be able to set set-values by colum-name via hash in initializer" do
21
28
  lambda { ClassWithEnum.new(:roles_bitfield => 3) }.should raise_error
22
29
  end
data/spec/spec_helper.rb CHANGED
@@ -30,6 +30,7 @@ def setup_db
30
30
  create_table :class_with_sets do |t|
31
31
  t.column :title, :string
32
32
  t.column :roles_bitfield, :integer
33
+ t.column :roles_yml, :text
33
34
  t.column :created_at, :datetime
34
35
  t.column :updated_at, :datetime
35
36
  end
@@ -75,6 +76,11 @@ class ClassWithSet < ActiveRecord::Base
75
76
  end
76
77
  end
77
78
 
79
+ class YmlSet < ActiveRecord::Base
80
+ set_table_name "class_with_sets"
81
+ has_set :roles, :field_type => :yml
82
+ end
83
+
78
84
  class ClassWithCustomNameSet < ActiveRecord::Base
79
85
  has_set :roles, :column_name => :custom_name
80
86
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - thyphoon
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-15 00:00:00 +02:00
17
+ date: 2010-06-28 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -58,6 +58,7 @@ extra_rdoc_files:
58
58
  files:
59
59
  - .document
60
60
  - .gitignore
61
+ - CHANGESET
61
62
  - LICENSE
62
63
  - README.rdoc
63
64
  - Rakefile