i18n_accessors 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2641f6115e4fbd623fab985ebc188a1881350204
4
+ data.tar.gz: 24a1d8811112f7a4c449eecd8c0ea44e7e41e972
5
+ SHA512:
6
+ metadata.gz: 1e2ee6e8e13756ea930fffb422c65d267c018650f2fdeeca67d8703355b199397df0c5d97d13a252093ca0afd47432b14c232a5dbc3d3988fc90bfda6620c5ce
7
+ data.tar.gz: ac83bff33d37df96709c92f2658c27fdc8852851797128ae3747a4132aeb62d92999ac2749364bcdc86d1fdc6e7546aa414c256c46704590b79a3ef783fc2899
@@ -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 chris@dejimata.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 i18n_accessors.gemspec
4
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n_accessors (0.1.0)
5
+ i18n (>= 0.6.10, < 0.9)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ i18n (0.8.1)
12
+ rake (10.5.0)
13
+ rspec (3.5.0)
14
+ rspec-core (~> 3.5.0)
15
+ rspec-expectations (~> 3.5.0)
16
+ rspec-mocks (~> 3.5.0)
17
+ rspec-core (3.5.4)
18
+ rspec-support (~> 3.5.0)
19
+ rspec-expectations (3.5.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.5.0)
22
+ rspec-mocks (3.5.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.5.0)
25
+ rspec-support (3.5.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.12)
32
+ i18n_accessors!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.12.5
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Chris Salzberg
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.
@@ -0,0 +1,105 @@
1
+ # I18nAccessors
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/i18n_accessors.svg)][gem]
4
+ [![Build Status](https://travis-ci.org/shioyama/i18n_accessors.svg?branch=master)][travis]
5
+
6
+ [gem]: https://rubygems.org/gems/i18n_accessors
7
+ [travis]: https://travis-ci.org/shioyama/i18n_accessors
8
+
9
+ Define locale accessors for your translated attributes.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'i18n_accessors', '~> 0.1.1'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install i18n_accessors
26
+
27
+ ## Usage
28
+
29
+ ### Accessor Methods
30
+
31
+ To define a set of accessors for a translated attribute of the form
32
+ `<attribute>_<locale>`, include an instance of the `I18nAccessors::Methods`
33
+ class in your model with translated attribute names as arguments:
34
+
35
+ ```ruby
36
+ class Post
37
+
38
+ def title
39
+ # ...
40
+ end
41
+
42
+ def title?
43
+ # ...
44
+ end
45
+
46
+ def title=(title)
47
+ # ...
48
+ end
49
+
50
+ include I18nAccessors::Methods.new(:title)
51
+ end
52
+ ```
53
+ By default, `I18n.available_locales` will be used to determine which locales to
54
+ define accessors for. So if `I18n.available_locales` is `[:en, :fr]`, then this
55
+ will define methods: `title_en`, `title_en?`, `title_en=`, `title_fr`,
56
+ `title_fr?` and `title_fr=`. Each of these methods is an alias to `title` (or
57
+ `title?`, or `title=`) with `I18n.locale` changed to the locale in the suffix.
58
+
59
+ If you want to specify explicitly which locales to define accessors for, pass
60
+ the locales as an option with the `locales` key to `new` when creating the
61
+ module:
62
+
63
+ ```ruby
64
+ def Post
65
+
66
+ # ...
67
+
68
+ include I18nAccessors::Methods.new(:title, :content, locales: [:en, :fr])
69
+ end
70
+ ```
71
+
72
+ This will define accessor methods for both `title` and `content`, in both
73
+ English (`en`) and French (`fr`).
74
+
75
+ ### Method Missing
76
+
77
+ `I18nAccessors::Missing` does a similar thing, but using `method_missing` to
78
+ respond to messages of the form `<attribute>_<locale>`. This is generally
79
+ slower (due to how `method_missing` works), but can be used to handle *any* locale:
80
+
81
+ ```ruby
82
+ class Post
83
+
84
+ # ...
85
+
86
+ include I18nAccessors::Missing.new(:title, :content)
87
+ end
88
+ ```
89
+
90
+ No options are accepted to the constructor for this class.
91
+
92
+ ## Development
93
+
94
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
95
+
96
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shioyama/i18n_accessors. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
101
+
102
+
103
+ ## License
104
+
105
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,19 @@
1
+ require "i18n_accessors/version"
2
+ require "i18n_accessors/methods"
3
+ require "i18n_accessors/missing"
4
+
5
+ module I18nAccessors
6
+ class << self
7
+ # Return normalized locale
8
+ # @param [String,Symbol] locale
9
+ # @return [String] Normalized locale
10
+ # @example
11
+ # I18nAccessors.normalize_locale(:ja)
12
+ # #=> "ja"
13
+ # I18nAccessors.normalize_locale("pt-BR")
14
+ # #=> "pt_br"
15
+ def normalize_locale(locale = I18n.locale)
16
+ "#{locale.to_s.downcase.sub("-", "_")}".freeze
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ # frozen-string-literal: true
2
+ module I18nAccessors
3
+ =begin
4
+
5
+ Defines methods for a set of locales to access translated attributes in those
6
+ locales directly with a method call, using a suffix including the locale:
7
+
8
+ article.title_pt_br
9
+
10
+ If no locales are passed as an option to the initializer,
11
+ +I18n.available_locales+ will be used by default.
12
+
13
+ @example
14
+ class Post
15
+ def title
16
+ "title in #{I18n.locale}"
17
+ end
18
+ include I18nAccessors::Methods.new("title", locales: [:en, :fr])
19
+ end
20
+
21
+ I18n.locale = :en
22
+ post = Post.new
23
+ post.title
24
+ #=> "title in en"
25
+ post.title_fr
26
+ #=> "title in fr"
27
+
28
+ =end
29
+ class Methods < Module
30
+ # @param [String] One or more attributes
31
+ # @param [Array<Symbol>] Locales
32
+ def initialize(*attributes, locales: I18n.available_locales)
33
+ attributes.each do |attribute|
34
+ locales.each do |locale|
35
+ normalized_locale = I18nAccessors.normalize_locale(locale)
36
+ define_method "#{attribute}_#{normalized_locale}" do |*args|
37
+ I18n.with_locale(locale) { send(attribute, *args) }
38
+ end
39
+ define_method "#{attribute}_#{normalized_locale}?" do |*args|
40
+ I18n.with_locale(locale) { send("#{attribute}?", *args) }
41
+ end
42
+ define_method "#{attribute}_#{normalized_locale}=" do |value, *args|
43
+ I18n.with_locale(locale) { send("#{attribute}=", value, *args) }
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ module I18nAccessors
2
+ =begin
3
+
4
+ Defines +method_missing+ and +respond_to_missing?+ methods for a set of
5
+ attributes such that a method call using a locale accessor, like:
6
+
7
+ article.title_pt_br
8
+
9
+ will return the value of +article.title+ with the locale set to +pt-BR+ around
10
+ the method call.
11
+
12
+ @example Using missing on a plain old ruby class
13
+ class Post
14
+ def title
15
+ "title in #{I18n.locale}"
16
+ end
17
+ include I18nAccessors::Missing.new("title")
18
+ end
19
+
20
+ I18n.locale = :en
21
+ post = Post.new
22
+ post.title
23
+ #=> "title in en"
24
+ post.title_fr
25
+ #=> "title in fr"
26
+
27
+ =end
28
+ class Missing < Module
29
+ # @param [String] One or more attributes
30
+ def initialize(*attributes)
31
+ method_name_regex = /\A(#{attributes.join('|'.freeze)})_([a-z]{2}(_[a-z]{2})?)(=?|\??)\z/.freeze
32
+
33
+ define_method :method_missing do |method_name, *arguments, &block|
34
+ if method_name =~ method_name_regex
35
+ attribute = $1.to_sym
36
+ locale, suffix = $2.split('_'.freeze)
37
+ locale = "#{locale}-#{suffix.upcase}".freeze if suffix
38
+ I18n.with_locale(locale) { public_send("#{attribute}#{$4}".freeze, *arguments) }
39
+ else
40
+ super(method_name, *arguments, &block)
41
+ end
42
+ end
43
+
44
+ define_method :respond_to_missing? do |method_name, include_private = false|
45
+ (method_name =~ method_name_regex) || super(method_name, include_private)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module I18nAccessors
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_accessors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Salzberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.10
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '0.9'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.10
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.9'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.12'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.12'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ description:
76
+ email:
77
+ - chris@dejimata.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - LICENSE.txt
86
+ - README.md
87
+ - Rakefile
88
+ - lib/i18n_accessors.rb
89
+ - lib/i18n_accessors/methods.rb
90
+ - lib/i18n_accessors/missing.rb
91
+ - lib/i18n_accessors/version.rb
92
+ homepage: https://github.com/shioyama/i18n_accessors
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Define locale-specific attribute accessors.
116
+ test_files: []