rosette 0.3.0 → 0.3.1

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
  SHA256:
3
- metadata.gz: 97ee90651ff8eb4ca983a10bf740934c878df05922013c149306b42e56778f21
4
- data.tar.gz: 78b300250348617901cd7d2165b9fe0763ad49bf3327850ffdb2852d9dd55678
3
+ metadata.gz: 7d06214ac63c9666ca0fd74730175cc9eb308dbd16bd83e3fca8e9336d7123dc
4
+ data.tar.gz: d7521ae037801fa8c83d74dd2a22f6aa11f3269f90e60fc117746429b238564b
5
5
  SHA512:
6
- metadata.gz: 182c58aa6fa14ba3b25aaf89d11dcc8e1c461fda6d1fd92b34ab709f98eade14da02d6afd66b994c4ddb2191f07ae5c5db281b131c2dfa280ff6c94bdb616761
7
- data.tar.gz: d6a1b2bb51890b68a9f33d9bd3aa799a55a7cc6135abdd614747ca2118bf6a139c50ae0874f5d99dcd5fa160d700c5a344bc5eb333bb4fe229549c4724bf74ee
6
+ metadata.gz: 2118244884e6269ae5b3ef2146037ca7086fe32bad51f4bc4be5ab9ba3a12c262cb9244ef48d564537f3223185b3f4f563787b1b14bbc4439a078e2a0dff6267
7
+ data.tar.gz: 62c3f6715880adb58917f3a69544673b4110c8b47f0f1e7192e4858919104e2679925f86bba89370410c049379f56de7c72818fe67f6752789617651c85828fe
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/setup"
2
4
 
3
5
  APP_RAKEFILE = File.expand_path("spec/example_app/Rakefile", __dir__)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rosette
2
4
  class ApplicationController < ActionController::Base
3
5
  end
@@ -2,12 +2,13 @@
2
2
 
3
3
  module Rosette
4
4
  class TranslationsController < ApplicationController
5
+
5
6
  def create
6
7
  available_locales.each { |locale, translation| Manager.create(locale, key, translation) }
7
8
 
8
9
  Manager.normalize!
9
10
 
10
- redirect_to redirect_path, notice: 'Translation(s) added'
11
+ redirect_to redirect_path, notice: "Translation(s) added"
11
12
  rescue StandardError => e
12
13
  @error_message = e.message
13
14
  render_rosette_new(key, redirect_path)
@@ -15,20 +16,21 @@ module Rosette
15
16
 
16
17
  private
17
18
 
18
- def translations_params
19
- params.require(:translations).permit(:key, :redirect_path, available_locales: {})
20
- end
19
+ def translations_params
20
+ params.require(:translations).permit(:key, :redirect_path, available_locales: {})
21
+ end
21
22
 
22
- def available_locales
23
- translations_params[:available_locales]
24
- end
23
+ def available_locales
24
+ translations_params[:available_locales]
25
+ end
25
26
 
26
- def key
27
- translations_params[:key]
28
- end
27
+ def key
28
+ translations_params[:key]
29
+ end
30
+
31
+ def redirect_path
32
+ URI.parse(translations_params[:redirect_path]).to_s
33
+ end
29
34
 
30
- def redirect_path
31
- URI.parse(translations_params[:redirect_path]).to_s
32
- end
33
35
  end
34
36
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rosette
2
4
  module ApplicationHelper
3
5
  end
@@ -1,5 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rosette
2
4
  class ApplicationRecord < ActiveRecord::Base
5
+
3
6
  self.abstract_class = true
7
+
4
8
  end
5
9
  end
data/bin/rails CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  # This command will automatically be run when you run "rails" with Rails gems
3
5
  # installed from the root of your application.
4
6
 
data/bin/rosette CHANGED
@@ -2,21 +2,20 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  begin
5
- require File.join(Dir.pwd, 'config/environment')
5
+ require File.join(Dir.pwd, "config/environment")
6
6
  rescue LoadError
7
7
  puts <<~ERR
8
+
8
9
  Could not load your Rails app
9
10
  =============================
10
11
  Rosette assumes you have a config/environment.rb file it can load. If this is the case, please
11
12
  make sure you are using Rosette CLI from the root of your Rails application. Do not hesitate to
12
13
  raise an issue if you need further assistance.
14
+
13
15
  ERR
14
16
  exit 1
15
17
  end
16
18
 
17
- require 'rosette'
18
-
19
- command = ARGV.shift
20
- ARGV.clear
19
+ require "rosette"
21
20
 
22
- Rosette::CLI.run(command: command)
21
+ Rosette::CLI.run
data/config/routes.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rosette::Engine.routes.draw do
2
4
  resources :translations, only: [:create]
3
5
  end
data/lib/rosette/cli.rb CHANGED
@@ -1,71 +1,84 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "tty-prompt"
4
+
3
5
  module Rosette
4
6
  class CLI
5
- COMMANDS = %w[add remove read].freeze
6
7
 
7
8
  class << self
8
- def run(command:)
9
- return display_help unless command.in?(COMMANDS)
10
9
 
11
- ask_user_for_key
12
- send(command)
10
+ attr_reader :prompt, :command
11
+
12
+ delegate :select, :ask, to: :@prompt
13
+
14
+ def run
15
+ ask_for_command
16
+
17
+ return help if command == "help"
18
+
19
+ ask_for_key
20
+ send command
13
21
  end
14
22
 
15
23
  private
16
24
 
17
- def read
18
- Rosette.available_locales.each do |locale|
19
- translation = Manager.read(locale, @key)
20
- puts "Translation for #{locale} is: #{translation}"
25
+ def ask_for_command
26
+ @prompt = TTY::Prompt.new
27
+ @command = select("What do you want to achieve?") do |menu|
28
+ menu.choice("Read a translation", "read")
29
+ menu.choice("Add a translation", "add")
30
+ menu.choice("Remove a translation", "remove")
31
+ menu.choice("Display help", "help")
32
+ end
21
33
  end
22
- end
23
34
 
24
- def add
25
- Rosette.available_locales.each do |locale|
26
- puts "Please enter #{locale} translation:"
27
- Manager.create(locale, @key, gets.chomp)
35
+ def ask_for_key
36
+ @key = ask "Please provide the key to translation:"
37
+ normalize_key!
28
38
  end
29
- end
30
39
 
31
- def remove
32
- Rosette.available_locales.each do |locale|
33
- Manager.delete(locale, @key)
40
+ def normalize_key!
41
+ Rosette.available_locales.each do |locale|
42
+ @key = @key.delete_prefix("#{locale}.")
43
+ end
34
44
  end
35
- end
36
45
 
37
- def ask_user_for_key
38
- puts 'Please provide the key to translation:'
39
- @key = gets.chomp
46
+ # COMMANDS
40
47
 
41
- normalize_key!
42
- end
48
+ def read
49
+ Rosette.available_locales.each do |locale|
50
+ translation = Manager.read(locale, @key)
51
+ puts "Translation for #{locale} is: #{translation}"
52
+ end
53
+ end
43
54
 
44
- def normalize_key!
45
- Rosette.available_locales.each do |locale|
46
- @key.delete_prefix!("#{locale}.")
55
+ def add
56
+ Rosette.available_locales.each do |locale|
57
+ translation = ask "Please enter #{locale} translation:"
58
+ Manager.create(locale, @key, translation)
59
+ end
47
60
  end
48
- end
49
61
 
50
- def display_help
51
- puts <<~HELP
52
- Usage: rosette [command]
62
+ def remove
63
+ Rosette.available_locales.each do |locale|
64
+ Manager.delete(locale, @key)
65
+ end
66
+ end
53
67
 
54
- Available commands:
68
+ def help
69
+ puts <<~HELP
55
70
 
56
- read read translation from available locales
57
- add add a translation to available locales
58
- remove remove translation from available locales
71
+ For each command you must provide a key pointing to the translation.
72
+ It can begin with or without the locale. These are all valid keys:
59
73
 
60
- For each command you must provide a key pointing to the translation.
61
- It can begin with or without the locale. These are all valid keys:
74
+ fr.activemodel.errors.blank
75
+ en.activemodel.errors.blank
76
+ activemodel.errors.blank
62
77
 
63
- fr.activemodel.errors.blank
64
- en.activemodel.errors.blank
65
- activemodel.errors.blank
78
+ HELP
79
+ end
66
80
 
67
- HELP
68
- end
69
81
  end
82
+
70
83
  end
71
84
  end
@@ -2,20 +2,22 @@
2
2
 
3
3
  module Rosette
4
4
  module Controller
5
+
5
6
  extend ActiveSupport::Concern
6
7
 
7
8
  included { rescue_from I18n::MissingTranslationData, with: :add_missing_translation_data }
8
9
 
9
10
  private
10
11
 
11
- def add_missing_translation_data(exception)
12
- key = exception.message.sub(/translation missing:.+?\./, '')
12
+ def add_missing_translation_data(exception)
13
+ key = exception.message.sub(/translation missing:.+?\./, "")
14
+
15
+ render_rosette_new(key, request.fullpath)
16
+ end
13
17
 
14
- render_rosette_new(key, request.fullpath)
15
- end
18
+ def render_rosette_new(key, redirect_path)
19
+ render "rosette/new", layout: "rosette/application", locals: { key: key, redirect_path: redirect_path }
20
+ end
16
21
 
17
- def render_rosette_new(key, redirect_path)
18
- render 'rosette/new', layout: 'rosette/application', locals: { key: key, redirect_path: redirect_path }
19
- end
20
22
  end
21
23
  end
@@ -2,16 +2,18 @@
2
2
 
3
3
  module Rosette
4
4
  class Engine < ::Rails::Engine
5
+
5
6
  isolate_namespace Rosette
6
7
 
7
8
  config.after_initialize do |app|
8
9
  app.routes.prepend do
9
- mount Rosette::Engine, at: '/rosette'
10
+ mount Rosette::Engine, at: "/rosette"
10
11
  end
11
12
  end
12
13
 
13
14
  ActiveSupport.on_load(:action_controller_base) do
14
15
  include Rosette::Controller
15
16
  end
17
+
16
18
  end
17
19
  end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Manager
4
- %w[create read delete].each do |verb|
4
+
5
+ ["create", "read", "delete"].each do |verb|
5
6
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
6
7
  def self.#{verb}(*args)
7
8
  new(*args).send('#{verb}')
@@ -11,63 +12,64 @@ class Manager
11
12
 
12
13
  private
13
14
 
14
- def initialize(locale, key, translation = nil)
15
- @file_path = Rails.root.join("config/locales/#{locale}.yml")
16
- @keys = normalize "#{locale}.#{key}"
17
- @translation = translation
18
- end
19
-
20
- def read
21
- stored_translations.dig(*@keys)
22
- end
15
+ def initialize(locale, key, translation = nil)
16
+ @file_path = Rails.root.join("config/locales/#{locale}.yml")
17
+ @keys = normalize "#{locale}.#{key}"
18
+ @translation = translation
19
+ end
23
20
 
24
- def create
25
- new_translation = @keys.reverse.inject(@translation) { |v, k| { k => v } }
26
- updated_translations = deep_merge(stored_translations, new_translation)
27
- persist(updated_translations)
28
-
29
- Manager.normalize!
30
- end
21
+ def read
22
+ stored_translations.dig(*@keys)
23
+ end
31
24
 
32
- def delete
33
- updated_translations = delete_translation(stored_translations, @keys)
34
- persist(updated_translations)
25
+ def create
26
+ new_translation = @keys.reverse.inject(@translation) { |v, k| { k => v } }
27
+ updated_translations = deep_merge(stored_translations, new_translation)
28
+ persist(updated_translations)
35
29
 
36
- Manager.normalize!
37
- end
30
+ Manager.normalize!
31
+ end
38
32
 
39
- def delete_translation(translations, keys)
40
- return translations.delete(keys.first) if keys.length == 1
33
+ def delete
34
+ updated_translations = delete_translation(stored_translations, @keys)
35
+ persist(updated_translations)
41
36
 
42
- head, *tail = keys
43
- delete_translation(translations[head], tail)
37
+ Manager.normalize!
38
+ end
44
39
 
45
- translations
46
- end
40
+ def delete_translation(translations, keys)
41
+ return translations.delete(keys.first) if keys.length == 1
47
42
 
48
- def persist(translations)
49
- File.write(@file_path, translations.deep_stringify_keys.to_yaml)
50
- end
43
+ head, *tail = keys
44
+ delete_translation(translations[head], tail)
51
45
 
52
- def stored_translations
53
- YAML.safe_load(File.read(@file_path), symbolize_names: true)
54
- end
46
+ translations
47
+ end
48
+
49
+ def persist(translations)
50
+ File.write(@file_path, translations.deep_stringify_keys.to_yaml)
51
+ end
55
52
 
56
- def deep_merge(hash, other_hash)
57
- hash.merge(other_hash) do |_key, this_val, other_val|
58
- if this_val.is_a?(Hash) && other_val.is_a?(Hash)
59
- deep_merge(this_val, other_val)
60
- else
61
- other_val
53
+ def stored_translations
54
+ YAML.safe_load(File.read(@file_path), symbolize_names: true)
55
+ end
56
+
57
+ def deep_merge(hash, other_hash)
58
+ hash.merge(other_hash) do |_key, this_val, other_val|
59
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
60
+ deep_merge(this_val, other_val)
61
+ else
62
+ other_val
63
+ end
62
64
  end
63
65
  end
64
- end
65
66
 
66
- def normalize(key)
67
- key.split('.').map(&:to_sym)
68
- end
67
+ def normalize(key)
68
+ key.split(".").map(&:to_sym)
69
+ end
70
+
71
+ def self.normalize!
72
+ I18n::Tasks::CLI.start(["normalize"]) if Rosette.normalize
73
+ end
69
74
 
70
- def self.normalize!
71
- I18n::Tasks::CLI.start(['normalize']) if Rosette.normalize
72
- end
73
75
  end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rosette
2
- VERSION = "0.3.0"
4
+
5
+ VERSION = "0.3.1"
6
+
3
7
  end
data/lib/rosette.rb CHANGED
@@ -10,9 +10,11 @@ require "i18n/tasks/cli"
10
10
  require "yaml"
11
11
 
12
12
  module Rosette
13
+
13
14
  mattr_accessor :normalize
14
15
 
15
16
  def self.available_locales
16
17
  Rails.configuration.i18n.available_locales.map(&:to_s)
17
18
  end
19
+
18
20
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # desc "Explaining what the task does"
2
3
  # task :rosette do
3
4
  # # Task goes here
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rosette
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Platteeuw
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-18 00:00:00.000000000 Z
11
+ date: 2023-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n-tasks
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 7.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-prompt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.23.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.23.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec-rails
43
57
  requirement: !ruby/object:Gem::Requirement