enumattr-ext 0.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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in enumattr-ext.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 aisuii
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.ja.md ADDED
@@ -0,0 +1,87 @@
1
+ # Enumattr::Ext
2
+
3
+ enumattr (https://github.com/aisuii/enumattr) のエクステンションです。
4
+
5
+ ## 導入
6
+
7
+ bundler を利用しているなら、Gemfile に以下の行を加え、
8
+
9
+ gem 'enumattr-ext'
10
+
11
+ 以下のコマンドを実行します。
12
+
13
+ $ bundle
14
+
15
+ あるいは自身でインストールすることもできます。
16
+
17
+ $ gem install enumattr-ext
18
+
19
+ ## 使い方
20
+
21
+ ### 例:
22
+
23
+ Ruby のコードで以下のように書いて、
24
+
25
+ class User
26
+ include Enumattr::Base
27
+ include Enumattr::Ext::Name
28
+
29
+ attr_accessor :status
30
+
31
+ enumattr :status do
32
+ enum :active, 1
33
+ enum :inactive, 2
34
+ enum :deleted, 3
35
+ end
36
+
37
+ def initialize(status)
38
+ @status = status
39
+ end
40
+ end
41
+
42
+ I18n の locales を以下のように用意して、
43
+
44
+ ja:
45
+ enumattr:
46
+ User:
47
+ status:
48
+ active: 有効
49
+ inactive: 無効
50
+ deleted: 削除
51
+
52
+ I18n のロードパスにいれます。
53
+ (Rails なら置くべきところに置いて、Rails から見えるようになっていればよい)
54
+
55
+ I18n.load_path = ['path/to/locales.yml']
56
+ I18n.locale = :ja
57
+
58
+ すると、以下のようなクラスメソッドとインスタンスメソッドが定義されます。
59
+
60
+ User.status_names
61
+ # => ["有効", "無効", "削除"]
62
+
63
+ User.status_name(:active)
64
+ # => "有効"
65
+
66
+ User.status_options
67
+ #=> [["有効", 1], ["無効", 2], ["削除", 3]]
68
+
69
+ user = User.new(1)
70
+ user.status_name
71
+ # => "有効"
72
+
73
+ 素の enumattr で既に用意されているものについては https://github.com/aisuii/enumattr/blob/master/README.ja.md を参照してください。
74
+
75
+ ## その他の例
76
+
77
+ _examples/*.rb_ と _spec/enumattr-ext/*.rb_ を参照してください。
78
+
79
+ ## ご意見・ご指導
80
+
81
+ ご意見やご指導よろしくおねがいします。
82
+
83
+ 1. Fork して
84
+ 2. feature branch を作って (`git checkout -b my-new-feature`)
85
+ 3. コミットして (`git commit -am 'Added some feature'`)
86
+ 4. branch を push して (`git push origin my-new-feature`)
87
+ 5. Pull Request してください
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Enumattr::Ext
2
+
3
+ enumattr (https://github.com/aisuii/enumattr) extensions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'enumattr-ext'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install enumattr-ext
18
+
19
+ ## Usage
20
+
21
+ ### example:
22
+
23
+ Ruby code:
24
+
25
+ class User
26
+ include Enumattr::Base
27
+ include Enumattr::Ext::Name
28
+
29
+ attr_accessor :status
30
+
31
+ enumattr :status do
32
+ enum :active, 1
33
+ enum :inactive, 2
34
+ enum :deleted, 3
35
+ end
36
+
37
+ def initialize(status)
38
+ @status = status
39
+ end
40
+ end
41
+
42
+ I18n locales:
43
+
44
+ ja:
45
+ enumattr:
46
+ User:
47
+ status:
48
+ active: 有効
49
+ inactive: 無効
50
+ deleted: 削除
51
+
52
+ and
53
+
54
+ I18n.load_path = ['path/to/locales.yml']
55
+ I18n.locale = :ja
56
+
57
+ then
58
+
59
+ User.status_names
60
+ # => ["有効", "無効", "削除"]
61
+
62
+ User.status_name(:active)
63
+ # => "有効"
64
+
65
+ User.status_options
66
+ #=> [["有効", 1], ["無効", 2], ["削除", 3]]
67
+
68
+ user = User.new(1)
69
+ user.status_name
70
+ # => "有効"
71
+
72
+ see also: https://github.com/aisuii/enumattr
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ task :default => :spec
6
+
7
+ desc "Run all specs in spec directory"
8
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/enumattr-ext/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["aisuii"]
6
+ gem.email = ["aisuiiaisuii@gmail.com"]
7
+ gem.description = %q{enumattr extensions}
8
+ gem.summary = %q{add enumattr features}
9
+ gem.homepage = "https://github.com/aisuii/enumattr-ext"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "enumattr-ext"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Enumattr::Ext::VERSION
17
+
18
+ gem.add_runtime_dependency('enumattr', '>= 0.0.5')
19
+ gem.add_runtime_dependency('i18n', '>= 0.6.0')
20
+ gem.add_runtime_dependency('activesupport', '>= 3.2.3')
21
+
22
+ gem.add_development_dependency('rspec')
23
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class AdminUser
4
+ include Enumattr::Base
5
+ include Enumattr::Ext::Name
6
+
7
+ attr_accessor :status
8
+
9
+ enumattr :status do
10
+ enum :active, 1, 'activated'
11
+ enum :inactive, 2, 'disabled'
12
+ enum :deleted, 3, 'deleted'
13
+ end
14
+
15
+ def initialize(status)
16
+ @status = status
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class Anonymous
4
+ include Enumattr::Base
5
+ include Enumattr::Ext::Name
6
+
7
+ attr_accessor :status
8
+
9
+ enumattr :status do
10
+ enum :active, 1
11
+ enum :inactive, 2
12
+ enum :deleted, 3
13
+ end
14
+
15
+ def initialize(status)
16
+ @status = status
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ en:
4
+ enumattr:
5
+ User:
6
+ status:
7
+ active: activated status
8
+ inactive: inacitive status
9
+ deleted: deleted status
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+ ja:
3
+ enumattr:
4
+ User:
5
+ status:
6
+ active: 有効
7
+ inactive: 無効
8
+ deleted: 削除
data/examples/user.rb ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class User
4
+ include Enumattr::Base
5
+ include Enumattr::Ext::Name
6
+
7
+ attr_accessor :status
8
+
9
+ enumattr :status do
10
+ enum :active, 1, 'activated'
11
+ enum :inactive, 2, 'disabled'
12
+ enum :deleted, 3, 'deleted'
13
+ end
14
+
15
+ def initialize(status)
16
+ @status = status
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "active_support/core_ext"
3
+ require "enumattr"
4
+ require "enumattr-ext/version"
5
+
6
+ module Enumattr
7
+ module Ext
8
+ autoload :Name, 'enumattr-ext/name'
9
+ end
10
+ end
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Enumattr
3
+ module Ext
4
+ module Name
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ Enumattr::Enums::Enum.send :include, Decorator
9
+ class << self
10
+ alias_method_chain :define_enumattr_class_methods, :name
11
+ alias_method_chain :define_enumattr_instance_methods, :name
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ def define_enumattr_class_methods_with_name(enumattr_name)
17
+ define_enumattr_class_methods_without_name(enumattr_name)
18
+
19
+ method_prefix = "#{enumattr_name}_"
20
+ class_eval(<<-METHOD, __FILE__, __LINE__)
21
+ def self.#{method_prefix}names
22
+ enumattrs[:#{enumattr_name}].enums.map(&:name)
23
+ end
24
+
25
+ def self.#{method_prefix}name(key)
26
+ enum = enumattrs[:#{enumattr_name}].enum_by_key(key)
27
+ enum && enum.name
28
+ end
29
+
30
+ def self.#{method_prefix}options
31
+ enumattrs[:#{enumattr_name}].enums.map do |enum|
32
+ [ enum.name, enum.value ]
33
+ end
34
+ end
35
+ METHOD
36
+ end
37
+
38
+ def define_enumattr_instance_methods_with_name(enumattr_name)
39
+ define_enumattr_instance_methods_without_name(enumattr_name)
40
+
41
+ enums = enumattrs[enumattr_name]
42
+ method_prefix = "#{enumattr_name}_"
43
+
44
+ class_eval(<<-METHOD, __FILE__, __LINE__)
45
+ def #{method_prefix}name
46
+ enum = #{method_prefix}enum
47
+ enum && enum.name
48
+ end
49
+ METHOD
50
+ end
51
+ end
52
+
53
+ module Decorator
54
+ def name
55
+ default = @extras.first || key.to_s
56
+ scope = ["enumattr.#{@container.base.name}.#{@container.enumattr}"]
57
+ I18n.translate(key, :scope => scope, :default => default)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Enumattr
3
+ module Ext
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
@@ -0,0 +1,138 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'user'
5
+ require 'admin_user'
6
+ require 'anonymous'
7
+
8
+ describe Enumattr::Ext::Name do
9
+ context "use I18n name when locale file and enum :keyword, value, name" do
10
+ before do
11
+ I18n.load_path = Dir[File.join(File.dirname(__FILE__), '../../examples/locales/*.yml')]
12
+ end
13
+
14
+ describe "class methods" do
15
+ subject { User }
16
+
17
+ context "locale :ja" do
18
+ before { I18n.locale = :ja }
19
+ its(:status_names) { should == ["有効", "無効", "削除"] }
20
+ its(:status_options) { should == [["有効", 1], ["無効", 2], ["削除", 3]] }
21
+ describe 'status_name(:active)' do
22
+ it 'should == "有効"' do
23
+ subject.status_name(:active).should == "有効"
24
+ end
25
+ end
26
+ end
27
+
28
+ context "locale :en" do
29
+ before { I18n.locale = :en }
30
+ its(:status_names) { should == ["activated status", "inacitive status", "deleted status"] }
31
+ its(:status_options) { should == [["activated status", 1], ["inacitive status", 2], ["deleted status", 3]] }
32
+ describe 'status_name(:active)' do
33
+ it 'should == "activated status"' do
34
+ subject.status_name(:active).should == "activated status"
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "instance methods" do
41
+ subject { User.new(User.status_value(:active)) }
42
+
43
+ context "locale :ja" do
44
+ before { I18n.locale = :ja }
45
+ its(:status_name) { should == "有効" }
46
+ end
47
+
48
+ context "locale :en" do
49
+ before { I18n.locale = :en }
50
+ its(:status_name) { should == "activated status" }
51
+ end
52
+ end
53
+ end
54
+
55
+ context "use enum name when no locale file and enum :keyword, value, name" do
56
+ describe "class methods" do
57
+ subject { AdminUser }
58
+
59
+ context "locale :ja" do
60
+ before { I18n.locale = :ja }
61
+ its(:status_names) { should == ['activated', 'disabled', 'deleted'] }
62
+ its(:status_options) { should == [["activated", 1], ["disabled", 2], ["deleted", 3]] }
63
+ describe 'status_name(:active)' do
64
+ it 'should == "activated"' do
65
+ subject.status_name(:active).should == "activated"
66
+ end
67
+ end
68
+ end
69
+
70
+ context "locale :en" do
71
+ before { I18n.locale = :en }
72
+ its(:status_names) { should == ['activated', 'disabled', 'deleted'] }
73
+ its(:status_options) { should == [["activated", 1], ["disabled", 2], ["deleted", 3]] }
74
+ describe 'status_name(:active)' do
75
+ it 'should == "activated"' do
76
+ subject.status_name(:active).should == "activated"
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "instance methods" do
83
+ subject { AdminUser.new(AdminUser.status_value(:active)) }
84
+
85
+ context "locale :ja" do
86
+ before { I18n.locale = :ja }
87
+ its(:status_name) { should == 'activated' }
88
+ end
89
+
90
+ context "locale :en" do
91
+ before { I18n.locale = :en }
92
+ its(:status_name) { should == 'activated' }
93
+ end
94
+ end
95
+ end
96
+
97
+ context "use keyword.to_s when no locale file and enum :keyword, value" do
98
+ describe "class methods" do
99
+ subject { Anonymous }
100
+
101
+ context "locale :ja" do
102
+ before { I18n.locale = :ja }
103
+ its(:status_names) { should == ['active', 'inactive', 'deleted'] }
104
+ its(:status_options) { should == [["active", 1], ["inactive", 2], ["deleted", 3]] }
105
+ describe 'status_name(:active)' do
106
+ it 'should == "active"' do
107
+ subject.status_name(:active).should == "active"
108
+ end
109
+ end
110
+ end
111
+
112
+ context "locale :en" do
113
+ before { I18n.locale = :en }
114
+ its(:status_names) { should == ['active', 'inactive', 'deleted'] }
115
+ its(:status_options) { should == [["active", 1], ["inactive", 2], ["deleted", 3]] }
116
+ describe 'status_name(:active)' do
117
+ it 'should == "active"' do
118
+ subject.status_name(:active).should == "active"
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "instance methods" do
125
+ subject { Anonymous.new(Anonymous.status_value(:active)) }
126
+
127
+ context "locale :ja" do
128
+ before { I18n.locale = :ja }
129
+ its(:status_name) { should == 'active' }
130
+ end
131
+
132
+ context "locale :en" do
133
+ before { I18n.locale = :en }
134
+ its(:status_name) { should == 'active' }
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'examples'))
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ require 'enumattr-ext'
6
+
7
+ # This file was generated by the `rspec --init` command. Conventionally, all
8
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
9
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
10
+ # loaded once.
11
+ #
12
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumattr-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - aisuii
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: enumattr
16
+ requirement: &82199750 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *82199750
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &82199500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *82199500
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &82199250 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 3.2.3
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *82199250
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &82199040 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *82199040
58
+ description: enumattr extensions
59
+ email:
60
+ - aisuiiaisuii@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rspec
67
+ - Gemfile
68
+ - LICENSE
69
+ - README.ja.md
70
+ - README.md
71
+ - Rakefile
72
+ - enumattr-ext.gemspec
73
+ - examples/admin_user.rb
74
+ - examples/anonymous.rb
75
+ - examples/locales/en.yml
76
+ - examples/locales/ja.yml
77
+ - examples/user.rb
78
+ - lib/enumattr-ext.rb
79
+ - lib/enumattr-ext/name.rb
80
+ - lib/enumattr-ext/version.rb
81
+ - spec/enumattr-ext/name_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: https://github.com/aisuii/enumattr-ext
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.15
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: add enumattr features
107
+ test_files:
108
+ - spec/enumattr-ext/name_spec.rb
109
+ - spec/spec_helper.rb