enumlogic 1.0.1 → 1.0.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/Rakefile CHANGED
@@ -13,6 +13,7 @@ begin
13
13
  gem.rubyforge_project = "enumlogic"
14
14
  gem.add_development_dependency "rspec"
15
15
  end
16
+ Jeweler::GemcutterTasks.new
16
17
  Jeweler::RubyforgeTasks.new
17
18
  rescue LoadError
18
19
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
data/VERSION.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
- :patch: 1
3
- :major: 1
4
2
  :minor: 0
3
+ :patch: 2
4
+ :major: 1
5
+ :build:
data/enumlogic.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{enumlogic}
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["binarylogic"]
12
- s.date = %q{2009-09-01}
12
+ s.date = %q{2009-11-18}
13
13
  s.description = %q{Adds enumerations to your models}
14
14
  s.email = %q{bjohnson@binarylogic.com}
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION.yml",
27
27
  "enumlogic.gemspec",
28
+ "init.rb",
28
29
  "lib/enumlogic.rb",
29
30
  "spec/enumlogic_spec.rb",
30
31
  "spec/spec_helper.rb"
@@ -53,3 +54,4 @@ Gem::Specification.new do |s|
53
54
  s.add_dependency(%q<rspec>, [">= 0"])
54
55
  end
55
56
  end
57
+
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'enumlogic'
2
+
3
+ ActiveRecord::Base.extend(Enumlogic)
data/lib/enumlogic.rb CHANGED
@@ -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 @@ module Enumlogic
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 @@ module Enumlogic
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 @@ module Enumlogic
58
58
  end
59
59
  end
60
60
 
61
- validates_inclusion_of field, :in => values_array, :message => message, :allow_nil => options[:allow_nil]
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
@@ -49,6 +49,12 @@ describe "Enumlogic" do
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 @@ describe "Enumlogic" do
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
@@ -75,4 +81,27 @@ describe "Enumlogic" do
75
81
  c = Computer.new
76
82
  c.should be_valid
77
83
  end
78
- end
84
+
85
+ it "should implement the if option during validation" do
86
+ Computer.enum :kind, ["apple", "dell", "hp"], :if => :return_false
87
+ c = Computer.new
88
+ c.should be_valid
89
+ end
90
+
91
+ it "should be included in the list" do
92
+ Computer.enum :kind, ["apple", "dell", "hp", "custom made"]
93
+ c = Computer.new(:kind => "custom made")
94
+ c.should be_valid
95
+ 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
data/spec/spec_helper.rb CHANGED
@@ -3,9 +3,10 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
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
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
10
  ActiveRecord::Base.configurations = true
10
11
 
11
12
  ActiveRecord::Schema.verbose = false
@@ -18,6 +19,10 @@ end
18
19
  Spec::Runner.configure do |config|
19
20
  config.before(:each) do
20
21
  class Computer < ActiveRecord::Base
22
+ private
23
+ def return_false
24
+ false
25
+ end
21
26
  end
22
27
  end
23
28
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumlogic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - binarylogic
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-01 00:00:00 -05:00
12
+ date: 2009-11-18 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,6 +40,7 @@ files:
40
40
  - Rakefile
41
41
  - VERSION.yml
42
42
  - enumlogic.gemspec
43
+ - init.rb
43
44
  - lib/enumlogic.rb
44
45
  - spec/enumlogic_spec.rb
45
46
  - spec/spec_helper.rb