moped-i18n 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ services:
5
+ - mongodb
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+
4
+ gem "rake"
5
+
6
+ group :test do
7
+ gem "database_cleaner", '~> 0.9.1'
8
+ gem "mocha"
9
+ gem "test_declarative"
10
+ end
11
+
12
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Josef Šimánek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Moped::I18n [![Build Status](https://travis-ci.org/simi/moped-i18n.png?branch=master)](https://travis-ci.org/simi/moped-i18n)
2
+
3
+ Simple Moped (Mongoid) i18n flattened basic backend implementation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'moped-i18n'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install moped-i18n
18
+
19
+ ## Usage
20
+
21
+ TODO: soon
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.pattern = "test/**/*_test.rb"
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,50 @@
1
+ require 'i18n/backend/base'
2
+
3
+ module I18n
4
+ module Backend
5
+ class Moped
6
+ module Implementation
7
+ attr_accessor :collection
8
+ include Base, Flatten
9
+
10
+ def initialize(collection, options={})
11
+ @collection, @options = collection, options
12
+ end
13
+
14
+ def available_locales
15
+ collection.find.distinct(:locale).map {|l| l.to_sym}
16
+ end
17
+
18
+ def store_translations(locale, data, options = {})
19
+ escape = options.fetch(:escape, true)
20
+ flatten_translations(locale, data, escape, false).each do |key, value|
21
+ collection.find(:key => key.to_s, :locale => locale.to_s).remove_all
22
+ doc = {:key => key.to_s, :value => value, :locale => locale.to_s}
23
+ collection.insert(doc)
24
+ end
25
+ end
26
+
27
+ protected
28
+ def lookup(locale, key, scope = [], options = {})
29
+ key = normalize_flat_keys(locale, key, scope, options[:separator])
30
+ result = collection.find(:locale => locale.to_s, :key => /^#{Regexp.quote(key.to_s)}(\.|$)/).entries
31
+
32
+ if result.empty?
33
+ nil
34
+ elsif result.first["key"] == key.to_s
35
+ result.first["value"]
36
+ else
37
+ chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
38
+ result = result.inject({}) do |hash, r|
39
+ hash[r["key"].slice(chop_range)] = r["value"]
40
+ hash
41
+ end
42
+ result.deep_symbolize_keys
43
+ end
44
+ end
45
+ end
46
+
47
+ include Implementation
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,2 @@
1
+ require 'moped'
2
+ require 'i18n/backend/moped'
@@ -0,0 +1,5 @@
1
+ module Moped
2
+ module I18n
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'moped/i18n/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "moped-i18n"
8
+ spec.version = Moped::I18n::VERSION
9
+ spec.authors = ["Josef Šimánek"]
10
+ spec.email = ["retro@ballgag.cz"]
11
+ spec.description = %q{I18n backend for moped}
12
+ spec.summary = %q{Store your dynamic translations in moped}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "i18n"
22
+ spec.add_dependency "moped"
23
+ spec.add_dependency "activesupport"
24
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class I18nMopedApiTest < Test::Unit::TestCase
4
+ def setup
5
+ I18n.backend = I18n::Backend::Moped.new(COLLECTION)
6
+ super
7
+ end
8
+
9
+ def self.can_store_procs?
10
+ false
11
+ end
12
+
13
+ include I18n::Tests::Basics
14
+ include I18n::Tests::Defaults
15
+ include I18n::Tests::Interpolation
16
+ include I18n::Tests::Link
17
+ include I18n::Tests::Lookup
18
+ include I18n::Tests::Pluralization
19
+ include I18n::Tests::Procs if can_store_procs?
20
+
21
+ include I18n::Tests::Localization::Date
22
+ include I18n::Tests::Localization::DateTime
23
+ include I18n::Tests::Localization::Time
24
+ include I18n::Tests::Localization::Procs if can_store_procs?
25
+
26
+ test "make sure we use an Moped backend" do
27
+ assert_equal I18n::Backend::Moped, I18n.backend.class
28
+ end
29
+
30
+ test "return only on full hash match" do
31
+ I18n.backend.store_translations(:en, :short_text => 'ABC you know ME')
32
+ assert_raises(I18n::MissingTranslationData) {I18n.translate!(:sh)}
33
+ end
34
+
35
+ test "changing current locale" do
36
+ I18n.backend.store_translations(:en, :text => 'Hi')
37
+ I18n.backend.store_translations(:cs, :text => 'Zdary')
38
+
39
+ original = I18n.locale
40
+
41
+ I18n.locale = :en
42
+ assert_equal 'Hi', I18n.t(:text)
43
+
44
+ I18n.locale = :cs
45
+ assert_equal 'Zdary', I18n.t(:text)
46
+
47
+ I18n.locale = original
48
+ end
49
+ end
@@ -0,0 +1,62 @@
1
+
2
+ # Do not load the i18n gem from libraries like active_support.
3
+ #
4
+ # This is required for testing against Rails 2.3 because active_support/vendor.rb#24 tries
5
+ # to load I18n using the gem method. Instead, we want to test the local library of course.
6
+ require 'bundler/setup'
7
+ require 'i18n'
8
+ require 'moped/i18n'
9
+ require 'test_declarative'
10
+ require 'test/unit'
11
+ require 'mocha/setup'
12
+
13
+
14
+ def database_id
15
+ "moped_i18n_test"
16
+ end
17
+
18
+ @@session = Moped::Session.new(["127.0.0.1:27017"])
19
+ @@session.use(database_id)
20
+
21
+ COLLECTION = @@session["i18n"]
22
+
23
+ class Test::Unit::TestCase
24
+
25
+ # TODO poor's man moped DB cleaner
26
+ def truncate_test_db
27
+ collections = @@session['system.namespaces'].find(:name => { '$not' => /system|\$/ }).to_a.map do |collection|
28
+ _, name = collection['name'].split('.', 2)
29
+ name
30
+ end
31
+ collections.each { |c| @@session[c].find.remove_all }
32
+ end
33
+
34
+ def setup
35
+ truncate_test_db
36
+ end
37
+
38
+ def teardown
39
+ I18n.locale = nil
40
+ I18n.default_locale = :en
41
+ I18n.load_path = []
42
+ I18n.available_locales = nil
43
+ I18n.backend = nil
44
+ truncate_test_db
45
+ end
46
+
47
+ def translations
48
+ I18n.backend.instance_variable_get(:@translations)
49
+ end
50
+
51
+ def store_translations(*args)
52
+ data = args.pop
53
+ locale = args.pop || :en
54
+ I18n.backend.store_translations(locale, data)
55
+ end
56
+ end
57
+
58
+ Object.class_eval do
59
+ def meta_class
60
+ class << self; self; end
61
+ end
62
+ end unless Object.method_defined?(:meta_class)
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moped-i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josef Šimánek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: moped
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: I18n backend for moped
63
+ email:
64
+ - retro@ballgag.cz
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/i18n/backend/moped.rb
76
+ - lib/moped/i18n.rb
77
+ - lib/moped/i18n/version.rb
78
+ - moped-i18n.gemspec
79
+ - test/api_test.rb
80
+ - test/test_helper.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Store your dynamic translations in moped
106
+ test_files:
107
+ - test/api_test.rb
108
+ - test/test_helper.rb