Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

solved conflicts

  • Loading branch information...
commit ce9fa4faf8f19b5e6d6afdaf3311225e4200171e 2 parents fbb8adb + 11e3132
Ben Johnson yourewelcome authored
3  init.rb
... ... @@ -0,0 +1,3 @@
  1 +require 'enumlogic'
  2 +
  3 +ActiveRecord::Base.extend(Enumlogic)
18 lib/enumlogic.rb
@@ -11,14 +11,15 @@ module Enumlogic
11 11 #
12 12 # You can now do the following:
13 13 #
14   - # Computer::KINDS # passes back exactly what you specified, Array or Hash
  14 + # Computer::KINDS # passes back the defined enum keys as array
15 15 # Computer.kind_options # gives you a friendly hash that you can easily pass into the select helper for forms
16 16 # Computer.new(:kind => "unknown").valid? # false, automatically validates inclusion of the enum field
17 17 #
18 18 # c = Computer.new(:kind => "apple")
19 19 # c.apple? # true
20   - # c.apple_key # :apple
21   - # c.apple_text # "apple" or "Apple" if you gave a hash with a user friendly text value
  20 + # c.kind_key # :apple
  21 + # c.kind_text # "apple" or "Apple" if you gave a hash with a user friendly text value
  22 + # c.enum?(:kind) # true
22 23 def enum(field, values, options = {})
23 24 values_hash = if values.is_a?(Array)
24 25 hash = {}
@@ -30,7 +31,6 @@ def enum(field, values, options = {})
30 31
31 32 values_array = values.is_a?(Hash) ? values.keys : values
32 33
33   - message = options[:message] || "#{field} is not included in the list"
34 34 constant_name = options[:constant] || field.to_s.pluralize.upcase
35 35 const_set constant_name, values_array unless const_defined?(constant_name)
36 36
@@ -47,7 +47,7 @@ def enum(field, values, options = {})
47 47 define_method("#{field}_text") do
48 48 value = send(field)
49 49 return nil if value.nil?
50   - values_hash.find { |key, text| key == value }.last
  50 + values_hash[value]
51 51 end
52 52
53 53 values_array.each do |value|
@@ -58,8 +58,10 @@ def enum(field, values, options = {})
58 58 end
59 59 end
60 60
61   - validates_inclusion_of field, :in => values_array, :message => message, :allow_nil => options[:allow_nil], :if => options[:if]
  61 + validates_inclusion_of field, :in => values_array, :message => options[:message], :allow_nil => options[:allow_nil], :if => options[:if]
62 62 end
63   -end
64 63
65   -ActiveRecord::Base.extend Enumlogic
  64 + def enum?(name)
  65 + method_defined?("#{name}_key")
  66 + end
  67 +end
21 spec/enumlogic_spec.rb
@@ -49,6 +49,12 @@
49 49 c = Computer.new(:kind => "hp")
50 50 c.kind_text.should == "HP"
51 51 end
  52 +
  53 + it "should create text method which results nil for wrong key" do
  54 + Computer.enum :kind, {"apple" => "Apple", "dell" => "Dell", "hp" => "HP"}
  55 + c = Computer.new :kind => 'ibm'
  56 + c.kind_text.should == nil
  57 + end
52 58
53 59 it "should create boolean methods" do
54 60 Computer.enum :kind, ["apple", "dell", "hp"]
@@ -67,7 +73,7 @@
67 73 c = Computer.new
68 74 c.kind = "blah"
69 75 c.should_not be_valid
70   - c.errors[:kind].should include("kind is not included in the list")
  76 + c.errors[:kind].should include("is not included in the list")
71 77 end
72 78
73 79 it "should allow nil during validations" do
@@ -87,4 +93,15 @@
87 93 c = Computer.new(:kind => "custom made")
88 94 c.should be_valid
89 95 end
90   -end
  96 +
  97 + it "should find a defined enum" do
  98 + Computer.enum :kind, ["apple", "dell", "hp"]
  99 +
  100 + Computer.enum?(:kind).should == true
  101 + Computer.enum?(:some_other_field).should == false
  102 + end
  103 +
  104 + it "should check for defined enums if there isn't any" do
  105 + Computer.enum?(:kind).should == false
  106 + end
  107 +end
3  spec/spec_helper.rb
@@ -3,7 +3,8 @@
3 3 require 'spec'
4 4 require 'spec/autorun'
5 5 require 'rubygems'
6   -require 'enumlogic'
  6 +require 'active_record'
  7 +require "#{File.dirname(__FILE__)}/../init"
7 8
8 9 ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9 10 ActiveRecord::Base.configurations = true

0 comments on commit ce9fa4f

Please sign in to comment.
Something went wrong with that request. Please try again.