enum_attr 1.0.0 → 1.0.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/enum_attr.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{enum_attr}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["qichunren"]
@@ -25,6 +25,8 @@ Gem::Specification.new do |s|
25
25
  "enum_attr.gemspec",
26
26
  "init.rb",
27
27
  "lib/enum_attr.rb",
28
+ "lib/enum_attr_base.rb",
29
+ "lib/enum_attr_for_active_record.rb",
28
30
  "rails/init.rb"
29
31
  ]
30
32
  s.homepage = %q{https://github.com/qichunren/enum_attr}
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ module EnumAttr
3
+ module Mixin
4
+
5
+ def enum_attr(attr, enums)
6
+ raise "The secone param must be a array!" unless enums.is_a? Array
7
+
8
+ if self.superclass == Object
9
+ attr_accessor attr.to_sym
10
+ end
11
+
12
+ attr = attr.to_s
13
+
14
+ enums.each do |enum|
15
+ const_set("#{attr.upcase}_#{enum[2].upcase}", enum[1] )
16
+
17
+ class_eval(%Q{
18
+ def #{attr}_#{enum[2]}?
19
+ #{attr} == #{enum[1]}
20
+ end
21
+ })
22
+
23
+ end # end: enums.each
24
+
25
+ self.class_eval(%Q{
26
+
27
+ ENUMS_#{attr.upcase} = enums.collect{|item| [item[0], item[1]]}
28
+
29
+ def self.#{attr}_name_by(arg)
30
+ ENUMS_#{attr.upcase}.find{|option| option[1] == arg }[0] rescue ""
31
+ end
32
+
33
+ def #{attr}_name
34
+ ENUMS_#{attr.upcase}.find{|option| option[1] == #{attr}}[0] rescue "" unless #{attr}.nil?
35
+ end
36
+
37
+ })
38
+ end
39
+
40
+
41
+ end
42
+ end
43
+
44
+ Object.send :extend, EnumAttr::Mixin
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require 'enum_attr_base'
3
+ require "active_record"
4
+
5
+ module EnumAttr
6
+ module MixinForActiveRecord
7
+ include EnumAttr::Mixin
8
+
9
+ def enum_attr(attr, enums)
10
+ super(attr, enums)
11
+ enums.each do |enum|
12
+ scope "#{attr}_#{enum[2]}".to_sym, where("#{attr}=#{enum[1]}")
13
+ end # end: enums.each
14
+
15
+ self.class_eval(%Q{
16
+ validates_inclusion_of attr, :in => enums.map{|e| e[1].to_i}, :allow_nil => true
17
+ def attr
18
+ read_attribute(attr.to_sym)
19
+ end
20
+
21
+ def attr=(value)
22
+ write_attribute(attr.to_sym, value)
23
+ end
24
+ })
25
+
26
+ end # end def
27
+ end
28
+ end
29
+
30
+
31
+ ::ActiveRecord::Base.send :extend, EnumAttr::MixinForActiveRecord
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_attr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -28,6 +28,8 @@ files:
28
28
  - enum_attr.gemspec
29
29
  - init.rb
30
30
  - lib/enum_attr.rb
31
+ - lib/enum_attr_base.rb
32
+ - lib/enum_attr_for_active_record.rb
31
33
  - rails/init.rb
32
34
  - spec/enum_spec.rb
33
35
  - spec/spec_helper.rb