enum_attr 0.0.6 → 0.0.7

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/README.rdoc CHANGED
@@ -23,6 +23,9 @@ methods:
23
23
  end
24
24
 
25
25
 
26
+ = See more detail in spec test
27
+ 更多用法见spec/enum_spec.rb文件
28
+
26
29
  = Inspiration
27
30
  Thanks Quakewang
28
31
  http://quake.javaeye.com/blog/448235
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
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.6"
8
+ s.version = "0.0.7"
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{2010-12-28}
12
+ s.date = %q{2011-04-22}
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,15 +23,23 @@ Gem::Specification.new do |s|
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "enum_attr.gemspec",
26
+ "enum_attr_test.sqlite3",
26
27
  "init.rb",
27
28
  "lib/enum_attr.rb",
28
- "rails/init.rb"
29
+ "rails/init.rb",
30
+ "spec/database.yml",
31
+ "spec/enum_spec.rb",
32
+ "spec/spec_helper.rb"
29
33
  ]
30
34
  s.homepage = %q{https://github.com/qichunren/enum_attr}
31
35
  s.rdoc_options = ["--charset=UTF-8"]
32
36
  s.require_paths = ["lib"]
33
37
  s.rubygems_version = %q{1.3.7}
34
38
  s.summary = %q{enum_attr is a Ruby gem to manage enum column for active_record.}
39
+ s.test_files = [
40
+ "spec/enum_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
35
43
 
36
44
  if s.respond_to? :specification_version then
37
45
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
Binary file
data/lib/enum_attr.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  # encoding: utf-8
2
-
3
2
  module EnumAttr
4
3
  module Mixin
5
4
  # for model Contract:
6
5
  # enum_attr :status, [['新建', 0, "origin"], ['整理中', 1, "collecting"], ["已上传", 2, "uploaded"]]
7
6
  # It generates these code for you:
8
- # # status const
7
+ # # status const 状态常量
9
8
  # STATUS_ORIGIN = 0; STATUS_COLLECTING = 2; STATUS_UPLOADED = 3
10
9
  #
11
10
  # # scopes for column status
@@ -16,7 +15,7 @@ module EnumAttr
16
15
  # # for view select
17
16
  # ENUMS_STATUS = [["新建", 0], ["整理中", 1], ["已上传", 2]]
18
17
 
19
- # # validates
18
+ # # validates 包含验证
20
19
  # validates_inclusion_of "status", :in => [1, 2, 3], :allow_nil => true
21
20
  #
22
21
  # # column_name instant method
@@ -55,7 +54,7 @@ ENUMS_#{attr.upcase} = enums.collect{|item| [item[0], item[1]]}
55
54
  validates_inclusion_of attr, :in => enums.map{|e| e[1].to_i}, :allow_nil => true
56
55
 
57
56
  def self.#{attr}_name_by(arg)
58
- ENUMS_#{attr.upcase}.find{|option| option[1] == arg }[0]
57
+ ENUMS_#{attr.upcase}.find{|option| option[1] == arg }[0] rescue ""
59
58
  end
60
59
 
61
60
  def #{attr}_name
data/spec/database.yml ADDED
@@ -0,0 +1,4 @@
1
+ adapter: sqlite3
2
+ database: enum_attr_test.sqlite3
3
+ pool: 5
4
+ timeout: 5000
data/spec/enum_spec.rb ADDED
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ require File.dirname(__FILE__) + "/spec_helper"
3
+
4
+
5
+ class Contract < ActiveRecord::Base
6
+ enum_attr :status, [['新建', 0, "origin"], ['整理中', 1, "collecting"], ["已上传", 2, "uploaded"]]
7
+ end
8
+
9
+ describe "EnumAttr" do
10
+ after(:each) do
11
+ Contract.delete_all
12
+ end
13
+
14
+ it "should get const" do
15
+ Contract::STATUS_ORIGIN.should == 0
16
+ Contract::STATUS_COLLECTING.should == 1
17
+ Contract::STATUS_UPLOADED.should == 2
18
+ end
19
+
20
+ it "should get scope" do
21
+ Contract.should respond_to(:status_origin)
22
+ Contract.should respond_to(:status_collecting)
23
+ 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)
28
+ Contract.status_origin.first.name.should == "contract 1"
29
+ Contract.status_collecting.count.should == 2
30
+ end
31
+
32
+ it "should get status_#name#?" do
33
+ contract = Contract.create(:name => "测试合同", :status => 0)
34
+ contract.should respond_to(:status_origin?)
35
+ contract.should respond_to(:status_collecting?)
36
+ contract.should respond_to(:status_uploaded?)
37
+ contract.status_origin?.should == true
38
+ contract.status_uploaded?.should == false
39
+ end
40
+
41
+ it "should get const array" do
42
+ Contract::ENUMS_STATUS.should == [['新建', 0], ['整理中', 1], ["已上传", 2]]
43
+ end
44
+
45
+ it "should get status_name" do
46
+ contract = Contract.create(:name => "测试合同", :status => 0)
47
+ contract.status_name.should == "新建"
48
+ end
49
+
50
+ it "should get status_name_by" do
51
+ Contract.status_name_by(0).should == "新建"
52
+ Contract.status_name_by(1).should == "整理中"
53
+ Contract.status_name_by(2).should == "已上传"
54
+ Contract.status_name_by(3).should == ""
55
+ end
56
+
57
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'yaml'
5
+ require File.expand_path("../../lib/enum_attr", __FILE__)
6
+
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 7
9
+ version: 0.0.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - qichunren
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-28 00:00:00 +08:00
17
+ date: 2011-04-22 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -34,9 +34,13 @@ files:
34
34
  - Rakefile
35
35
  - VERSION
36
36
  - enum_attr.gemspec
37
+ - enum_attr_test.sqlite3
37
38
  - init.rb
38
39
  - lib/enum_attr.rb
39
40
  - rails/init.rb
41
+ - spec/database.yml
42
+ - spec/enum_spec.rb
43
+ - spec/spec_helper.rb
40
44
  has_rdoc: true
41
45
  homepage: https://github.com/qichunren/enum_attr
42
46
  licenses: []
@@ -69,5 +73,6 @@ rubygems_version: 1.3.7
69
73
  signing_key:
70
74
  specification_version: 3
71
75
  summary: enum_attr is a Ruby gem to manage enum column for active_record.
72
- test_files: []
73
-
76
+ test_files:
77
+ - spec/enum_spec.rb
78
+ - spec/spec_helper.rb