labelizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTQ0N2QyMDA2NDY5NDYwYjgxY2IzZjJhZTg3ODM3ZDEzOWUyNTcxMQ==
5
+ data.tar.gz: !binary |-
6
+ MmQ5MWY2YTNjYjI5ODljN2FkYjIwNzE3ZDI5MmU1YmVmYzViZDljYw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OTQxY2EwOWMyYTQ5Y2JlNjQ1OTkzMzkyYzE5ZTA1NTQzMjEwMjZkOGVkZDMz
10
+ MmM0YTRlOWI2ZmUzMmE0MGQ1MjQ5MjJjYzlmYTFiOTg4MGVkMjFjY2Y0OTBl
11
+ NDg5NWIzNDEyMzgwMDc3OWYwYmE0ZTYzZWY1YWJkZTNmNmIzY2M=
12
+ data.tar.gz: !binary |-
13
+ M2ZiY2UyYzBmODliYWU4NGFhOWU3ODUzNmY4NjMxMDY5MjRmZTRjMWQ3OWUy
14
+ ZmNjNjI4NTU0MjAyN2M5MGEyMmVjMjFjOTljZjg3ODM0MjViZTJmMTNjZmU1
15
+ YmJlMDQ2NGY5ZGFlYmMzN2E1MWZjMWM3ZDk4NTI4ZDA2ZDcxNjk=
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /release/
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.1
6
+ deploy:
7
+ provider: rubygems
8
+ on:
9
+ tags: true
10
+ api_key:
11
+ secure: H+TAbsNwXNoeiqdXukbgE8woG3uEjNqSPN6RxULie+UZ2DqbqNVyN03cGR5UMSDwhsvXH6nfLJPACiyvEPGjBwx+yag6JPEfH2ttpv0wuGJVmDXUFvH76jIGEeMcMVAH6xt1EgNgmxbm7MPs2Mdj8JfIuMO9DWD5vRT0gYWDrOxmJw8IpPTITpReWU/fG81bc1ZqLZ9xMW8YFl5XbHSPy72XbHNhD72OthE0h4s6xyNXkIFfbTrw2FPdLgZOcp7y5OU9MVGujS0UmBRBmps0ufrcarvB3YbFDWZY18U3m8HKpJH9TnNJ6DS+ghhkMali45Tn6jMreok2kXja1R2h2/LHXrbfm0FktnP54PQ2EFr/TPqAcaI8rNTEnUEeCJTv2T47Qd1CCKsTsQfPWtGe+si6XaPcNg/vcrYaVZG0wZR3yKPFhKrcG9ggl+akS0dN9NsHYfjYLi6yMjLJ2gJYlLLqVJB2agOcdrCuQ5omrhuiCXS0n3+sEFpwVEXXBBdGjAKeoTUgcyStWsauSOcSnBfyyX8/KWDGlACOa2yLp+09TZ9LcUF4TOsaxbowOgC6QNpogreSXYsLCyV0NiVp3Cd3Z9Dcx36JK7Fyia6FJjZPWZ9M/ULoq02CVCo5A9DifTe/ZPQ6QbdbZX+XhWjw20px/8+8mBXHNsevXQny+xw=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in labelizer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 shun
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,163 @@
1
+ # Labelizer
2
+
3
+ [![Build Status](https://travis-ci.org/getto-systems/labelizer.svg?branch=master)](https://travis-ci.org/getto-systems/labelizer)
4
+ [![Gem Version](https://badge.fury.io/rb/labelizer.svg)](https://badge.fury.io/rb/labelizer)
5
+
6
+ add labels to attribute
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'labelizer'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install labelizer
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ # config/initializers/labelizer.rb
28
+ Labelizer.configure do |config|
29
+ config.labels = %w(label description color)
30
+ end
31
+ ```
32
+
33
+ ```yaml
34
+ # config/locales/labelizer.ja.yml
35
+ ja:
36
+ labelizer:
37
+ customer:
38
+ registration_state:
39
+ starting:
40
+ label: start
41
+ description: registration starting...
42
+ color: label label-info
43
+ confirming:
44
+ label: start
45
+ description: registration confirming...
46
+ color: label label-warning
47
+ completed:
48
+ label: start
49
+ description: registration completed!!
50
+ color: label label-success
51
+ ```
52
+
53
+ ```ruby
54
+ # app/models/my_model.rb
55
+ class Customer < ApplicationModel
56
+ enum registration_state: {
57
+ starting: 0,
58
+ confirming: 1,
59
+ completed: 2,
60
+ }
61
+
62
+ include Labelizer
63
+
64
+ labelize :registration_state
65
+ end
66
+ ```
67
+
68
+ ```erb
69
+ # app/views/customers/show.html.erb
70
+ <% customer = Customer.find id %>
71
+
72
+ <%# status label %>
73
+ <span class="<%= customer.registration_state_color %>"><%= customer.registration_state_label %></span>
74
+
75
+ <% labels = customer.registration_state_labelized %>
76
+ <span class="<%= labels[:color] %>"><%= labels[:label] %></span>
77
+
78
+ <% labels = customer.labelized[:registration_state] %>
79
+ <span class="<%= labels[:color] %>"><%= labels[:label] %></span>
80
+
81
+ <%# description %>
82
+ <ul>
83
+ <%# Customer.registration_states # => define by enum %>
84
+ <% Customer.registration_states.each do |state,value| %>
85
+ <% labels = Customer.labelized[:registration_state][state] %>
86
+ <li><%= labels[:label] %> : <%= labels[:description] %></li>
87
+ <% end %>
88
+ </ul>
89
+ ```
90
+
91
+ ## Converters
92
+
93
+ Convert label:
94
+
95
+ ```ruby
96
+ # config/initializers/labelizer.rb
97
+ Labelizer.configure do |config|
98
+ config.converter = {
99
+ color: ->(value){
100
+ # value : attribute value
101
+ "label label-#{value}"
102
+ },
103
+ }
104
+ end
105
+ ```
106
+
107
+ ```yaml
108
+ ja:
109
+ labelizer:
110
+ customer:
111
+ registration_state:
112
+ starting:
113
+ color: info
114
+ ```
115
+
116
+ ```ruby
117
+ Customer.starting.last.color # => "label label-info"
118
+ ```
119
+
120
+ ## default labels
121
+
122
+ ```yaml
123
+ ja:
124
+ labelizer:
125
+ color: ... # <= global default
126
+ customer:
127
+ color: ... # <= model default
128
+ registration_state:
129
+ color: ... # <= attribute default
130
+ starting:
131
+ color: ...
132
+ ```
133
+
134
+ ## outside rails
135
+
136
+ ```ruby
137
+ class Customer
138
+ include Labelizer
139
+
140
+ def self.model_name
141
+ OpenStruct.new i18n_key: :customer
142
+ end
143
+
144
+ labelize ...
145
+ end
146
+ ```
147
+
148
+
149
+ ## Development
150
+
151
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
152
+
153
+ 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).
154
+
155
+ ## Contributing
156
+
157
+ Bug reports and pull requests are welcome on GitHub at https://github.com/getto-systems/labelizer.
158
+
159
+
160
+ ## License
161
+
162
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
163
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "labelizer"
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/labelizer.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'labelizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "labelizer"
8
+ spec.version = Labelizer::VERSION
9
+ spec.authors = ["shun"]
10
+ spec.email = ["shun@getto.systems"]
11
+
12
+ spec.summary = %q{add labels to attribute}
13
+ spec.description = %q{add labels to attribute. label, color, or description, etc.}
14
+ spec.homepage = "https://github.com/getto-systems/labelizer"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygens.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ spec.add_development_dependency "travis"
37
+
38
+ spec.add_dependency "i18n"
39
+ end
data/lib/labelizer.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "i18n"
2
+
3
+ require "labelizer/version"
4
+
5
+ module Labelizer
6
+ class << self
7
+ def configure
8
+ yield config
9
+ end
10
+
11
+ private
12
+
13
+ def config
14
+ @config ||= OpenStruct.new
15
+ end
16
+ end
17
+
18
+ def self.included(base)
19
+ base.send :extend, ClassMethods
20
+ end
21
+
22
+ module ClassMethods
23
+ def labelize(*attrs)
24
+ attrs.each do |attr|
25
+ define_method :"#{attr}_labelized" do
26
+ self.class.labelized[attr][__send__(attr)]
27
+ end
28
+
29
+ (Labelizer.__send__(:config).labels || []).each do |label|
30
+ define_method :"#{attr}_#{label}" do
31
+ self.class.labelized[attr][__send__(attr)][label]
32
+ end
33
+ end
34
+ end
35
+ end
36
+ def labelized
37
+ model = model_name.i18n_key
38
+ @labelized ||= Hash.new{|h,attr|
39
+ h[attr] = Hash.new{|h,value|
40
+ h[value] = Hash.new{|h,label|
41
+ result = ::I18n.translate(
42
+ "labelizer.#{model}.#{attr}.#{value}.#{label}",
43
+ default: [
44
+ :"labelizer.#{model}.#{attr}.#{label}",
45
+ :"labelizer.#{model}.#{label}",
46
+ :"labelizer.#{label}",
47
+ value,
48
+ ],
49
+ raise: true,
50
+ ) rescue value
51
+ if converter = (Labelizer.__send__(:config).converter || {})[label.to_sym]
52
+ result = converter[result]
53
+ end
54
+ h[value] = result
55
+ }
56
+ }
57
+ }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Labelizer
2
+ VERSION = "0.0.1"
3
+ end
data/release.sh ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ git up && git tag v$(cat version.txt) && git push origin --tags
data/version-dump.sh ADDED
@@ -0,0 +1,40 @@
1
+ #!/bin/bash
2
+
3
+ . version-functions.sh
4
+
5
+ mode=$1
6
+ if [ -z "$mode" ]; then
7
+ mode=patch
8
+ fi
9
+
10
+ version_file=version.txt
11
+ version_rb=lib/labelizer/version.rb
12
+ current_version=$(cat $version_file)
13
+
14
+ git fetch --tags
15
+ current_tag=$(git tag | tail -1)
16
+
17
+ if [ "v$current_version" != "$current_tag" ]; then
18
+ read -p "file: '$current_version', tag: '$current_tag'. continue? [Y/n] " confirm
19
+ case $confirm in
20
+ Y*|y*)
21
+ ;;
22
+ *)
23
+ exit 1
24
+ ;;
25
+ esac
26
+ fi
27
+
28
+ version_build_next "$mode" $(cat $version_file)
29
+
30
+ read -p "dump version: $version. OK? [Y/n] " confirm
31
+ case $confirm in
32
+ Y*|y*)
33
+ echo $version > $version_file
34
+ sed -i 's/VERSION.*/VERSION = "'$version'"/' $version_rb
35
+ git add $version_file $version_rb && git commit -m "version dump: $version"
36
+ ;;
37
+ *)
38
+ exit 1
39
+ ;;
40
+ esac
data/version.txt ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: labelizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - shun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: travis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: i18n
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: add labels to attribute. label, color, or description, etc.
84
+ email:
85
+ - shun@getto.systems
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - labelizer.gemspec
99
+ - lib/labelizer.rb
100
+ - lib/labelizer/version.rb
101
+ - release.sh
102
+ - version-dump.sh
103
+ - version.txt
104
+ homepage: https://github.com/getto-systems/labelizer
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ allowed_push_host: https://rubygens.org
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.5
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: add labels to attribute
129
+ test_files: []