babylonia 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: 073d7b1426c878adc6e781b9f92c312ffc839b24
4
- data.tar.gz: 1a2e6c6ee29702aa1a4e2dce7cd9dc0532d1dd52
3
+ metadata.gz: a13bf3ea4fe0951e8ee79c6a22787739541c1225
4
+ data.tar.gz: f92431777d5a4df6fb6068ec6c4fbd37a5703d37
5
5
  SHA512:
6
- metadata.gz: b100a52c1ec74ee94b59389474f0e41b5518d379f10df37344d244edceeda4cc7cda92f925ad8338d888a1bea7f59f4418bcf6bcc85393028feac458d38c9d22
7
- data.tar.gz: 4698094b8e16d962b870b02b37c8b8fad5fba9d928a77456120bfe3d2d155a4415a60bdb5622136ca71daa5b524455f89eeeeebfbaa36452a4feef23e2ae1417
6
+ metadata.gz: e61ded2cb171b09824548fb281b768ae98d6a8289618613607cd37af950fb7876ed8046ece7cbe6f5c2c87ee2a70ef8ebc1094a447b07e9c706af395800d1257
7
+ data.tar.gz: 3d434f2287d4e7da7c80af4815fc00a3faae7f866c39b0b3ad079fb4c7dd5a077d023094fb486905370cd5e723b248a84abbdb83497c8979f9116534c68959aa
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - rbx-19mode
7
+ - rbx-20mode
8
+ - jruby-19mode
9
+ - jruby-20mode
10
+
11
+ script: bundle exec rspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- babylonia (0.1.0)
4
+ babylonia (0.0.1)
5
5
  i18n (>= 0.5.0)
6
6
 
7
7
  GEM
@@ -3,16 +3,22 @@ require 'i18n'
3
3
 
4
4
  module Babylonia
5
5
  module ClassMethods
6
+
6
7
  def build_babylonian_tower_on(*fields)
7
8
  babylonian_options = fields.last.is_a?(Hash) ? fields.pop : {}
8
9
  babylonian_options[:locale] ||= lambda { |r, f| I18n.locale }
9
10
  babylonian_options[:default_locale] ||= lambda { |r, f| I18n.default_locale }
10
11
  babylonian_options[:fallback] = true if babylonian_options[:fallback].nil?
11
12
 
12
- # loop through fields to define methods such as "name" and "description"
13
13
  fields.each do |field|
14
14
  instance_variable_set(:"@babylonian_options_for_#{field}", babylonian_options)
15
15
 
16
+ # Alias method chain the field to a translated value
17
+ # @param [Symbol] locale Pass a locale to get the field translation in this specific locale
18
+ # @return [String, NilClass] Either the string with the translation, the fallback, the placeholder or nil
19
+ # @example Call a field in italian
20
+ # your_instance.field(:it) #=> "Translation"
21
+ #
16
22
  define_method :"#{field}_translated" do |locale=nil|
17
23
  field_hash = send(:"#{field}_hash")
18
24
  translation = field_hash[locale || evaluate_babylonian_option!(:locale, field)]
@@ -22,15 +28,30 @@ module Babylonia
22
28
  alias_method :"#{field}_raw", field
23
29
  alias_method field, :"#{field}_translated"
24
30
 
31
+ # Return the translated values as a hash
32
+ # @return [Hash] The hash with all the translations stored in the field
33
+ #
25
34
  define_method :"#{field}_hash" do
26
35
  field_content = send(:"#{field}_raw")
27
36
  field_content.is_a?(String) ? YAML.load(field_content) : {}
28
37
  end
29
38
 
39
+ # Return all the languages stored
40
+ # @return [Array] An array containing all languages stored in the field
41
+ #
30
42
  define_method :"#{field}_languages" do
31
43
  send(:"#{field}_hash").keys
32
44
  end
33
45
 
46
+ # Set the field to a value. This either takes a string or a hash
47
+ # If given a String, the current locale is set to this value
48
+ # If given a Hash, the hash is merged into the current translation hash, and empty values are purged
49
+ # @param [String, Hash] data The data to set either the current language translation or all translations to
50
+ # @example Set the translation for the current locale
51
+ # your_object.field = "TRANSLATION"
52
+ # @example Set the translation and delete italian
53
+ # your_object.field = {de: 'DEUTSCH', it: ''}
54
+ #
34
55
  define_method :"#{field}_translated=" do |data|
35
56
  current_hash = send(:"#{field}_hash")
36
57
 
@@ -40,7 +61,7 @@ module Babylonia
40
61
  current_hash.merge! data
41
62
  end
42
63
 
43
- send(:"#{field}_raw=", YAML.dump(current_hash))
64
+ send(:"#{field}_raw=", YAML.dump(current_hash.delete_if{|k,v| v.nil? || v.empty? }))
44
65
  end
45
66
  alias_method :"#{field}_raw=", :"#{field}="
46
67
  alias_method :"#{field}=", :"#{field}_translated="
@@ -1,3 +1,3 @@
1
1
  module Babylonia
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -235,6 +235,32 @@ describe Babylonia::ClassMethods do
235
235
  subject.grasslands(:it).should == 'SOME ITALIAN'
236
236
  end
237
237
  end
238
+ context "deleting a value" do
239
+ context "with a string" do
240
+ it "should be deleted" do
241
+ subject.grasslands = ''
242
+ subject.grasslands.should be_nil
243
+ end
244
+ end
245
+ context "with nil" do
246
+ it "should be deleted" do
247
+ subject.grasslands = nil
248
+ subject.grasslands.should be_nil
249
+ end
250
+ end
251
+ context "with a hash containing an empty string" do
252
+ it "should be deleted" do
253
+ subject.grasslands = {it: ''}
254
+ subject.grasslands(:it).should be_nil
255
+ end
256
+ end
257
+ context "with a hash containing nil" do
258
+ it "should be deleted" do
259
+ subject.grasslands = {it: nil}
260
+ subject.grasslands(:it).should be_nil
261
+ end
262
+ end
263
+ end
238
264
  end
239
265
  end
240
266
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babylonia
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
  - Beat Richartz
@@ -47,10 +47,10 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - .gitignore
49
49
  - .rspec
50
+ - .travis.yml
50
51
  - Gemfile
51
52
  - Gemfile.lock
52
53
  - LICENSE
53
- - README.md
54
54
  - README.rdoc
55
55
  - babylonia.gemspec
56
56
  - lib/babylonia.rb
data/README.md DELETED
@@ -1,4 +0,0 @@
1
- babylonia
2
- =========
3
-
4
- User-side translation for ruby apps, persistence is yours