phrase 0.2.0.beta.3 → 0.2.0.beta.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/phrase.rb CHANGED
@@ -35,6 +35,10 @@ module Phrase
35
35
  def enabled?
36
36
  enabled
37
37
  end
38
+
39
+ def disabled?
40
+ !enabled
41
+ end
38
42
  end
39
43
 
40
44
  autoload :ViewHelpers, 'phrase/view_helpers'
@@ -28,6 +28,15 @@ class Phrase::Api::Client
28
28
  locales
29
29
  end
30
30
 
31
+ def fetch_blacklisted_keys
32
+ result = perform_api_request("/blacklisted_keys", :get)
33
+ blacklisted_keys = []
34
+ JSON.parse(result).map do |blacklisted_key|
35
+ blacklisted_keys << blacklisted_key['name']
36
+ end
37
+ blacklisted_keys
38
+ end
39
+
31
40
  def translate(key)
32
41
  raise "You must specify a key" if key.nil? or key.blank?
33
42
  keys = {}
@@ -1,94 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
- require 'phrase/delegate'
4
-
5
3
  module Phrase::Backend::Base
6
-
7
- def translate(*args)
8
- if Phrase.enabled?
9
- key = lookup_normalized_key(*args)
10
- phrase_delegate_for(key[:key], *args)
11
- else
12
- I18n.translate_without_phrase(*args)
13
- end
14
- end
15
-
16
- protected
17
- def phrase_delegate_for(key=nil, *args)
18
- return nil unless key.present?
19
- options = args[1].nil? ? {} : args[1]
20
- Phrase::Delegate.new(key, options)
21
- end
22
-
23
- def lookup_normalized_key(*args)
24
- caller = identify_caller
25
-
26
- if caller && args.first =~ /^\./
27
- args = transform_args_for_caller(caller, *args)
28
- end
29
-
30
- new_args = split_args(*args)
31
-
32
- normalized_key = I18n::Backend::Flatten.normalize_flat_keys(*new_args)
33
- normalized_key.gsub!("..", ".")
34
-
35
- {:key => normalized_key}
36
- end
37
-
38
- def identify_caller
39
- caller = nil
40
- send(:caller)[0..6].each do |string|
41
- caller = match_caller(string) unless caller
42
- end
43
-
44
- if caller && caller.present?
45
- find_lookup_scope(caller)
46
- else
47
- nil
48
- end
49
- end
50
-
51
- def match_caller(string)
52
- string.match(/(views)(\/.+)(?>:[0-9]+:in)/)
53
- end
54
-
55
- def split_args(*args)
56
- options = args.last.is_a?(Hash) ? args.pop : {}
57
- key ||= args.shift
58
- locale = options.delete(:locale) || I18n.locale
59
- return [locale, key, options[:scope], nil]
60
- end
61
-
62
- def transform_args_for_caller(caller, *args)
63
- _scope = caller
64
-
65
- options = args.last.is_a?(Hash) ? args.pop : {}
66
-
67
- if !options[:scope].presence && _scope.presence
68
- options[:scope] = _scope
69
- end
70
-
71
- args.push(options)
72
- parts = args.first.to_s.split(".").select { |e| !e.blank? }
73
- args[0] = parts[0] if parts.size == 1
74
-
75
- return args
76
- end
77
-
78
- def find_lookup_scope(caller)
79
- split_path = caller[2][1..-1].split(".")[0].split("/")
80
-
81
- template_or_partial = remove_underscore_form_partial(split_path[-1])
82
- split_path[-1] = template_or_partial
83
-
84
- split_path.map!(&:to_sym)
85
- end
4
+ end
86
5
 
87
- def remove_underscore_form_partial(template_or_partial)
88
- if template_or_partial.to_s[0,1] == "_"
89
- template_or_partial.to_s[1..-1]
90
- else
91
- template_or_partial.to_s
92
- end
93
- end
94
- end
6
+ require 'phrase/backend/phrase_service'
@@ -1,11 +1,117 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
- module Phrase::Backend
4
- class PhraseService
5
- include Base
3
+ require 'phrase/delegate'
4
+
5
+ class Phrase::Backend::PhraseService
6
+
7
+ attr_accessor :api_client, :blacklisted_keys
8
+
9
+ def initialize(args = {})
10
+ self
11
+ end
12
+
13
+ def translate(*args)
14
+ key = lookup_normalized_key(*args)
15
+ if Phrase.disabled? or key_is_blacklisted?(key[:key])
16
+ I18n.translate_without_phrase(*args)
17
+ else
18
+ phrase_delegate_for(key[:key], *args)
19
+ end
20
+ end
21
+
22
+ protected
23
+ def key_is_blacklisted?(key)
24
+ blacklisted_keys.each do |blacklisted_key|
25
+ if key.to_s[/\A#{blacklisted_key.gsub("*", ".*")}\Z/].present?
26
+ return true
27
+ end
28
+ end
29
+ false
30
+ end
31
+
32
+ def blacklisted_keys
33
+ @blacklisted_keys ||= api_client.fetch_blacklisted_keys
34
+ end
35
+
36
+ def api_client
37
+ @api_client ||= Phrase::Api::Client.new(Phrase.auth_token)
38
+ end
39
+
40
+ def phrase_delegate_for(key=nil, *args)
41
+ return nil unless key.present?
42
+ options = args[1].nil? ? {} : args[1]
43
+ Phrase::Delegate.new(key, options)
44
+ end
45
+
46
+ def lookup_normalized_key(*args)
47
+ caller = identify_caller
48
+
49
+ if caller && args.first =~ /^\./
50
+ args = transform_args_for_caller(caller, *args)
51
+ end
52
+
53
+ new_args = split_args(*args)
54
+
55
+ normalized_key = I18n::Backend::Flatten.normalize_flat_keys(*new_args)
56
+ normalized_key.gsub!("..", ".")
57
+
58
+ {:key => normalized_key}
59
+ end
60
+
61
+ def identify_caller
62
+ caller = nil
63
+ send(:caller)[0..6].each do |string|
64
+ caller = match_caller(string) unless caller
65
+ end
6
66
 
7
- def initialize(args = {})
8
- self
9
- end
67
+ if caller && caller.present?
68
+ find_lookup_scope(caller)
69
+ else
70
+ nil
71
+ end
72
+ end
73
+
74
+ def match_caller(string)
75
+ string.match(/(views)(\/.+)(?>:[0-9]+:in)/)
76
+ end
77
+
78
+ def split_args(*args)
79
+ options = args.last.is_a?(Hash) ? args.pop : {}
80
+ key ||= args.shift
81
+ locale = options.delete(:locale) || I18n.locale
82
+ return [locale, key, options[:scope], nil]
83
+ end
84
+
85
+ def transform_args_for_caller(caller, *args)
86
+ _scope = caller
87
+
88
+ options = args.last.is_a?(Hash) ? args.pop : {}
89
+
90
+ if !options[:scope].presence && _scope.presence
91
+ options[:scope] = _scope
92
+ end
93
+
94
+ args.push(options)
95
+ parts = args.first.to_s.split(".").select { |e| !e.blank? }
96
+ args[0] = parts[0] if parts.size == 1
97
+
98
+ return args
99
+ end
100
+
101
+ def find_lookup_scope(caller)
102
+ split_path = caller[2][1..-1].split(".")[0].split("/")
103
+
104
+ template_or_partial = remove_underscore_form_partial(split_path[-1])
105
+ split_path[-1] = template_or_partial
106
+
107
+ split_path.map!(&:to_sym)
108
+ end
109
+
110
+ def remove_underscore_form_partial(template_or_partial)
111
+ if template_or_partial.to_s[0,1] == "_"
112
+ template_or_partial.to_s[1..-1]
113
+ else
114
+ template_or_partial.to_s
115
+ end
10
116
  end
11
117
  end
@@ -1,3 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+
1
3
  require 'phrase/api'
2
4
 
3
5
  class Phrase::Delegate
@@ -17,6 +19,7 @@ class Phrase::Delegate
17
19
  end
18
20
 
19
21
  def method_missing(*args, &block)
22
+ puts args.first
20
23
  if @key.respond_to?(args.first)
21
24
  to_s.send(*args)
22
25
  else
data/phrase.gemspec CHANGED
@@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "phrase"
7
- s.version = "0.2.0.beta.3"
7
+ s.version = "0.2.0.beta.4"
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Dynport GmbH"]
10
10
  s.email = ["info@phraseapp.com"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phrase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.beta.3
4
+ version: 0.2.0.beta.4
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-21 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport