enum_attr 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  .svn
2
+ pkg/*
data/Gemfile CHANGED
@@ -0,0 +1 @@
1
+ source "http://rubygems.org"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.3
data/enum_attr.gemspec ADDED
@@ -0,0 +1,45 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{enum_attr}
8
+ s.version = "0.0.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["qichunren"]
12
+ s.date = %q{2010-12-27}
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
+ s.email = %q{whyruby@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "Gemfile",
21
+ "MIT-LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "enum_attr.gemspec",
26
+ "init.rb",
27
+ "lib/enum_attr.rb"
28
+ ]
29
+ s.homepage = %q{https://github.com/qichunren/enum_attr}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.7}
33
+ s.summary = %q{enum_attr is a Ruby gem to manage enum column for active_record.}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ else
41
+ end
42
+ else
43
+ end
44
+ end
45
+
data/init.rb CHANGED
@@ -1,2 +1 @@
1
- require 'enum_attr'
2
- Object.send :include, EnumAttr::Mixin
1
+ require File.join(File.dirname(__FILE__), "lib", "enum_attr")
data/lib/enum_attr.rb CHANGED
@@ -1,17 +1,51 @@
1
1
  module EnumAttr
2
2
  module Mixin
3
3
  # for model Contract:
4
- # enum_attr :contract_type, [['拍品合同', '0', "auction"], ['商品合同', '1', "goods"], ["台湾合同", '2', "taiwan"]]
4
+ # enum_attr :status, [['新建', 0, "origin"], ['整理中', 1, "collecting"], ["已上传", 2, "uploaded"]]
5
+ # It generates these code for you:
6
+ # # status const
7
+ # STATUS_ORIGIN = 0; STATUS_COLLECTING = 2; STATUS_UPLOADED = 3
8
+ #
9
+ # # scopes for column status
10
+ # scope :status_origin, where("status=0")
11
+ # scope :status_collection, where("status=1")
12
+ # scope :status_uploaded, where("status=2")
13
+ #
14
+ # # for view select
15
+ # ENUMS_STATUS = [["新建", 0], ["整理中", 1], ["已上传", 2]]
16
+
17
+ # # validates
18
+ # validates_inclusion_of "status", :in => [1, 2, 3], :allow_nil => true
19
+ #
20
+ # # column_name instant method
21
+ # def status_origin
22
+ # # return status name by it's status value
23
+ # end
5
24
  #
6
25
  def enum_attr(attr, enums)
26
+ raise "The secone param must be a array!" unless enums.is_a? Array
27
+
7
28
  attr = attr.to_s
29
+
8
30
  enums.each do |enum|
9
31
  const_set("#{attr.upcase}_#{enum[2].upcase}", enum[1] )
10
- end
32
+
33
+ # TODO: how to judge if there is active_record 3 gem here?
34
+ scope "#{attr}_#{enum[2]}".to_sym, where("#{attr}=#{enum[1]}")
35
+
36
+ # TODO define_method such as "stauts_origin?"
37
+ define_method "#{attr}_#{enum}?" do
38
+ attr == enum[1]
39
+ end
40
+
41
+ end # end: enums.each
42
+
11
43
  self.class_eval(%Q{
12
44
 
13
45
  ENUMS_#{attr.upcase} = enums.collect{|item| [item[0], item[1]]}
14
- validates_inclusion_of attr, :in => enums.map{|e| e[1]}, :allow_nil => true
46
+
47
+ validates_inclusion_of attr, :in => enums.map{|e| e[1].to_i}, :allow_nil => true
48
+
15
49
  def #{attr}_name
16
50
  ENUMS_#{attr.upcase}.find{|option| option[1] == #{attr}}[0] unless #{attr}.nil?
17
51
  end
@@ -19,3 +53,5 @@ end
19
53
  end
20
54
  end
21
55
  end
56
+
57
+ Object.send :include, EnumAttr::Mixin
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 3
9
+ version: 0.0.3
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-11-12 00:00:00 +08:00
17
+ date: 2010-12-27 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -33,6 +33,7 @@ files:
33
33
  - README.rdoc
34
34
  - Rakefile
35
35
  - VERSION
36
+ - enum_attr.gemspec
36
37
  - init.rb
37
38
  - lib/enum_attr.rb
38
39
  has_rdoc: true