transmating 0.1.0
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.
- data/Gemfile +4 -0
- data/Gemfile.lock +45 -0
- data/README.md +31 -0
- data/Rakefile +21 -0
- data/lib/transmating.rb +6 -0
- data/lib/transmating/i18n.rb +23 -0
- data/lib/transmating/translation.rb +11 -0
- data/lib/transmating/version.rb +3 -0
- data/spec/models/user.rb +7 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/unit/i18n_spec.rb +25 -0
- data/spec/unit/translation_spec.rb +8 -0
- data/transmating.gemspec +26 -0
- metadata +95 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
transmating (0.1.0)
|
5
|
+
bson_ext
|
6
|
+
mongoid
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (3.1.3)
|
12
|
+
activesupport (= 3.1.3)
|
13
|
+
builder (~> 3.0.0)
|
14
|
+
i18n (~> 0.6)
|
15
|
+
activesupport (3.1.3)
|
16
|
+
multi_json (~> 1.0)
|
17
|
+
bson (1.5.2)
|
18
|
+
bson_ext (1.5.2)
|
19
|
+
bson (= 1.5.2)
|
20
|
+
builder (3.0.0)
|
21
|
+
diff-lcs (1.1.3)
|
22
|
+
i18n (0.6.0)
|
23
|
+
mongo (1.5.2)
|
24
|
+
bson (= 1.5.2)
|
25
|
+
mongoid (2.4.0)
|
26
|
+
activemodel (~> 3.1)
|
27
|
+
mongo (~> 1.3)
|
28
|
+
tzinfo (~> 0.3.22)
|
29
|
+
multi_json (1.0.4)
|
30
|
+
rspec (2.8.0)
|
31
|
+
rspec-core (~> 2.8.0)
|
32
|
+
rspec-expectations (~> 2.8.0)
|
33
|
+
rspec-mocks (~> 2.8.0)
|
34
|
+
rspec-core (2.8.0)
|
35
|
+
rspec-expectations (2.8.0)
|
36
|
+
diff-lcs (~> 1.1.2)
|
37
|
+
rspec-mocks (2.8.0)
|
38
|
+
tzinfo (0.3.31)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
rspec (~> 2.8)
|
45
|
+
transmating!
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Transmating
|
2
|
+
===========
|
3
|
+
|
4
|
+
Transmating adds i18n information to Mongoid documents. Simply include
|
5
|
+
`Transmating::I18n` to your model classes which include
|
6
|
+
`Mongoid::Document`.
|
7
|
+
|
8
|
+
class Person
|
9
|
+
include Mongoid::Document
|
10
|
+
include Transmating::I18n
|
11
|
+
|
12
|
+
field :name
|
13
|
+
field :organization
|
14
|
+
end
|
15
|
+
|
16
|
+
`Transmating::I18n` adds a simple `embeds_many` relationship to your
|
17
|
+
models, which is placeholder for translated attributes. You can
|
18
|
+
translate your models with calling `Transmating::I18n#translate` method
|
19
|
+
as presented below.
|
20
|
+
|
21
|
+
# Translate your model objects
|
22
|
+
p = Person.find_by_name("Yuna Kim")
|
23
|
+
p.translate("ko", :name => "김연아", :organization => "고려대")
|
24
|
+
|
25
|
+
After translation, you can utilize it by calling
|
26
|
+
`Transmating::I18n#translated` method.
|
27
|
+
|
28
|
+
<!-- Present translated attributes in your views -->
|
29
|
+
안녕하세요! <%= @person.translated("ko").name %>님!
|
30
|
+
|
31
|
+
That's it. Feel free to fork and add your own features!
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
desc 'Default: run specs.'
|
5
|
+
task :default => :spec
|
6
|
+
|
7
|
+
desc "Run specs"
|
8
|
+
RSpec::Core::RakeTask.new do |t|
|
9
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
10
|
+
# Put spec opts in a file named .rspec in root
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Generate code coverage"
|
14
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
15
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
16
|
+
t.rcov = true
|
17
|
+
t.rcov_opts = ['--exclude', 'spec']
|
18
|
+
end
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
data/lib/transmating.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Transmating
|
2
|
+
module I18n
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
embeds_many :translations, :class_name => "Transmating::Translation",
|
7
|
+
:validate => false, :as => :translatable
|
8
|
+
end
|
9
|
+
|
10
|
+
def translated(locale)
|
11
|
+
self.translations.where(:locale => locale.to_s).first
|
12
|
+
end
|
13
|
+
|
14
|
+
def translate(locale, keys = {})
|
15
|
+
if translated(locale)
|
16
|
+
translated(locale).update_attributes keys
|
17
|
+
else
|
18
|
+
new_translation = Translation.new keys.merge(:locale => locale.to_s)
|
19
|
+
translations << new_translation
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/models/user.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
|
+
|
4
|
+
MODELS = File.join(File.dirname(__FILE__), "models")
|
5
|
+
$LOAD_PATH.unshift(MODELS)
|
6
|
+
|
7
|
+
require "mongoid"
|
8
|
+
require "transmating"
|
9
|
+
require "rspec"
|
10
|
+
|
11
|
+
Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
|
12
|
+
|
13
|
+
def to_proc(&block)
|
14
|
+
block
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#encoding=utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Transmating::I18n do
|
5
|
+
let(:user) { User.new :name => 'John Doe', :organization => 'SNU' }
|
6
|
+
|
7
|
+
it "embeds translation model in the target class" do
|
8
|
+
user.translations.should == []
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#translated" do
|
12
|
+
it "returns nil if no translation is found" do
|
13
|
+
user.translated("ko").should == nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#translate" do
|
18
|
+
it "adds a translation object to model" do
|
19
|
+
user.translate("ko", :name => '홍길동', :organization => '서울대학교')
|
20
|
+
user.translations.to_a.count.should == 1
|
21
|
+
user.translated("ko").name.should == '홍길동'
|
22
|
+
user.translated("ko").organization.should == '서울대학교'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/transmating.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require "transmating/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "transmating"
|
7
|
+
s.version = Transmating::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Inbeom Hwang"]
|
10
|
+
s.email = ["inbeom@wafflestudio.com"]
|
11
|
+
s.homepage = "http://github.com/inbeom/transmating"
|
12
|
+
s.summary = "I18n for Mongoid"
|
13
|
+
s.description = "Transmating manages translated attributes for Mongoid documents"
|
14
|
+
|
15
|
+
s.add_dependency("bson_ext")
|
16
|
+
s.add_dependency("mongoid")
|
17
|
+
|
18
|
+
s.add_development_dependency("rspec", ["~> 2.8"])
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
25
|
+
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: transmating
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Inbeom Hwang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bson_ext
|
16
|
+
requirement: &70340991074920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70340991074920
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mongoid
|
27
|
+
requirement: &70340991073900 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70340991073900
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &70340991073020 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '2.8'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70340991073020
|
47
|
+
description: Transmating manages translated attributes for Mongoid documents
|
48
|
+
email:
|
49
|
+
- inbeom@wafflestudio.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/transmating.rb
|
59
|
+
- lib/transmating/i18n.rb
|
60
|
+
- lib/transmating/translation.rb
|
61
|
+
- lib/transmating/version.rb
|
62
|
+
- spec/models/user.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
- spec/unit/i18n_spec.rb
|
65
|
+
- spec/unit/translation_spec.rb
|
66
|
+
- transmating.gemspec
|
67
|
+
homepage: http://github.com/inbeom/transmating
|
68
|
+
licenses: []
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.10
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: I18n for Mongoid
|
91
|
+
test_files:
|
92
|
+
- spec/models/user.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/unit/i18n_spec.rb
|
95
|
+
- spec/unit/translation_spec.rb
|