has_constant 0.7.1 → 0.7.2

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.7.1
1
+ 0.7.2
data/has_constant.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_constant}
8
- s.version = "0.7.1"
8
+ s.version = "0.7.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mattbeedle"]
@@ -24,8 +24,12 @@ module HasConstant
24
24
 
25
25
  class_eval do
26
26
  unless fields.map(&:first).include?(singular.to_s)
27
- type = (options[:as] == :array) ? Array : Integer
28
- field singular.to_sym, { :type => type }.merge(options)
27
+ if options[:as] == :array
28
+ field plural.to_sym, { :type => Array, :default => [] }.
29
+ merge(options)
30
+ else
31
+ field singular.to_sym, { :type => Integer }.merge(options)
32
+ end
29
33
  end
30
34
 
31
35
  index singular.to_sym, :background => true if options[:index]
@@ -38,37 +42,37 @@ module HasConstant
38
42
  if options[:as] == :array
39
43
  define_method("#{plural}=") do |value_set|
40
44
  indexes = value_set.map do |value|
41
- values.index(value)
45
+ self.class.send(plural).index(value)
42
46
  end
43
47
  write_attribute plural, indexes
44
48
  end
45
- else
46
- define_method("#{singular}=") do |val|
47
- if val.instance_of?(String)
48
- if index = self.class.send(name.to_s).index(val)
49
- write_attribute singular.to_sym, index
50
- elsif !val.blank?
51
- values = values.call if values.respond_to?(:call)
52
- @has_constant_errors ||= {}
53
- @has_constant_errors.merge!(singular.to_sym => "must be one of #{values.join(', ')}")
54
- end
55
- else
56
- write_attribute singular.to_sym, val
49
+ end
50
+
51
+ define_method("#{singular}=") do |val|
52
+ if val.instance_of?(String)
53
+ if index = self.class.send(name.to_s).index(val)
54
+ write_attribute singular.to_sym, index
55
+ elsif !val.blank?
56
+ values = values.call if values.respond_to?(:call)
57
+ @has_constant_errors ||= {}
58
+ @has_constant_errors.merge!(singular.to_sym => "must be one of #{values.join(', ')}")
57
59
  end
60
+ else
61
+ write_attribute singular.to_sym, val
58
62
  end
59
63
  end
60
64
 
61
65
  # Add the getter method. This returns the string representation of the stored value
62
66
  if options[:as] == :array
63
67
  define_method(plural) do
64
- attributes[plural].map { |val| values[val] }
65
- end
66
- else
67
- define_method(singular) do
68
- eval("#{self.class}.#{name.to_s}[self.attributes[singular].to_i] if self.attributes[singular]")
68
+ attributes[plural].map { |val| self.class.send(plural)[val] }
69
69
  end
70
70
  end
71
71
 
72
+ define_method(singular) do
73
+ eval("#{self.class}.#{name.to_s}[self.attributes[singular].to_i] if self.attributes[singular]")
74
+ end
75
+
72
76
  (class << self; self; end).instance_eval do
73
77
  if options[:as] == :array
74
78
  define_method "#{plural}_include".to_sym do |value|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_constant
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 1
10
- version: 0.7.1
9
+ - 2
10
+ version: 0.7.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - mattbeedle