enum_attribute 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,18 +21,58 @@ or write Gemfile and "bundle install"
21
21
 
22
22
  Usage
23
23
  -----
24
- #add in your model
25
- include EnumAttribute
24
+ #add in your model
25
+ include EnumAttribute
26
26
 
27
- #setting in a Aaa model
28
- #your_method is already defined.
29
- #ex.
30
- enum_attribute :(your_method), ['a','b','c']
27
+ #extend your method to model class
28
+ enum_attribute :test, ['a','b','c']
31
29
 
30
+ ex.
31
+ #class methods is defined.
32
+ Aaa.tests => ['a','b','c']
33
+ Aaa.test_names => [ t('activerecord.attributes.aaa.tests.a'),
34
+ t('activerecord.attributes.aaa.tests.b'),
35
+ t('activerecord.attributes.aaa.tests.c') ]
32
36
 
37
+ Aaa.test_name('a') => t('activerecord.attributes.aaa.tests.a')
38
+
39
+ Aaa.test_pairs => [[t('activerecord.attributes.aaa.tests.a'),a],
40
+ [t('activerecord.attributes.aaa.tests.b'),b],
41
+ [t('activerecord.attributes.aaa.tests.c'),c]]
42
+
43
+ test_pairs method is useful for select_tag
44
+ select @aaa, Aaa.test_pairs
45
+
46
+ #instance methods also is defined.
47
+ @aaa = Aaa.new(:test=>'a')
48
+ @aaa.test_name => t('activerecord.attributes.aaa.tests.a')
49
+
50
+
51
+ #@params [Hash] options
52
+ #@options options [boolean] :number(false)
53
+ if true, (name)_pairs method return number in 2nd array
54
+ and change column name :column => (name)_id automatically!
55
+ It will be useful for select type
56
+ ex.
57
+ seting in a model
58
+ enum_attribute :test, ['a','b','c'] :number => true
59
+ you can get
60
+ object.test_pairs => [[a,1],[b,2],[c,3]]
61
+
62
+
63
+ #@options options [string] :column_name(false) attached column name in table of DB
64
+
65
+ ex. :column =>
66
+ it will be set when name and column name is difference
67
+
68
+ #@options options [boolean] :i18n(true)
69
+ if i18n == false ,just return raw data
70
+
71
+
72
+
73
+ Contributing
74
+ ------------
33
75
 
34
- == Contributing to enum_attribute
35
-
36
76
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
37
77
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
38
78
  * Fork the project
@@ -41,7 +81,8 @@ Usage
41
81
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
42
82
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
43
83
 
44
- == Copyright
84
+ Copyright
85
+ ---------
45
86
 
46
87
  Copyright (c) 2011 Eiji Kosaki. See LICENSE.txt for
47
88
  further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{enum_attribute}
8
- s.version = "0.0.1"
8
+ s.version = "0.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 = [%q{Eiji Kosaki}]
12
- s.date = %q{2011-07-16}
12
+ s.date = %q{2011-07-27}
13
13
  s.description = %q{I'm going to describe detail later...}
14
14
  s.email = %q{monkeytohuman@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,52 +1,4 @@
1
1
 
2
- # usage
3
- # #add in your model
4
- # include EnumAttribute
5
- #
6
- # #seting in a Aaa model
7
- # enum_attribute :test, ['a','b','c']
8
- #
9
- # ex.
10
- # class methods is defined.
11
- # Aaa.tests => ['a','b','c']
12
- # Aaa.test_names => [ t('activerecord.attributes.aaa.tests.a'),
13
- # t('activerecord.attributes.aaa.tests.b'),
14
- # t('activerecord.attributes.aaa.tests.c') ]
15
- #
16
- # Aaa.test_name('a') => t('activerecord.attributes.aaa.tests.a')
17
- #
18
- # Aaa.test_pairs => [[t('activerecord.attributes.aaa.tests.a'),a],
19
- # [t('activerecord.attributes.aaa.tests.b'),b],
20
- # [t('activerecord.attributes.aaa.tests.c'),c]]
21
- #
22
- # test_pairs method is useful for select_tag
23
- # select @aaa, Aaa.test_pairs
24
- #
25
- # instance methods also is defined.
26
- # @aaa = Aaa.new(:test=>'a')
27
- # @aaa.test_name => t('activerecord.attributes.aaa.tests.a')
28
- #
29
- #
30
- # @params [Hash] options
31
- # @options options [boolean] :number(false)
32
- # if true, (name)_pairs method return number in 2nd array
33
- # and change column name :column => (name)_id automatically!
34
- # It will be useful for select type
35
- # ex.
36
- # seting in a model
37
- # enum_attribute :test, ['a','b','c'] :number => true
38
- # you can get
39
- # object.test_pairs => [[a,1],[b,2],[c,3]]
40
- #
41
- #
42
- # @options options [string] :column_name(false) attached column name in table of DB
43
- #
44
- # ex. :column =>
45
- # it will be set when name and column name is difference
46
- #
47
- # @options options [boolean] :i18n(true)
48
- # if i18n == false ,just return raw data
49
- #
50
2
  module EnumAttribute
51
3
  def self.included target
52
4
  target.class_eval do
@@ -81,11 +33,13 @@ module EnumAttribute
81
33
  end
82
34
  end
83
35
  define_method "#{name}_name" do |col|
84
- return "" unless col
85
- if col.to_i==0
86
- [self.i18nt(col,pluralized)]
36
+ return "" if col.blank?
37
+ if col.to_i==0 #0 and char -> true
38
+ return "" unless values.any?{|v| v == col.to_s}
39
+ self.i18nt(col,pluralized)
87
40
  else
88
- [self.i18nt(values[col - 1],pluralized)]
41
+ return "" if values.size < col.to_i
42
+ self.i18nt(values[col - 1],pluralized)
89
43
  end
90
44
  end
91
45
  def i18nt(value,pluralized)
@@ -102,21 +56,16 @@ module EnumAttribute
102
56
  column_name = name
103
57
  column_name = opts[:column_name] if opts[:column_name]
104
58
  if opts[:number]
105
- if __send__(column_name) #reject nil in column
106
- values[__send__(column_name) - 1]
107
- else
108
- ""
109
- end
59
+ return "" if __send__(column_name).to_i - 1 < 0
60
+ values[__send__(column_name).to_i - 1]
110
61
  else
111
62
  column_name
112
63
  end
113
64
  end
114
65
 
115
-
116
66
  # if locale is just variable,it'll cause to error
117
67
  # So array(*locale) can make parameter nothing.
118
68
  # ex. foo.val_name
119
- #
120
69
  define_method "#{name}_name" do |*locale|
121
70
  column_name = name
122
71
  column_name = opts[:column_name] if opts[:column_name]
@@ -139,7 +88,6 @@ module EnumAttribute
139
88
  i18n_scope = "activerecord"
140
89
  i18n_scope = self.i18n_scope.to_s if defined?(self.i18n_scope) #rails2 support i18n_scope
141
90
  I18n.t("#{i18n_scope}.attributes.#{self.model_name.to_s.underscore}.#{pluralized}.#{value}",:locale=>l)
142
-
143
91
  end
144
92
 
145
93
  # I want to use for checkbox type ,but indeed its hard to manage to store&read with Mysql
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: enum_attribute
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Eiji Kosaki
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-16 00:00:00 Z
13
+ date: 2011-07-27 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
104
  - - ">="
105
105
  - !ruby/object:Gem::Version
106
- hash: 2777831526094708268
106
+ hash: -827134506953628287
107
107
  segments:
108
108
  - 0
109
109
  version: "0"