translateable_attributes 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: 1c3f4b9d3b35070a71a8495ded176171b3de558d
4
+ data.tar.gz: 434f55f169fa3b844f84d4c82106ac998664b455
5
+ SHA512:
6
+ metadata.gz: 494d31020c4a2e2f7987e3c37c91018990caf3350c8c2255f06becb0485131c8eaeacda657fdd51a1db0ae88b43202a7bdfc8ddc774ebf8d682857bea68a8241
7
+ data.tar.gz: 4330f27b819394806216312213dbca2a44371f654edbc0fad7de550b06622f5ba1324d886c66012f07032ddab7ebcb3c10cafef2aa23c7b67376212a5d1e232f
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/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ AllCops:
2
+ Exclude:
3
+ - tmp/**/*
4
+ - vendor/**/*
5
+
6
+ Metrics/LineLength:
7
+ Max: 145
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ AssignmentInCondition:
13
+ Enabled: false
14
+
15
+ Style/ClassAndModuleChildren:
16
+ Enabled: false
17
+
18
+ Style/MethodLength:
19
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ script:
5
+ - bundle exec rspec
6
+ - bundle exec rubocop
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in translateable_attributes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Toms Mikoss
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,70 @@
1
+ # TranslateableAttributes ![Travis CI](https://travis-ci.org/tmikoss/translateable_attributes.svg)
2
+
3
+ Helper methods to display translated values of object attributes (stored in database, plain object properties, ...)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'translateable_attributes'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install translateable_attributes
20
+
21
+ ## Usage
22
+
23
+ Setup your translations:
24
+
25
+ ```yaml
26
+ en:
27
+ states:
28
+ new: 'Initiated'
29
+ completed: 'Done'
30
+ ```
31
+
32
+ Extend a class with `TranslateableAttributes::Methods` and call `translate_attributes` method on it, passing a hash of `attribute_name: 'translation.namespace'` to it:
33
+
34
+ ```ruby
35
+ class TranslatedClass
36
+ extend TranslateableAttributes::Methods
37
+
38
+ translate_attributes state: 'states'
39
+ end
40
+ ```
41
+
42
+ Use `translated_attribute` class and instance methods to display translated values:
43
+
44
+ ```ruby
45
+ TranslatedClass.translated_state('new') #=> 'Initiated'
46
+
47
+ translated_instance = TranslatedClass.new
48
+ translated_instance.state = 'new'
49
+ translated_instance.translated_state #=> 'Initiated'
50
+ ```
51
+
52
+ In addition, `attributes_for_select` class method is defined that returns all available [translation, value] pairs - useful for select inputs in Rails form builders.
53
+
54
+ ```ruby
55
+ TranslatedClass.states_for_select #=> [['Initiated', 'new'], ['Done', 'completed']]
56
+ ```
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/tmikoss/translateable_attributes/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'i18n'
6
+ require 'active_support/all'
7
+
8
+ require 'translateable_attributes'
9
+
10
+ require 'irb'
11
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,22 @@
1
+ module TranslateableAttributes
2
+ module Methods
3
+ def translate_attributes(options = {})
4
+ options.each do |attribute, namespace|
5
+ define_method "translated_#{attribute}" do
6
+ value = send(attribute)
7
+ value.present? ? I18n.t("#{namespace}.#{value}", default: value) : value
8
+ end
9
+
10
+ define_singleton_method "#{attribute.to_s.pluralize}_for_select" do
11
+ I18n.t(namespace).each_with_object([]) do |(value, translation), collection|
12
+ collection << [translation, value]
13
+ end
14
+ end
15
+
16
+ define_singleton_method "translated_#{attribute}" do |value|
17
+ I18n.t([namespace, value].join '.')
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module TranslateableAttributes
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'translateable_attributes/version'
2
+ require 'translateable_attributes/methods'
3
+
4
+ module TranslateableAttributes
5
+ end
@@ -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 'translateable_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'translateable_attributes'
8
+ spec.version = TranslateableAttributes::VERSION
9
+ spec.authors = ['Toms Mikoss']
10
+ spec.email = ['toms.mikoss@gmail.com']
11
+
12
+ spec.summary = 'Helper methods to display translated values of object attributes.'
13
+ spec.description = 'Helper methods to display translated values of object attributes (stored in database, plain object properties, ...)'
14
+ spec.homepage = 'https://github.com/tmikoss/translateable_attributes'
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_runtime_dependency 'activesupport', ['>= 4', '< 5']
23
+ spec.add_runtime_dependency 'i18n'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.9'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '>= 3.3.0'
28
+ spec.add_development_dependency 'rubocop'
29
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: translateable_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Toms Mikoss
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: i18n
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.9'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.9'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 3.3.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.3.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: Helper methods to display translated values of object attributes (stored
104
+ in database, plain object properties, ...)
105
+ email:
106
+ - toms.mikoss@gmail.com
107
+ executables: []
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - ".rubocop.yml"
114
+ - ".travis.yml"
115
+ - Gemfile
116
+ - LICENSE.txt
117
+ - README.md
118
+ - Rakefile
119
+ - bin/console
120
+ - bin/setup
121
+ - lib/translateable_attributes.rb
122
+ - lib/translateable_attributes/methods.rb
123
+ - lib/translateable_attributes/version.rb
124
+ - translateable_attributes.gemspec
125
+ homepage: https://github.com/tmikoss/translateable_attributes
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.5
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Helper methods to display translated values of object attributes.
149
+ test_files: []