has_options 0.2.0 → 0.3.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.1
data/has_options.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_options}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["James Stuart"]
@@ -17,22 +17,40 @@ module HasOptionsHash
17
17
 
18
18
  class_eval <<-EOV
19
19
  define_method "find_#{singular_association}_value" do |name|
20
- (opt = association_name.find_by_name(name.to_s)) ? opt.value : nil
20
+ (opt = #{association_name}.find_by_name(name.to_s)) ? opt.value : nil
21
21
  end
22
22
 
23
23
  define_method "find_#{singular_association}_values" do |name|
24
- association_name.find_all_by_name(name.to_s).collect(&:value)
24
+ #{association_name}.find_all_by_name(name.to_s).collect(&:value)
25
25
  end
26
26
 
27
27
  define_method "#{association_name}_hash" do
28
28
  option_hash = Hash.new {|h,k| h[k] = []}
29
29
 
30
- association_name.each do |option|
30
+ #{association_name}.each do |option|
31
31
  option_hash[k] << option.value.to_s
32
32
  end
33
33
 
34
34
  option_hash
35
35
  end
36
+
37
+ define_method "set_#{singular_association}!" do |name, *values|
38
+ name = name.to_s
39
+ existing = #{association_name}.find_all_by_name(name)
40
+ new_opts = []
41
+ values.each do |value|
42
+ if (match = existing.detect { |opt| opt.value == value})
43
+ existing.delete(match)
44
+ new_opts <<match
45
+ else
46
+ new_opts << #{association_name}.create!(:name => name, :value => value)
47
+ end
48
+ end
49
+
50
+ existing.each { |opt| opt.destroy }
51
+
52
+ return new_opts
53
+ end
36
54
  EOV
37
55
 
38
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_options
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Stuart