booletania 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17b4fb52dbb212b476970a5ddc26914d402d8e72
4
- data.tar.gz: f8878973581469831d5fbdc91a95007e825f8c63
3
+ metadata.gz: 8a7290fd84e48977e1d6b4684d2794a07f6f1c2f
4
+ data.tar.gz: 7b2b170dc90d2fb5fcab599f4d742bffebc3c826
5
5
  SHA512:
6
- metadata.gz: d9877b985b92fcd66dccacd6544af6995d4638f074310ecbaed69d296343e54ac9fbcfc1de736af43dd2e7b65fdabb12a3705bc54c8880bc34d58e2d90c88f89
7
- data.tar.gz: c9cd83aaaf0813f7d4d892e3a8201f5926ed5b9c60a11b034b7572d6eca886f9729addde35d44fa30b3a46bdf66402fccb95f24d7a8fd5adb709eee34a2a2292
6
+ metadata.gz: 57c575c2d5c130906903019a03959f2f275e22f76e536aac982be240e7b965533359ccd2c4ba2be45972b2782e264231b7e0dbf4eb1b22b96cb0f2e8f3693218
7
+ data.tar.gz: 25362bbda2696e947f631ede4998d6b402e9491ac686b3bddaa3267d7820a348d5f874f6db6cd3d0fa98355ae6b2bf69bf496df6de4942a6c79baa12948e0bc1
data/README.md CHANGED
@@ -52,6 +52,7 @@ ja:
52
52
  - http://yaml.org/type/bool.html
53
53
  - https://groups.google.com/forum/#!topic/rails-i18n/aL-Ed1Y1KGo
54
54
 
55
+ ### #xxx_text
55
56
  ````ruby
56
57
  invitation.accepted = true
57
58
  invitation.accepted_text # => "承諾"
@@ -66,6 +67,19 @@ invitation.accepted = true
66
67
  invitation.accepted_text # => "accept"
67
68
  ```
68
69
 
70
+ ### .xxx_options
71
+ ````ruby
72
+ Invitation.accepted_options # => [['accept', true], ['deny', false]]
73
+
74
+ I18n.locale = :ja
75
+ Invitation.accepted_options # => [["承諾", true], ["拒否", false]]
76
+ ```
77
+
78
+ for use in form
79
+ ````ruby
80
+ f.collection_radio_buttons :accepted, Invitation.accepted_options, :last, :first
81
+ ```
82
+
69
83
  ## Contributing
70
84
 
71
85
  1. Fork it ( https://github.com/[my-github-username]/booletania/fork )
data/booletania.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "activerecord", ">= 4.0"
22
+ spec.add_dependency "wannabe_bool"
22
23
  spec.add_development_dependency "bundler", "~> 1.7"
23
24
  spec.add_development_dependency "rake", "~> 10.0"
24
25
  spec.add_development_dependency "rspec"
@@ -1,13 +1,24 @@
1
1
  module Booletania
2
2
  class Attribute
3
- def self.define_methods!(klass, boolean_columns)
4
- boolean_columns.each do |boolean_column|
5
- klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
6
- def #{boolean_column.name}_text
7
- # http://yaml.org/type/bool.html
8
- I18n.t "booletania.#{klass.name.underscore}.#{boolean_column.name}." + #{boolean_column.name}.__send__(:to_s), default: #{boolean_column.name}.__send__(:to_s)
9
- end
10
- RUBY
3
+ class << self
4
+ def define_methods!(klass, boolean_columns)
5
+ boolean_columns.each do |boolean_column|
6
+ column_obj = Booletania::Column.new(klass, boolean_column)
7
+
8
+ define_attribute_text(column_obj)
9
+
10
+ define_attribute_options(column_obj)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def define_attribute_text(column_obj)
17
+ column_obj.klass.class_eval column_obj._text, __FILE__, __LINE__ + 1
18
+ end
19
+
20
+ def define_attribute_options(column_obj)
21
+ column_obj.klass.class_eval column_obj._options, __FILE__, __LINE__ + 1
11
22
  end
12
23
  end
13
24
  end
@@ -0,0 +1,30 @@
1
+ module Booletania
2
+ class Column
3
+ attr_accessor :klass, :boolean_column
4
+
5
+ def initialize(klass, boolean_column)
6
+ @klass = klass
7
+ @boolean_column = boolean_column
8
+ end
9
+
10
+ def booletania_i18n_path
11
+ "booletania.#{@klass.name.underscore}.#{@boolean_column.name}"
12
+ end
13
+
14
+ def _text
15
+ <<-RUBY
16
+ def #{@boolean_column.name}_text
17
+ I18n.t "#{booletania_i18n_path}." + #{@boolean_column.name}.__send__(:to_s), default: #{@boolean_column.name}.__send__(:to_s)
18
+ end
19
+ RUBY
20
+ end
21
+
22
+ def _options
23
+ <<-RUBY
24
+ def self.#{@boolean_column.name}_options
25
+ (I18n.t "#{booletania_i18n_path}", default: {}).invert.map { |k, v| [k, v.to_b] }
26
+ end
27
+ RUBY
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module Booletania
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/booletania.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "booletania/version"
2
+ require "booletania/column"
2
3
  require "booletania/attribute"
3
4
 
4
5
  module Booletania
@@ -8,6 +9,6 @@ module Booletania
8
9
  fail ArgumentError, "booletania only support ActiveRecord" unless ancestors.include? ActiveRecord::Base
9
10
  fail ArgumentError, "not found .columns method" unless respond_to? :columns
10
11
 
11
- Attribute.define_methods!(self, columns.select{ |column| column.type == :boolean })
12
+ Booletania::Attribute.define_methods!(self, columns.select{ |column| column.type == :boolean })
12
13
  end
13
14
  end
@@ -24,7 +24,7 @@ describe Booletania do
24
24
  end
25
25
  end
26
26
 
27
- describe "_text" do
27
+ describe "#_text" do
28
28
  let!(:invitation) { Invitation.create(accepted: accepted) }
29
29
  after { Invitation.delete_all }
30
30
 
@@ -62,4 +62,26 @@ describe Booletania do
62
62
  end
63
63
  end
64
64
  end
65
+
66
+ describe "._options" do
67
+ subject { Invitation.accepted_options }
68
+
69
+ context "lang is ja" do
70
+ before { I18n.locale = :ja }
71
+
72
+ it { is_expected.to eq [['承諾', true], ['拒否', false]] }
73
+ end
74
+
75
+ context "lang is en" do
76
+ before { I18n.locale = :en }
77
+
78
+ it { is_expected.to eq [['accept', true], ['deny', false]] }
79
+ end
80
+
81
+ context "lans is invalid" do
82
+ before { I18n.locale = :xx }
83
+
84
+ it { is_expected.to eq [] }
85
+ end
86
+ end
65
87
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_record'
2
2
  require 'pry'
3
3
  require 'pry-byebug'
4
+ require 'wannabe_bool'
4
5
  require 'bundler/setup'
5
6
  Bundler.require
6
7
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: booletania
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryoff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-07 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wannabe_bool
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -125,6 +139,7 @@ files:
125
139
  - booletania.gemspec
126
140
  - lib/booletania.rb
127
141
  - lib/booletania/attribute.rb
142
+ - lib/booletania/column.rb
128
143
  - lib/booletania/version.rb
129
144
  - spec/booletania_spec.rb
130
145
  - spec/fixtures/locales/en.yml