phraseapp-in-context-editor-ruby 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +12 -0
  3. data/.gitignore +0 -1
  4. data/.package-licenses.yml +4 -0
  5. data/.tool-versions +1 -0
  6. data/CHANGELOG.md +52 -6
  7. data/CODEOWNERS +1 -0
  8. data/Gemfile +1 -1
  9. data/Gemfile.lock +241 -0
  10. data/Guardfile +13 -13
  11. data/README.md +81 -51
  12. data/RELEASING.md +10 -0
  13. data/config/license-decisions.yml +34 -0
  14. data/config/license_finder.yml +2 -0
  15. data/lib/generators/phraseapp_in_context_editor/install_generator.rb +3 -9
  16. data/lib/generators/phraseapp_in_context_editor/templates/phraseapp_in_context_editor.rb +9 -17
  17. data/lib/phraseapp-in-context-editor-ruby/adapters/fast_gettext.rb +20 -20
  18. data/lib/phraseapp-in-context-editor-ruby/adapters/i18n.rb +10 -10
  19. data/lib/phraseapp-in-context-editor-ruby/backend_service.rb +33 -100
  20. data/lib/phraseapp-in-context-editor-ruby/config.rb +13 -50
  21. data/lib/phraseapp-in-context-editor-ruby/delegate/fast_gettext.rb +13 -15
  22. data/lib/phraseapp-in-context-editor-ruby/delegate/i18n_delegate.rb +4 -50
  23. data/lib/phraseapp-in-context-editor-ruby/delegate.rb +13 -14
  24. data/lib/phraseapp-in-context-editor-ruby/engine.rb +5 -6
  25. data/lib/phraseapp-in-context-editor-ruby/view_helpers.rb +25 -19
  26. data/lib/phraseapp-in-context-editor-ruby.rb +15 -54
  27. data/phraseapp-in-context-editor-ruby.gemspec +18 -33
  28. metadata +32 -103
  29. data/.travis.yml +0 -8
  30. data/lib/generators/phraseapp_in_context_editor/templates/README +0 -10
  31. data/lib/phraseapp-in-context-editor-ruby/api_collection.rb +0 -41
  32. data/lib/phraseapp-in-context-editor-ruby/api_wrapper.rb +0 -59
  33. data/lib/phraseapp-in-context-editor-ruby/cache.rb +0 -37
  34. data/lib/phraseapp-in-context-editor-ruby/displayable_key_identifier.rb +0 -46
  35. data/lib/phraseapp-in-context-editor-ruby/fallback_keys_fetcher.rb +0 -44
  36. data/lib/phraseapp-in-context-editor-ruby/hash_flattener.rb +0 -53
  37. data/lib/phraseapp-in-context-editor-ruby/key_names_cache.rb +0 -53
  38. data/lib/phraseapp-in-context-editor-ruby/version.rb +0 -5
@@ -1,46 +0,0 @@
1
- require 'phraseapp-in-context-editor-ruby/fallback_keys_fetcher'
2
- require 'phraseapp-in-context-editor-ruby/key_names_cache'
3
-
4
- module PhraseApp
5
- module InContextEditor
6
- class DisplayableKeyIdentifier
7
- attr_reader :api_wrapper, :key_names_cache
8
-
9
- def initialize(api_wrapper)
10
- @api_wrapper = api_wrapper
11
- @key_names_cache = KeyNamesCache.new(@api_wrapper)
12
- end
13
-
14
- def identify(key_name, options)
15
- fallback_key_names = FallbackKeysFetcher.extract_fallback_keys(key_name, options)
16
- return key_name if fallback_key_names.empty?
17
-
18
- key_names = [key_name] | fallback_key_names
19
- available_key_names = find_keys_within_phraseapp(key_names)
20
-
21
- key_names.each do |item|
22
- if available_key_names.include?(item)
23
- return item
24
- end
25
- end
26
-
27
- return key_name
28
- end
29
-
30
- private
31
-
32
- def find_keys_within_phraseapp(key_names)
33
- key_names_to_check_against_api = key_names - @key_names_cache.pre_fetched(key_names)
34
- @key_names_cache.pre_cached(key_names) | key_names_returned_from_api_for(key_names_to_check_against_api)
35
- end
36
-
37
- def key_names_returned_from_api_for(key_names)
38
- if key_names.size > 0
39
- api_wrapper.keys_by_names(key_names).map { |key| key.name }
40
- else
41
- []
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,44 +0,0 @@
1
- module PhraseApp
2
- module InContextEditor
3
- class FallbackKeysFetcher
4
-
5
- def self.extract_fallback_keys(key, options)
6
- fallback_items = []
7
- if options.has_key?(:default)
8
- if options[:default].kind_of?(Array)
9
- fallback_items = options[:default]
10
- else
11
- fallback_items << options[:default]
12
- end
13
- end
14
-
15
- return fallback_items.map{ |item| process_fallback_item(item, key, options) }.flatten.uniq
16
- end
17
-
18
- def self.process_fallback_item(item, key, options)
19
- fallback_keys = []
20
- if item.kind_of?(Symbol)
21
- fallback_key_name = item.to_s
22
- if options.has_key?(:scope)
23
- if options[:scope].is_a?(Array)
24
- fallback_key_name = "#{options[:scope].join(".")}.#{item}"
25
- else
26
- fallback_key_name = "#{options[:scope]}.#{item}"
27
- end
28
- end
29
- fallback_keys << fallback_key_name
30
-
31
- if key == "helpers.label.#{fallback_key_name}" # http://apidock.com/rails/v3.1.0/ActionView/Helpers/FormHelper/label
32
- fallback_keys << "activerecord.attributes.#{fallback_key_name}"
33
- end
34
-
35
- if key.start_with?("simple_form.") # special treatment for simple form
36
- fallback_keys << "activerecord.attributes.#{item}"
37
- end
38
- end
39
-
40
- return fallback_keys
41
- end
42
- end
43
- end
44
- end
@@ -1,53 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module PhraseApp
3
- module InContextEditor
4
- module HashFlattener
5
- FLATTEN_SEPARATOR = "."
6
- SEPARATOR_ESCAPE_CHAR = "\001"
7
-
8
- def self.flatten(hash, escape, previous_key=nil, &block)
9
- hash.each_pair do |key, value|
10
- key = escape_default_separator(key) if escape
11
- current_key = [previous_key, key].compact.join(FLATTEN_SEPARATOR).to_sym
12
- yield current_key, value
13
- flatten(value, escape, current_key, &block) if value.is_a?(Hash)
14
- end
15
- end
16
-
17
- def self.expand_flat_hash(flat_hash, prefix=nil)
18
- flat_hash ||= []
19
- result = flat_hash.map do |key, value|
20
- key = key.gsub(/#{prefix}[\.]?/, '') if prefix
21
- to_nested_hash(key, value)
22
- end
23
-
24
- result = result.inject({}) { |hash, subhash| hash.deep_merge!(subhash) }
25
- result
26
- end
27
-
28
- def self.to_nested_hash key, value
29
- if contains_only_dots?(key) or starts_with_dot?(key) or ends_with_dot?(key)
30
- {key.to_sym => value}
31
- else
32
- key.to_s.split(".").reverse.inject(value) { |hash, part| {part.to_sym => hash} }
33
- end
34
- end
35
-
36
- def self.contains_only_dots?(string)
37
- string.to_s.gsub(/\./, "").length == 0
38
- end
39
-
40
- def self.starts_with_dot?(string)
41
- string.to_s.start_with?(".")
42
- end
43
-
44
- def self.ends_with_dot?(string)
45
- string.to_s.end_with?(".")
46
- end
47
-
48
- def self.escape_default_separator(key)
49
- key.to_s.tr(FLATTEN_SEPARATOR, SEPARATOR_ESCAPE_CHAR)
50
- end
51
- end
52
- end
53
- end
@@ -1,53 +0,0 @@
1
- module PhraseApp
2
- module InContextEditor
3
- class KeyNamesCache
4
- attr_reader :api_wrapper
5
-
6
- def initialize(api_wrapper)
7
- @api_wrapper = api_wrapper
8
- end
9
-
10
- def pre_cached(key_names)
11
- unless cache.cached?(:translation_key_names)
12
- cache.set(:translation_key_names, prefetched_key_names)
13
- end
14
-
15
- return key_names.select { |key_name| key_name_precached?(key_name) }
16
- end
17
-
18
- def pre_fetched(key_names)
19
- key_names.select { |key_name| covered_by_initial_caching?(key_name) }
20
- end
21
-
22
- private
23
-
24
- def prefetched_key_names
25
- prefetched = Set.new
26
- PhraseApp::InContextEditor.cache_key_segments_initial.each do |prefix|
27
- api_wrapper.keys_with_prefix(prefix).each do |key|
28
- prefetched.add(key.name)
29
- end
30
- end
31
- prefetched
32
- end
33
-
34
- def covered_by_initial_caching?(key_name)
35
- key_name.start_with?(*PhraseApp::InContextEditor.cache_key_segments_initial)
36
- end
37
-
38
- def key_name_is_in_cache?(key_name)
39
- cache.get(:translation_key_names).include?(key_name)
40
- end
41
-
42
- def key_name_precached?(key_name)
43
- covered = covered_by_initial_caching?(key_name)
44
- in_cache = key_name_is_in_cache?(key_name)
45
- return covered && in_cache
46
- end
47
-
48
- def cache
49
- RequestStore.store[:phraseapp_cache] ||= PhraseApp::InContextEditor::Cache.new
50
- end
51
- end
52
- end
53
- end
@@ -1,5 +0,0 @@
1
- module PhraseApp
2
- module InContextEditor
3
- VERSION = "1.3.1"
4
- end
5
- end