enum_attr 0.0.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  .svn
2
2
  pkg/*
3
+ spec/enum_attr_test.sqlite3
data/Gemfile CHANGED
@@ -1 +1,8 @@
1
- source "http://rubygems.org"
1
+ source "http://rubygems.org"
2
+
3
+ gem "activerecord", :require => "active_record"
4
+
5
+ group :test do
6
+ gem "rspec", "> 2.0"
7
+ gem "sqlite3"
8
+ end
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require "rubygems"
2
- require 'rake'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
3
4
 
4
5
  begin
5
6
  require 'jeweler'
@@ -15,4 +16,11 @@ begin
15
16
 
16
17
  rescue LoadError
17
18
  puts "Jeweler not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ task :default => :spec
22
+
23
+ RSpec::Core::RakeTask.new(:spec) do |t|
24
+ t.pattern = Dir.glob('spec/**/*_spec.rb')
25
+ t.rspec_opts = '--format progress -c'
18
26
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 1.0.0
data/enum_attr.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{enum_attr}
8
- s.version = "0.0.7"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["qichunren"]
12
- s.date = %q{2011-04-22}
12
+ s.date = %q{2012-09-05}
13
13
  s.description = %q{A Rails plugin which brings easy-to-use enum-like functionality to ActiveRecord models (now compatible with rails 3, ruby 1.9 and jruby). .}
14
14
  s.email = %q{whyruby@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -23,13 +23,9 @@ Gem::Specification.new do |s|
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "enum_attr.gemspec",
26
- "enum_attr_test.sqlite3",
27
26
  "init.rb",
28
27
  "lib/enum_attr.rb",
29
- "rails/init.rb",
30
- "spec/database.yml",
31
- "spec/enum_spec.rb",
32
- "spec/spec_helper.rb"
28
+ "rails/init.rb"
33
29
  ]
34
30
  s.homepage = %q{https://github.com/qichunren/enum_attr}
35
31
  s.rdoc_options = ["--charset=UTF-8"]
data/lib/enum_attr.rb CHANGED
@@ -1,71 +1,2 @@
1
- # encoding: utf-8
2
- module EnumAttr
3
- module Mixin
4
- # for model Contract:
5
- # enum_attr :status, [['新建', 0, "origin"], ['整理中', 1, "collecting"], ["已上传", 2, "uploaded"]]
6
- # It generates these code for you:
7
- # # status const 状态常量
8
- # STATUS_ORIGIN = 0; STATUS_COLLECTING = 2; STATUS_UPLOADED = 3
9
- #
10
- # # scopes for column status
11
- # scope :status_origin, where("status=0")
12
- # scope :status_collection, where("status=1")
13
- # scope :status_uploaded, where("status=2")
14
- #
15
- # # for view select
16
- # ENUMS_STATUS = [["新建", 0], ["整理中", 1], ["已上传", 2]]
17
-
18
- # # validates 包含验证
19
- # validates_inclusion_of "status", :in => [1, 2, 3], :allow_nil => true
20
- #
21
- # # column_name instant method
22
- # def status_origin
23
- # # return status name by it's status value
24
- # end
25
- #
26
- def enum_attr(attr, enums)
27
- raise "The secone param must be a array!" unless enums.is_a? Array
28
-
29
- attr = attr.to_s
30
-
31
- enums.each do |enum|
32
- const_set("#{attr.upcase}_#{enum[2].upcase}", enum[1] )
33
-
34
- # TODO: how to judge if there is active_record 3 gem here?
35
- scope "#{attr}_#{enum[2]}".to_sym, where("#{attr}=#{enum[1]}")
36
-
37
- # # TODO define_method such as "stauts_origin?"
38
- # why does this get error result?
39
- # define_method "#{attr}_#{enum[2]}?" do
40
- # attr == enum[1]
41
- # end
42
- class_eval(%Q{
43
- def #{attr}_#{enum[2]}?
44
- #{attr} == #{enum[1]}
45
- end
46
- })
47
-
48
- end # end: enums.each
49
-
50
- self.class_eval(%Q{
51
-
52
- ENUMS_#{attr.upcase} = enums.collect{|item| [item[0], item[1]]}
53
-
54
- validates_inclusion_of attr, :in => enums.map{|e| e[1].to_i}, :allow_nil => true
55
-
56
- def self.#{attr}_name_by(arg)
57
- ENUMS_#{attr.upcase}.find{|option| option[1] == arg }[0] rescue ""
58
- end
59
-
60
- def #{attr}_name
61
- ENUMS_#{attr.upcase}.find{|option| option[1] == #{attr}}[0] unless #{attr}.nil?
62
- end
63
-
64
- })
65
- end
66
-
67
-
68
- end
69
- end
70
-
71
- Object.send :include, EnumAttr::Mixin
1
+ require "enum_attr_base"
2
+ require "enum_attr_for_active_record"
data/spec/enum_spec.rb CHANGED
@@ -1,34 +1,66 @@
1
- # encoding: utf-8
2
- require File.dirname(__FILE__) + "/spec_helper"
1
+ # encoding: utf-8
2
+ require File.dirname(__FILE__) + "/spec_helper"
3
+ require "active_record"
4
+
5
+
6
+ dbconfig = YAML::load(File.open(File.dirname(__FILE__) + "/database.yml"))
7
+ ActiveRecord::Base.establish_connection(dbconfig)
8
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
9
+
10
+ #migrations
11
+ class CreateAllTables < ActiveRecord::Migration
12
+ def self.up
13
+ create_table(:contracts) do |t|
14
+ t.string :name
15
+ t.integer :status
16
+ end
17
+ end
18
+ end
19
+
20
+ RSpec.configure do |config|
21
+ config.before :all do
22
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'contracts'
23
+ end
24
+ end
3
25
 
4
26
 
5
27
  class Contract < ActiveRecord::Base
6
28
  enum_attr :status, [['新建', 0, "origin"], ['整理中', 1, "collecting"], ["已上传", 2, "uploaded"]]
7
29
  end
8
30
 
9
- describe "EnumAttr" do
31
+ describe "EnumAttr" do
10
32
  after(:each) do
11
33
  Contract.delete_all
12
34
  end
13
-
35
+
36
+ it "should get superclass" do
37
+ Contract.superclass.should == ActiveRecord::Base
38
+ end
39
+
14
40
  it "should get const" do
15
41
  Contract::STATUS_ORIGIN.should == 0
16
42
  Contract::STATUS_COLLECTING.should == 1
17
43
  Contract::STATUS_UPLOADED.should == 2
18
44
  end
19
-
20
- it "should get scope" do
45
+
46
+ it "should get scope" do
21
47
  Contract.should respond_to(:status_origin)
22
48
  Contract.should respond_to(:status_collecting)
23
49
  Contract.should respond_to(:status_uploaded)
24
- Contract.create(:name => "contract 1", :status => 0)
25
- Contract.create(:name => "contract 2", :status => 1)
26
- Contract.create(:name => "contract 3", :status => 2)
27
- Contract.create(:name => "contract 4", :status => 1)
50
+ Contract.create!(:name => "contract 1", :status => 0)
51
+ Contract.create!(:name => "contract 2", :status => 1)
52
+ Contract.create!(:name => "contract 3", :status => 2)
53
+ Contract.create!(:name => "contract 4", :status => 1)
28
54
  Contract.status_origin.first.name.should == "contract 1"
29
- Contract.status_collecting.count.should == 2
55
+ Contract.status_collecting.count.should == 2
56
+ end
57
+
58
+ it "should return origanl value if not matched" do
59
+ contract_with_error_status = Contract.new(:name => "contract with no correct status value", :status => 100000)
60
+ contract_with_error_status.save(:validate => false)
61
+ Contract.first.status_name.should == ""
30
62
  end
31
-
63
+
32
64
  it "should get status_#name#?" do
33
65
  contract = Contract.create(:name => "测试合同", :status => 0)
34
66
  contract.should respond_to(:status_origin?)
@@ -37,21 +69,21 @@ describe "EnumAttr" do
37
69
  contract.status_origin?.should == true
38
70
  contract.status_uploaded?.should == false
39
71
  end
40
-
72
+
41
73
  it "should get const array" do
42
74
  Contract::ENUMS_STATUS.should == [['新建', 0], ['整理中', 1], ["已上传", 2]]
43
75
  end
44
-
76
+
45
77
  it "should get status_name" do
46
78
  contract = Contract.create(:name => "测试合同", :status => 0)
47
79
  contract.status_name.should == "新建"
48
80
  end
49
-
50
- it "should get status_name_by" do
81
+
82
+ it "should get status_name_by" do
51
83
  Contract.status_name_by(0).should == "新建"
52
84
  Contract.status_name_by(1).should == "整理中"
53
85
  Contract.status_name_by(2).should == "已上传"
54
- Contract.status_name_by(3).should == ""
86
+ Contract.status_name_by(3).should == ""
55
87
  end
56
88
 
57
89
  end
data/spec/spec_helper.rb CHANGED
@@ -1,27 +1,6 @@
1
1
  # encoding: utf-8
2
- require 'rubygems'
3
- require 'active_record'
4
- require 'yaml'
5
- require File.expand_path("../../lib/enum_attr", __FILE__)
2
+ require 'yaml'
3
+ require 'rspec'
4
+ require 'logger'
6
5
 
7
- dbconfig = YAML::load(File.open(File.dirname(__FILE__) + "/database.yml"))
8
- ActiveRecord::Base.establish_connection(dbconfig)
9
-
10
- #migrations
11
- class CreateAllTables < ActiveRecord::Migration
12
- def self.up
13
- create_table(:contracts) do |t|
14
- t.string :name
15
- t.integer :status
16
- end
17
- end
18
- end
19
-
20
- RSpec.configure do |config|
21
- config.before :all do
22
- # ActiveRecord::Base.connection.execute
23
- # 'CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255))'
24
- # unless ActiveRecord::Base.connection.table_exists? 'users'
25
- CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'contracts'
26
- end
27
- end
6
+ require File.expand_path("../../lib/enum_attr", __FILE__)
metadata CHANGED
@@ -1,32 +1,24 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: enum_attr
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 7
9
- version: 0.0.7
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - qichunren
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-04-22 00:00:00 +08:00
18
- default_executable:
12
+ date: 2012-09-05 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
- description: A Rails plugin which brings easy-to-use enum-like functionality to ActiveRecord models (now compatible with rails 3, ruby 1.9 and jruby). .
14
+ description: A Rails plugin which brings easy-to-use enum-like functionality to ActiveRecord
15
+ models (now compatible with rails 3, ruby 1.9 and jruby). .
22
16
  email: whyruby@gmail.com
23
17
  executables: []
24
-
25
18
  extensions: []
26
-
27
- extra_rdoc_files:
19
+ extra_rdoc_files:
28
20
  - README.rdoc
29
- files:
21
+ files:
30
22
  - .gitignore
31
23
  - Gemfile
32
24
  - MIT-LICENSE
@@ -34,45 +26,37 @@ files:
34
26
  - Rakefile
35
27
  - VERSION
36
28
  - enum_attr.gemspec
37
- - enum_attr_test.sqlite3
38
29
  - init.rb
39
30
  - lib/enum_attr.rb
40
31
  - rails/init.rb
41
- - spec/database.yml
42
32
  - spec/enum_spec.rb
43
33
  - spec/spec_helper.rb
44
- has_rdoc: true
45
34
  homepage: https://github.com/qichunren/enum_attr
46
35
  licenses: []
47
-
48
36
  post_install_message:
49
- rdoc_options:
37
+ rdoc_options:
50
38
  - --charset=UTF-8
51
- require_paths:
39
+ require_paths:
52
40
  - lib
53
- required_ruby_version: !ruby/object:Gem::Requirement
41
+ required_ruby_version: !ruby/object:Gem::Requirement
54
42
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- segments:
59
- - 0
60
- version: "0"
61
- required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
48
  none: false
63
- requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- segments:
67
- - 0
68
- version: "0"
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
69
53
  requirements: []
70
-
71
54
  rubyforge_project:
72
- rubygems_version: 1.3.7
55
+ rubygems_version: 1.8.24
73
56
  signing_key:
74
57
  specification_version: 3
75
58
  summary: enum_attr is a Ruby gem to manage enum column for active_record.
76
- test_files:
59
+ test_files:
77
60
  - spec/enum_spec.rb
78
61
  - spec/spec_helper.rb
62
+ has_rdoc:
Binary file
data/spec/database.yml DELETED
@@ -1,4 +0,0 @@
1
- adapter: sqlite3
2
- database: enum_attr_test.sqlite3
3
- pool: 5
4
- timeout: 5000