exnum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94835057ae6ed47472c1db7f5ac350751d613270
4
+ data.tar.gz: c66802026aa68c3490b843594bc176cb0bd1968c
5
+ SHA512:
6
+ metadata.gz: 29e6109316bf870ceb49401060f23404c9f74fe7a48b119796b1f53d6940eb15bf289253e0c324d812e7700fbb374414b742429be7ceee1a30216b8358a499fd
7
+ data.tar.gz: 85a8dd2163bd8109d6a0c4a74ed33a775dca2f4f4ed72c468bca0ccb2f4a789e12b469c424e69508023d526b195bad37ae62936a28b79d065aaee3c432f92992
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
6
+ script: CODECLIMATE_REPO_TOKEN=9eff3c4d9a268cc72972edde460103ef26b162565dafa64ef5b86649fd43427f bundle exec rake
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at mohri1219@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in exnum.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kta-M
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Exnum
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/exnum.svg)](https://badge.fury.io/rb/exnum)
4
+ [![Build Status](https://travis-ci.org/Kta-M/exnum.svg?branch=master)](https://travis-ci.org/Kta-M/exnum)
5
+ [![Code Climate](https://codeclimate.com/github/Kta-M/exnum/badges/gpa.svg)](https://codeclimate.com/github/Kta-M/exnum)
6
+ [![Test Coverage](https://codeclimate.com/github/Kta-M/exnum/badges/coverage.svg)](https://codeclimate.com/github/Kta-M/exnum/coverage)
7
+
8
+ Exnum is enum extention for Rails.
9
+ This gem extends enum about i18n and associated parameters.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'exnum'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install exnum
26
+
27
+ ## Usage
28
+
29
+ ActiveRecord:
30
+ ```
31
+ class User < ActiveRecord::Base
32
+ # the value of the key "val" defines enum values
33
+ # and the others defines associated parameters
34
+ exnum role: {
35
+ guest: {val: 10, label: :red },
36
+ general: {val: 20, label: :green, permission: false },
37
+ admin: {val: 30, label: :blue, permission: true},
38
+ }
39
+ end
40
+
41
+ ```
42
+
43
+ I18n:
44
+ ```
45
+ ja:
46
+ activerecord:
47
+ enum:
48
+ user: # model name
49
+ role: # field name
50
+ guest: ゲスト
51
+ general: 一般ユーザー
52
+ admin: 管理者
53
+
54
+ ```
55
+
56
+ Then you can use i18n extention
57
+ ```
58
+ User.roles_i18n
59
+ # => {"guest" => "ゲスト", "general" => "一般ユーザー", "admin" => "管理者"}
60
+
61
+ user = User.new(role: :guest)
62
+ user.role_i18n
63
+ # => "ゲスト"
64
+ ```
65
+
66
+ and assosiated parameter extention.
67
+ ```
68
+ User.role_labels
69
+ # => {"guest" => :red, "general" => :green, "admin" => :blue}
70
+ User.role_permissions
71
+ # => {"guest" => nil, "general" => false, "admin" => true}
72
+
73
+ user = User.new(role: :general)
74
+ user.role_label
75
+ # => :green
76
+ user.role_permission
77
+ # => false
78
+ ```
79
+
80
+ And of cause, you can use methods provided by `enum`.
81
+
82
+ `exnum` can also use as well as `enum`.
83
+ Then you can use extention only about i18n.
84
+ ```
85
+ class User < ActiveRecord::Base
86
+ exnum role: [:guest, :general, :admin]
87
+ end
88
+ ```
89
+ ```
90
+ class User < ActiveRecord::Base
91
+ exnum role: {guest: 10, general: 20, admin: 30}
92
+ end
93
+ ```
94
+
95
+ ## Contributing
96
+
97
+ 1. Fork it!
98
+ 1. Create your feature branch (git checkout -b my-new-feature)
99
+ 1. Commit your changes (git commit -am 'Add some feature')
100
+ 1. Push to the branch (git push origin my-new-feature)
101
+ 1. Create a new Pull Request
102
+
103
+ ## License
104
+
105
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
106
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exnum"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exnum.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'exnum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "exnum"
8
+ spec.version = Exnum::VERSION
9
+ spec.authors = ["Kta-M"]
10
+ spec.email = ["mohri1219@gmail.com"]
11
+
12
+ spec.summary = %q{Enum extention for Rails}
13
+ spec.description = %q{Exnum is enum extention for Rails. This gem extends enum about i18n and associated parameters.}
14
+ spec.homepage = "https://github.com/Kta-M/exnum"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'activerecord'
23
+ spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "sqlite3", "~> 1.0"
27
+ spec.add_development_dependency "pry-rails"
28
+ spec.add_development_dependency "codeclimate-test-reporter"
29
+ end
@@ -0,0 +1,120 @@
1
+ module ActiveRecord
2
+ module Exnum
3
+ def self.prepended(base)
4
+ base.singleton_class.class_eval { self.prepend(ClassMethods) }
5
+ end
6
+ module ClassMethods
7
+ def extended(base)
8
+ base.class_attribute(:defined_params, instance_writer: false)
9
+ base.defined_params = {}
10
+ super(base)
11
+ end
12
+ end
13
+
14
+ def inherited(base)
15
+ base.defined_params = defined_params.deep_dup
16
+ super
17
+ end
18
+
19
+ #--------------------------------------------------------------------------
20
+
21
+ def exnum(definitions)
22
+ divided_definitions = definitions.inject({enums: {}, params: {}}) do |ret, (name, values)|
23
+ if !self.column_names.include?(name.to_s) || !values.kind_of?(Hash)
24
+ ret[:enums][name] = values
25
+ else
26
+ ret[:enums][name] = extract_enums(name, values)
27
+ ret[:params][name] = extract_params(name, values)
28
+ end
29
+ ret
30
+ end
31
+
32
+ enum(divided_definitions[:enums])
33
+ enum_i18n(divided_definitions[:enums])
34
+ enum_param(divided_definitions[:params])
35
+ end
36
+
37
+ #--------------------------------------------------------------------------
38
+
39
+ private
40
+ def extract_enums(_name, values)
41
+ values.inject({}) do |ret, (field, value)|
42
+ ret[field.to_sym] = (value.kind_of?(Hash) ? value[:val] : value)
43
+ ret
44
+ end
45
+ end
46
+
47
+ def extract_params(_name, values)
48
+ values.inject({}) do |ret, (field, value)|
49
+ if value.kind_of?(Hash)
50
+ ret[field.to_sym] = value.reject{|k, _| k == :val}
51
+ end
52
+ ret
53
+ end
54
+ end
55
+
56
+ def enum_i18n(definitions)
57
+ klass = self
58
+ definitions.delete(:_prefix)
59
+ definitions.delete(:_suffix)
60
+
61
+ definitions.each do |name, values|
62
+
63
+ # define class method which returns i18n string of each satus
64
+ method_name = "#{name.to_s.pluralize}_i18n"
65
+ detect_enum_conflict!(name, method_name, true)
66
+ klass.singleton_class.send(:define_method, method_name) do
67
+ i18n_hash = ActiveSupport::HashWithIndifferentAccess.new
68
+ values.inject(i18n_hash) do |ret, (enum_name, _value)|
69
+ ret[enum_name] = i18n_string(klass, name, enum_name)
70
+ ret
71
+ end
72
+ end
73
+
74
+ # define instance method which returns current i18n string of the instance
75
+ method_name = "#{name}_i18n"
76
+ detect_enum_conflict!(name, method_name, false)
77
+ klass.send(:define_method, method_name) do
78
+ status = self.send(name)
79
+ status.nil? ? nil : klass.send(:i18n_string, klass, name, status)
80
+ end
81
+ end
82
+ end
83
+
84
+ def i18n_string(klass, name, enum_name)
85
+ I18n.t("#{klass.i18n_scope}.enum.#{klass.model_name.i18n_key}.#{name}.#{enum_name}")
86
+ end
87
+
88
+ def enum_param(definitions)
89
+ klass = self
90
+ klass.defined_params = definitions
91
+
92
+ definitions.each do |name, values|
93
+ param_names = values.values.map(&:keys).flatten.uniq
94
+ param_names.each do |param_name|
95
+
96
+ # define class methods which returns the parameter's values of each status
97
+ method_name = "#{name}_#{param_name.to_s.pluralize}"
98
+ detect_enum_conflict!(name, method_name, true)
99
+ klass.singleton_class.send(:define_method, method_name) do
100
+ param_hash = ActiveSupport::HashWithIndifferentAccess.new
101
+ values.inject(param_hash) do |ret, (enum_name, params)|
102
+ ret[enum_name] = params[param_name]
103
+ ret
104
+ end
105
+ end
106
+
107
+ # define instance methods which returns the parameter's current value of the instance
108
+ method_name = "#{name}_#{param_name}"
109
+ detect_enum_conflict!(name, method_name, false)
110
+ define_method(method_name) do
111
+ status = self.send(name)
112
+ return if status.nil?
113
+ defined_params[name][status.to_sym][param_name]
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+
@@ -0,0 +1,3 @@
1
+ module Exnum
2
+ VERSION = "0.1.0"
3
+ end
data/lib/exnum.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'active_record' unless defined? ActiveRecord
2
+ require "exnum/version"
3
+ require "exnum/active_record/exnum"
4
+
5
+ ActiveRecord::Enum.prepend(ActiveRecord::Exnum)
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exnum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kta-M
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Exnum is enum extention for Rails. This gem extends enum about i18n and
112
+ associated parameters.
113
+ email:
114
+ - mohri1219@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - exnum.gemspec
130
+ - lib/exnum.rb
131
+ - lib/exnum/active_record/exnum.rb
132
+ - lib/exnum/version.rb
133
+ homepage: https://github.com/Kta-M/exnum
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Enum extention for Rails
157
+ test_files: []