talk_like_a_pirate 0.2.0 → 0.2.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
  SHA1:
3
- metadata.gz: 8889d472451363ee0bf6512c4b553f7930578c85
4
- data.tar.gz: 1482daf20f68724ff9970cbd2c43f41a17806f61
3
+ metadata.gz: 801d5bcf62c5d990f5cb9b5379e823432dbf6f50
4
+ data.tar.gz: 588ff4de48eae2b03e5702a566fd53099e47593f
5
5
  SHA512:
6
- metadata.gz: ee40a7bcd68a17fe95d9176163cb11b456c08cf3d9cd4241a34e5257c9decef44b6c87ef9097bb8897cbbb21b19042f49cee8fd39b7490fed879384130c427e0
7
- data.tar.gz: 41256b674a9594ad736ccc4229fb2f0212a34f77c32d36aa06fcad9c1afaacc7043f11be42f5a32fec30bc3ac398a88217eb1016e2ad439161774ec727017763
6
+ metadata.gz: 70a8ecb9b3d870979dcf4af96b4e0435eb76bb48e7a1e738bef2cfe67a1d16081692f24bb0c43efd1681e513b23a2d21b52c49539d6ef2416ea3174d3580954e
7
+ data.tar.gz: c8c4718b9978ab21394746386c7265e947f2b8090a9c5b116647d2cee90d8fac12f983dcf940c9dc7243728f157c8505551100953f5444a54716b095202929f8
@@ -0,0 +1,15 @@
1
+ ## [0.2.1] - 2018-10-01
2
+ ### Added
3
+ - rake pirate:initialize installs a sample YAML file into your Rails config folder
4
+ - Specs for I18n generation
5
+
6
+ ### Changed
7
+ - I18n generation now works with YAML files with prefixes (i.e. devise.en.yml)
8
+
9
+ ## [0.2.0] - 2018-10-01
10
+ ### Added
11
+ - Now compatible with Rails 3.2 through 5.2.X
12
+ - Specs for all supported Rails versions
13
+
14
+ ### Changed
15
+ - Class public methods
data/README.md CHANGED
@@ -28,7 +28,11 @@ Config
28
28
  ----
29
29
  The pirate dictionary is fairly generic. You may have domain-specific lingo you think would be hillarious in pirate. So, add on to the dictionary!
30
30
 
31
- Add a config file at
31
+ You can generate a config file using the rake task:
32
+
33
+ rake pirate:initialize
34
+
35
+ Or, add a config file at
32
36
 
33
37
  * Rails:
34
38
  config/pirate_booty.yml
@@ -0,0 +1,9 @@
1
+ dictionary:
2
+ # Capitalization will be handled by talk_like_a_pirate.
3
+ computer: voodoo
4
+ bill: debt
5
+ policies: creeds
6
+ inventory: treasure chest
7
+
8
+ pirate_flavor: # the flavoring occasionally added to the ends of sentences
9
+ - "T' Davy Jones' locker wit ya"
@@ -0,0 +1,48 @@
1
+ class TalkLikeAPirate
2
+ class InstallLocalConfig
3
+ class << self
4
+ def install(target_file_path=nil)
5
+ target_file_path = set_target_file_path(target_file_path)
6
+ if File.exists?(target_file_path)
7
+ "Configuration file already exists at #{target_file_path}"
8
+ else
9
+ copy_local_config_template_to(target_file_path)
10
+ output_local_instructions(target_file_path)
11
+ end
12
+ end
13
+
14
+ private #####################################################################
15
+
16
+ def set_target_file_path(target_file_path)
17
+ if TalkLikeAPirate.on_rails?
18
+ Rails.root.join 'config', 'pirate_booty.yml'
19
+ else
20
+ target_file_path = path_looks_like_file(target_file_path) ? FileUtils.pwd : target_file_path
21
+ File.join target_file_path, 'pirate_booty.yml'
22
+ end
23
+ end
24
+
25
+ def copy_local_config_template_to(target_file_path)
26
+ target_directory = File.dirname(target_file_path)
27
+ FileUtils.mkdir_p(target_directory) unless Dir.exists?(target_directory)
28
+ source_path = File.join File.dirname(__FILE__), 'config', 'sample_config.yml'
29
+
30
+ FileUtils.copy source_path, target_file_path
31
+ end
32
+
33
+ def output_local_instructions(target_file_path)
34
+ return "Configuration installed at #{target_file_path}" if TalkLikeAPirate.on_rails?
35
+ <<-INSTRUCTIONS
36
+ ********************************************************************************************
37
+ Be sure to specify the path to your YAML file in ENV['TALK_LIKE_A_PIRATE_CONFIG_PATH'], i.e.
38
+ export TALK_LIKE_A_PIRATE_CONFIG_PATH="#{target_file_path}"
39
+ ********************************************************************************************
40
+ INSTRUCTIONS
41
+ end
42
+
43
+ def path_looks_like_file(target_file_path)
44
+ target_file_path =~ /\.[^\.\/]*\z/
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,78 @@
1
+ class TalkLikeAPirate
2
+ class I18nTranslator
3
+ def initialize(from_locale: 'en', verbose: false)
4
+ @source_locale = from_locale
5
+ @verbose = verbose
6
+ end
7
+
8
+ def translate_file_or_directory(path='config/locales')
9
+ path = Rails.root.join(path) if TalkLikeAPirate.on_rails? && !path.include?(Rails.root.to_s)
10
+ return translate(path) if File.exists?(path) && file_is_source_yaml_file?(path)
11
+
12
+ directory_contents(path).each do |file_path|
13
+ process_file_or_directory file_path
14
+ end
15
+ end
16
+ alias :translate :translate_file_or_directory
17
+
18
+ private #####################################################################
19
+
20
+ def process_file_or_directory(file_path)
21
+ if File.directory? file_path
22
+ source_path = File.join(file_path, "#{@source_locale}.yml")
23
+ translate_file(source_path) if File.exists?(source_path)
24
+ translate_file_or_directory file_path
25
+ elsif file_is_source_yaml_file?(file_path)
26
+ translate_file file_path
27
+ end
28
+ end
29
+
30
+ def file_is_source_yaml_file?(file_path)
31
+ file_path.to_s.match source_yaml_file_matcher
32
+ end
33
+
34
+ def source_yaml_file_matcher
35
+ # prefix_with_dot locale_name sublocale_name .yml
36
+ /([^\/]*\.)?#{@source_locale}([a-zA-Z-]*).yml/
37
+ end
38
+
39
+ def translate_file(source_file_path)
40
+ pirate_locale_name = pirate_locale_name_for(source_file_path)
41
+ target_file_path = File.join File.dirname(source_file_path), "#{source_filename_prefix(source_file_path)}#{pirate_locale_name}.yml"
42
+ puts "Translatin' #{source_file_path} to #{pirate_locale_name}.yml" if @verbose
43
+
44
+ en_yml = YAML::load_file(source_file_path)
45
+ translation = {pirate_locale_name => parse_element(en_yml).values.first}
46
+ File.open(target_file_path, 'w:utf-8'){|f| YAML::dump translation, f }
47
+ end
48
+
49
+ def pirate_locale_name_for(source_filename)
50
+ TalkLikeAPirate.pirate_locale + source_filename.match(source_yaml_file_matcher)[2]
51
+ end
52
+
53
+ def source_filename_prefix(source_filename)
54
+ source_filename.match(source_yaml_file_matcher)[1]
55
+ end
56
+
57
+ def parse_element(element)
58
+ case element
59
+ when Hash
60
+ new_hash = {}
61
+ element.each{|k,v| new_hash[k] = parse_element(v)}
62
+ new_hash
63
+ when Array
64
+ element.map{|el| parse_element(element)}
65
+ when String
66
+ TalkLikeAPirate.translate element
67
+ else
68
+ element
69
+ end
70
+ end
71
+
72
+ def directory_contents(path)
73
+ Dir.new(path).entries.map do |filename|
74
+ File.join(path, filename) unless ['.', '..'].include?(filename)
75
+ end.compact
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'i18n_translator'
2
+ require_relative '../install_local_config'
3
+
4
+ class LoadTasks < Rails::Railtie
5
+ rake_tasks do
6
+
7
+ namespace 'pirate' do
8
+ desc 'Translate config/locales dictionary into pirrrrate'
9
+ task :translate, [:dir_or_file_to_translate] => :environment do |t, args|
10
+ args.with_defaults(dir_or_file_to_translate: 'config/locales')
11
+ TalkLikeAPirate::I18nTranslator.new.translate args[:dir_or_file_to_translate]
12
+ end
13
+ end
14
+
15
+ namespace 'pirate' do
16
+ desc 'Add a pirate config file to enable customization of the translation dictionary'
17
+ task :initialize, [:target_path] => :environment do |t, args|
18
+ message = TalkLikeAPirate::InstallLocalConfig.install(args[:target_path])
19
+ puts "#{message}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -5,6 +5,14 @@ class TalkLikeAPirate
5
5
  me_string.is_a?(String) ? build_string(me_string) : me_string
6
6
  end
7
7
 
8
+ def pirate_locale
9
+ @@locale ||= local_config['locale'] || config['locale']
10
+ end
11
+
12
+ def on_rails?
13
+ Object.const_defined?(:Rails)
14
+ end
15
+
8
16
  private #####################################################################
9
17
 
10
18
  def build_string(me_string)
@@ -76,10 +84,6 @@ class TalkLikeAPirate
76
84
  capitalize_first(sprinklings_of_flavor.sample) + ["!!","!","."].sample
77
85
  end
78
86
 
79
- def pirate_locale
80
- @@locale ||= local_config.has_key?("locale") ? local_config["locale"] : config["locale"]
81
- end
82
-
83
87
  def dictionary
84
88
  @@dictionary_map ||= config['dictionary'].merge(local_dictionary)
85
89
  end
@@ -90,7 +94,7 @@ class TalkLikeAPirate
90
94
 
91
95
  def config
92
96
  @@config ||= begin
93
- gem_config_path = File.join File.dirname(__FILE__), 'talk_like_a_pirate', 'pirate_booty.yml'
97
+ gem_config_path = File.join File.dirname(__FILE__), 'config', 'pirate_booty.yml'
94
98
  YAML::load_file gem_config_path
95
99
  end
96
100
  end
@@ -105,8 +109,8 @@ class TalkLikeAPirate
105
109
 
106
110
  def local_config
107
111
  @@local_configs ||= begin
108
- local_config_path = if Object.const_defined?(:Rails)
109
- Rails.root.join('config', 'pirate_booty.yml')
112
+ local_config_path = if on_rails?
113
+ Rails.root.join 'config', 'pirate_booty.yml'
110
114
  else
111
115
  ENV['TALK_LIKE_A_PIRATE_CONFIG_PATH']
112
116
  end
@@ -134,8 +138,8 @@ class TalkLikeAPirate
134
138
  end
135
139
  end
136
140
 
137
- if Object.const_defined? :Rails
138
- require 'talk_like_a_pirate/railties'
141
+ if TalkLikeAPirate.on_rails?
142
+ require 'rails/railties'
139
143
  else
140
144
  require 'yaml'
141
145
  end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ include I18nHelperMethods
3
+
4
+ describe TalkLikeAPirate::I18nTranslator do
5
+ let(:source_locale) { 'en' }
6
+ let(:instance) { TalkLikeAPirate::I18nTranslator.new(from_locale: source_locale) }
7
+ setup_temp_folder
8
+ after { remove_temp_files }
9
+
10
+ describe '#translate_file_or_directory' do
11
+ let(:source_filename) { 'en.yml' }
12
+ let(:source_path) { nil }
13
+
14
+ subject do
15
+ create_source_file source_path, source_filename
16
+ instance.translate_file_or_directory(@temp_folder)
17
+ target_filename = source_filename.sub(source_locale, 'arr')
18
+ tempfile_exists? File.join(*[source_path, target_filename].compact)
19
+ end
20
+
21
+ describe 'translated file creation' do
22
+ it { is_expected.to be true }
23
+
24
+ context 'prefixed filenames' do
25
+ let(:source_filename) { 'devise.en.yml' }
26
+ it { is_expected.to be true }
27
+ end
28
+
29
+ context 'sublocale filenames' do
30
+ let(:source_filename) { 'en-US.yml' }
31
+ it { is_expected.to be true }
32
+ end
33
+
34
+ context 'subfolders' do
35
+ let(:source_path) { 'subfolder' }
36
+ it { is_expected.to be true }
37
+ end
38
+
39
+ context 'nested subfolders' do
40
+ let(:source_path) { 'subfolder/another_subfolder' }
41
+ it { is_expected.to be true }
42
+ end
43
+
44
+ context 'multiple source files in folder' do
45
+ before do
46
+ create_source_file '', source_filename_1
47
+ create_source_file '', source_filename_2
48
+ end
49
+ let(:source_filename_1) { 'en.yml' }
50
+ let(:source_filename_2) { 'en-US.yml' }
51
+ it { expect(tempfile_exists?(source_filename_1)).to be true }
52
+ it { expect(tempfile_exists?(source_filename_2)).to be true }
53
+ end
54
+ end
55
+
56
+ describe 'yaml top level key' do
57
+ subject do
58
+ create_source_file '', source_filename
59
+ instance.translate_file_or_directory(@temp_folder)
60
+ target_filename = source_filename.sub(source_locale, 'arr')
61
+
62
+ yaml_for(target_filename).keys[0]
63
+ end
64
+
65
+ it { is_expected.to eq 'arr' }
66
+
67
+ context 'prefixed filenames' do
68
+ let(:source_filename) { 'devise.en.yml' }
69
+ it { is_expected.to eq 'arr' }
70
+ end
71
+
72
+ context 'sublocale filenames' do
73
+ let(:source_filename) { 'en-US.yml' }
74
+ it { is_expected.to eq 'arr-US' }
75
+ end
76
+ end
77
+
78
+ describe 'translations' do
79
+ def subject(yaml_path)
80
+ create_source_file '', 'en.yml'
81
+ instance.translate_file_or_directory(@temp_folder)
82
+ yaml_for('arr.yml').dig *yaml_path
83
+ end
84
+ it { expect(subject(['arr', 'house', 'address'])).to eq "Port o' call"}
85
+ it { expect(subject(['arr', 'house', 'country'])).to eq "Land"}
86
+ it { expect(subject(['arr', 'person', 'employee'])).to eq "Crew"}
87
+ it { expect(subject(['arr', 'person', 'family'])).to eq "kin"}
88
+ end
89
+ end
90
+ end
@@ -1,6 +1,4 @@
1
- require 'talk_like_a_pirate'
2
-
3
- # TODO: Test Rails integration/railties/rake task
1
+ require 'spec_helper'
4
2
 
5
3
  describe TalkLikeAPirate do
6
4
  def execute(string)
@@ -11,10 +9,6 @@ describe TalkLikeAPirate do
11
9
  expect(execute('between')).to eq "betwixt"
12
10
  end
13
11
 
14
- it "shouldn't translate plural words without ActiveSupport" do
15
- expect(execute("islands")).to eq 'islands'
16
- end
17
-
18
12
  it "makes gerunds piratey" do
19
13
  expect(execute('having')).to eq "havin'"
20
14
  end
@@ -23,39 +17,47 @@ describe TalkLikeAPirate do
23
17
  expect(execute('havings')).to eq "havin's"
24
18
  end
25
19
 
26
- it "properly capitalizes single words" do
20
+ it "capitalizes single words" do
27
21
  expect(execute('Boss')).to eq "Admiral"
28
22
  end
29
23
 
30
- it "properly capitalizes all caps words" do
24
+ it "capitalizes all caps words" do
31
25
  expect(execute('BOSS')).to eq "ADMIRAL"
32
26
  expect(execute('BOSS!!!!')).to eq "ADMIRAL!!!!"
33
27
  end
34
28
 
35
- it "properly capitalizes phrases" do
29
+ it "capitalizes phrases" do
36
30
  expect(execute('Bourbon Country')).to eq "Rum Land"
37
31
  expect(execute('Bourbon country')).to eq "Rum land"
38
32
  end
39
33
 
40
- it "properly translates words with trailing punctuation" do
34
+ it "translates words with trailing punctuation" do
41
35
  expect(execute('man!!!')).to eq "pirate!!!"
42
36
  expect(execute('man!?!?!?!')).to eq "pirate!?!?!?!"
43
37
  end
44
38
 
45
- it "properly translates plural gerunds with trailing punctuation" do
39
+ it "translates plural gerunds with trailing punctuation" do
46
40
  expect(execute('belongings!')).to eq "belongin's!"
47
41
  end
48
42
 
49
- it "properly punctuates and translates words with leading and punctuation" do
43
+ it "punctuates and translates words with leading and punctuation" do
50
44
  expect(execute('"The boss said kill."')).to eq '"Tha admiral said keelhaul."'
51
45
  expect(execute('"The boss said to kill the dude!"')).to eq '"Tha admiral said t\' keelhaul tha pirate!"'
52
46
  end
53
47
 
54
- it "translates plural words when ActiveSupport's available" do
55
- require "active_support"
56
- require "active_support/inflector"
48
+ context 'without ActiveSupport' do
49
+ it "does not translate plural words" do
50
+ expect(execute("islands")).to eq 'islands'
51
+ end
52
+ end
53
+
54
+ context 'with ActiveSupport' do
55
+ it "translates plural words" do
56
+ require "active_support"
57
+ require "active_support/inflector"
57
58
 
58
- expect(execute("islands")).to eq 'isles'
59
- expect(execute("men")).to eq 'pirates'
59
+ expect(execute("islands")).to eq 'isles'
60
+ expect(execute("men")).to eq 'pirates'
61
+ end
60
62
  end
61
63
  end
@@ -0,0 +1,5 @@
1
+ require 'talk_like_a_pirate'
2
+ require 'support/i18n_helper_methods'
3
+ require 'rails/i18n_translator'
4
+ RSpec.configure do |c|
5
+ end
@@ -0,0 +1,49 @@
1
+ module I18nHelperMethods
2
+ def setup_temp_folder
3
+ before(:all) do
4
+ tmp_path = File.join File.dirname(__FILE__), 'tmp'
5
+ @temp_folder = FileUtils.mkdir(tmp_path)[0]
6
+ end
7
+ after(:all) do
8
+ FileUtils.rm_r(@temp_folder) unless @temp_folder.nil?
9
+ @temp_folder = nil
10
+ end
11
+ end
12
+
13
+ def tempfile_exists?(target_filename)
14
+ File.exists? tempfile(target_filename)
15
+ end
16
+
17
+ def tempfile_contents(target_filename)
18
+ File.read tempfile(target_filename)
19
+ end
20
+
21
+ def yaml_for(target_filename)
22
+ YAML.load(tempfile_contents(target_filename))
23
+ end
24
+
25
+ def tempfile(target_filename)
26
+ File.join(@temp_folder, target_filename)
27
+ end
28
+
29
+ def remove_temp_files
30
+ return if @temp_folder.nil? || @temp_folder == ''
31
+ Dir.glob(@temp_folder).each do |file_path|
32
+ next if ['.','..',@temp_folder].include?(file_path)
33
+ FileUtils.remove_entry file_path
34
+ end
35
+ end
36
+
37
+ def create_source_file(target_path, target_filename)
38
+ target_path = File.join [@temp_folder, target_path].compact
39
+ FileUtils.mkdir_p(target_path) unless Dir.exists?(target_path)
40
+
41
+ source_path = File.join File.dirname(__FILE__), 'source_template.yml'
42
+ target_file_path = File.join target_path, target_filename
43
+ FileUtils.copy source_path, target_file_path
44
+ end
45
+
46
+ def delete_config_file
47
+ FileUtils.remove_entry File.dirname(target_file_path)
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ en:
2
+ house:
3
+ address: Address
4
+ country: Country
5
+ person:
6
+ employee: Employee
7
+ family: family
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = 'talk_like_a_pirate'
4
- spec.version = '0.2.0'
4
+ spec.version = '0.2.1'
5
5
  spec.authors = ['Steve Hodges']
6
6
  spec.email = ['shodges317@gmail.com']
7
7
  spec.homepage = 'https://github.com/stevehodges/talk_like_a_pirate'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: talk_like_a_pirate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Hodges
@@ -109,16 +109,23 @@ extra_rdoc_files: []
109
109
  files:
110
110
  - ".gitignore"
111
111
  - Appraisals
112
+ - CHANGELOG.md
112
113
  - Gemfile
113
114
  - Gemfile.lock
114
115
  - LICENSE
115
116
  - README.md
116
117
  - Rakefile
118
+ - lib/config/pirate_booty.yml
119
+ - lib/config/sample_config.yml
120
+ - lib/install_local_config.rb
121
+ - lib/rails/i18n_translator.rb
122
+ - lib/rails/railties.rb
117
123
  - lib/talk_like_a_pirate.rb
118
- - lib/talk_like_a_pirate/pirate_booty.yml
119
- - lib/talk_like_a_pirate/railties.rb
120
- - lib/talk_like_a_pirate/tasks/pirate.rake
124
+ - spec/lib/i18n_translator_spec.rb
121
125
  - spec/lib/talk_like_a_pirate_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/i18n_helper_methods.rb
128
+ - spec/support/source_template.yml
122
129
  - talk_like_a_pirate.gemspec
123
130
  homepage: https://github.com/stevehodges/talk_like_a_pirate
124
131
  licenses: []
@@ -1,5 +0,0 @@
1
- class LoadTasks < Rails::Railtie
2
- rake_tasks do
3
- Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
4
- end
5
- end
@@ -1,57 +0,0 @@
1
- namespace "pirate" do
2
- desc "Use this task to translate config/locales dictionary into pirrrrate"
3
- task :translate, :dir_or_file_to_translate do |t, args|
4
- require 'cgi'
5
- args.with_defaults(:dir_or_file_to_translate => "config/locales")
6
- translate_locale(args[:dir_or_file_to_translate])
7
- end
8
- end
9
-
10
- def translate(filename)
11
- en_yml = YAML::load_file(filename)
12
- target_filename = TalkLikeAPirate.pirate_locale + filename.match(/\/en([a-zA-Z-]*).yml/)[1]
13
- dirname = filename.split("/")[0..-2].join("/")
14
- translation = {target_filename => parse_element(en_yml).values.first}
15
- File.open(dirname + "/#{target_filename}.yml", 'w:utf-8') do |f|
16
- YAML::dump translation, f
17
- end
18
- end
19
-
20
- def translate_locale(path)
21
- path = "#{Rails.root}/#{path}" unless path.include? Rails.root.to_s
22
- return translate(path) if File.exists?(path) && path.match("/\/en([a-zA-Z-]*).yml")
23
-
24
- relevent_file_paths(path).each do |file_path|
25
- if File.directory? file_path
26
- translate(file_path + "/en.yml") if File.exists?(file_path + "/en.yml")
27
- translate_locale file_path
28
- elsif file_path.match("\/en([a-zA-Z-]*).yml")
29
- puts file_path
30
- translate file_path
31
- end
32
- end
33
- end
34
-
35
- def parse_element(element)
36
- case element
37
- when Hash
38
- new_hash = {}
39
- element.each {|k,v| new_hash[k] = parse_element(v)}
40
- new_hash
41
- when Array
42
- element.map {|el| parse_element(element)}
43
- when String
44
- TalkLikeAPirate.translate element
45
- else
46
- element
47
- end
48
- end
49
-
50
- def relevent_file_paths(path)
51
- Dir.new(path).entries.reject do |filename|
52
- [".", ".."].include? filename
53
- end.map do |filename|
54
- "#{path}/#{filename}"
55
- end
56
- end
57
-