haz_enum 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/haz_enum.gemspec +2 -2
  3. data/lib/haz_enum/set.rb +10 -7
  4. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
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.4.0"
8
+ s.version = "0.4.1"
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-28}
12
+ s.date = %q{2010-06-29}
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 = [
data/lib/haz_enum/set.rb CHANGED
@@ -5,16 +5,22 @@ module HazEnum
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
7
  enum_class = options.has_key?(:class_name) ? options[:class_name].to_s.camelize.constantize : set_name.to_s.camelize.constantize
8
+ separator = options.has_key?(:separator) ? options[:separator].to_s : ", "
8
9
 
9
10
  after_find "initialize_#{set_name}_from_#{field_type}"
10
11
  before_save "convert_#{set_name}_to_#{field_type}"
11
12
 
12
13
  define_method("#{set_name}") do
13
- instance_variable_get("@#{set_name}")
14
+ instance_variable_get("@#{set_name}") || "initialize_#{set_name}_from_#{field_type}"
14
15
  end
15
16
 
16
17
  define_method("#{set_name}=") do |value|
18
+ value.collect! { |val| val.is_a?(String) ? val.constantize : val }.compact! if value.is_a?(Array)
19
+ value.instance_variable_set("@separator", separator)
17
20
  class <<value
21
+ define_method :to_s do
22
+ self.join(@separator)
23
+ end
18
24
  yield if block_given?
19
25
  end
20
26
  instance_variable_set("@#{set_name}", value)
@@ -39,7 +45,7 @@ module HazEnum
39
45
 
40
46
  define_method("convert_#{set_name}_to_bitfield") do
41
47
  self[set_column] = 0
42
- if send(set_name)
48
+ unless send(set_name).blank?
43
49
  send(set_name).each do |element|
44
50
  2**element.bitfield_index & self[set_column] == 2**element.bitfield_index ? next : self[set_column] += 2**element.bitfield_index
45
51
  end
@@ -54,6 +60,7 @@ module HazEnum
54
60
  send("#{set_name}=", set_elements)
55
61
  else
56
62
  self[set_column] = 0
63
+ send("#{set_name}=", [])
57
64
  end
58
65
  end
59
66
 
@@ -64,11 +71,7 @@ module HazEnum
64
71
  end
65
72
 
66
73
  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
74
+ deserialized_value = (YAML::load(self[set_column]) || [])
72
75
  send("#{set_name}=", deserialized_value)
73
76
  end
74
77
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
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-28 00:00:00 +02:00
17
+ date: 2010-06-29 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency