Browse files

solved conflicts

  • Loading branch information...
2 parents fbb8adb + 11e3132 commit ce9fa4faf8f19b5e6d6afdaf3311225e4200171e @binarylogic committed Nov 18, 2009
Showing with 34 additions and 11 deletions.
  1. +3 −0 init.rb
  2. +10 −8 lib/enumlogic.rb
  3. +19 −2 spec/enumlogic_spec.rb
  4. +2 −1 spec/spec_helper.rb
View
3 init.rb
@@ -0,0 +1,3 @@
+require 'enumlogic'
+
+ActiveRecord::Base.extend(Enumlogic)
View
18 lib/enumlogic.rb
@@ -11,14 +11,15 @@ module Enumlogic
#
# You can now do the following:
#
- # Computer::KINDS # passes back exactly what you specified, Array or Hash
+ # Computer::KINDS # passes back the defined enum keys as array
# Computer.kind_options # gives you a friendly hash that you can easily pass into the select helper for forms
# Computer.new(:kind => "unknown").valid? # false, automatically validates inclusion of the enum field
#
# c = Computer.new(:kind => "apple")
# c.apple? # true
- # c.apple_key # :apple
- # c.apple_text # "apple" or "Apple" if you gave a hash with a user friendly text value
+ # c.kind_key # :apple
+ # c.kind_text # "apple" or "Apple" if you gave a hash with a user friendly text value
+ # c.enum?(:kind) # true
def enum(field, values, options = {})
values_hash = if values.is_a?(Array)
hash = {}
@@ -30,7 +31,6 @@ def enum(field, values, options = {})
values_array = values.is_a?(Hash) ? values.keys : values
- message = options[:message] || "#{field} is not included in the list"
constant_name = options[:constant] || field.to_s.pluralize.upcase
const_set constant_name, values_array unless const_defined?(constant_name)
@@ -47,7 +47,7 @@ def enum(field, values, options = {})
define_method("#{field}_text") do
value = send(field)
return nil if value.nil?
- values_hash.find { |key, text| key == value }.last
+ values_hash[value]
end
values_array.each do |value|
@@ -58,8 +58,10 @@ def enum(field, values, options = {})
end
end
- validates_inclusion_of field, :in => values_array, :message => message, :allow_nil => options[:allow_nil], :if => options[:if]
+ validates_inclusion_of field, :in => values_array, :message => options[:message], :allow_nil => options[:allow_nil], :if => options[:if]
end
-end
-ActiveRecord::Base.extend Enumlogic
+ def enum?(name)
+ method_defined?("#{name}_key")
+ end
+end
View
21 spec/enumlogic_spec.rb
@@ -49,6 +49,12 @@
c = Computer.new(:kind => "hp")
c.kind_text.should == "HP"
end
+
+ it "should create text method which results nil for wrong key" do
+ Computer.enum :kind, {"apple" => "Apple", "dell" => "Dell", "hp" => "HP"}
+ c = Computer.new :kind => 'ibm'
+ c.kind_text.should == nil
+ end
it "should create boolean methods" do
Computer.enum :kind, ["apple", "dell", "hp"]
@@ -67,7 +73,7 @@
c = Computer.new
c.kind = "blah"
c.should_not be_valid
- c.errors[:kind].should include("kind is not included in the list")
+ c.errors[:kind].should include("is not included in the list")
end
it "should allow nil during validations" do
@@ -87,4 +93,15 @@
c = Computer.new(:kind => "custom made")
c.should be_valid
end
-end
+
+ it "should find a defined enum" do
+ Computer.enum :kind, ["apple", "dell", "hp"]
+
+ Computer.enum?(:kind).should == true
+ Computer.enum?(:some_other_field).should == false
+ end
+
+ it "should check for defined enums if there isn't any" do
+ Computer.enum?(:kind).should == false
+ end
+end
View
3 spec/spec_helper.rb
@@ -3,7 +3,8 @@
require 'spec'
require 'spec/autorun'
require 'rubygems'
-require 'enumlogic'
+require 'active_record'
+require "#{File.dirname(__FILE__)}/../init"
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Base.configurations = true

0 comments on commit ce9fa4f

Please sign in to comment.