wor 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c6c0153991d04b7e5455702cbe0791b867d7e70
4
- data.tar.gz: cb25136401a2339342fbda30215edca734143fdf
3
+ metadata.gz: a3a97cac4c47daa5b8066be8e72cfecb6b120179
4
+ data.tar.gz: f8bf53bc4c0798c05e8ad543cf1922d994000321
5
5
  SHA512:
6
- metadata.gz: c2257badb3676c73687323a5f1221f4aafff0caa9f25532dffe4ee06ad983a807cf696163c2def7b362af92c58ec55f20eba17c24dbc41a8dfa9aac59c937395
7
- data.tar.gz: 163588ab97a54f854507af0a7042792b313238206c02d708f419fd77d5ec7759b3f7acc228242ac44fc70040bcb1b1f804d2cb38b79b5d853d82913aee82e9c7
6
+ metadata.gz: 32ca962788a29ccb71eebe3afa297d82cce48e5962361172a657dc4fd4ddc52f4d0e87d3f9cb1edecd7c6c3cecc82b58cd3c702fbeb6e418eebe2950c5dbf79c
7
+ data.tar.gz: 7a335f27b49f245fa256cf6198bfc3cf370c04c3a7212713e111f5d87ec431bd7f34f8a1885a6db0c86a77241e14d1e203bb1ae0bafc0319e3eb819a0a8204f9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wor (0.0.3)
4
+ wor (0.0.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,6 +2,54 @@ module WOR
2
2
  class Collection
3
3
  attr_accessor :name
4
4
 
5
+ # Class Methods (Static)
6
+ class << self
7
+ def get_folder_name params
8
+ if (!WOR::Configuration::USE_LOCALIZATION)
9
+ return params[:collection_name]
10
+ end
11
+
12
+ locale_info = WOR::Localization.read_locale_file(params[:locale])
13
+
14
+ if (locale_info.nil?)
15
+ return params[:collection_name]
16
+ end
17
+
18
+ locale_info['paths'].each do | key, value |
19
+ if (value == params[:collection_name].to_s)
20
+ return key
21
+ end
22
+ end
23
+
24
+ return params[:collection_name]
25
+ end
26
+
27
+ def get_folder_name_by_path(params)
28
+ original_collection_name = WOR::Helpers.get_parent_directory_name params[:path]
29
+ return get_folder_name(locale: params[:locale], collection_name: original_collection_name)
30
+ end
31
+
32
+ def get_folder_url(params)
33
+ if (!WOR::Configuration::USE_LOCALIZATION)
34
+ return params[:collection_name]
35
+ end
36
+
37
+ locale_info = WOR::Localization.read_locale_file(params[:locale])
38
+
39
+ if (locale_info.nil?)
40
+ return params[:collection_name]
41
+ end
42
+
43
+ locale_info['paths'].each do | key, value |
44
+ if (key == params[:collection_name].to_s)
45
+ return value
46
+ end
47
+ end
48
+
49
+ return params[:collection_name]
50
+ end
51
+ end
52
+
5
53
  def initialize collection_name
6
54
  @name = collection_name
7
55
  end
@@ -1,9 +1,8 @@
1
1
  module WOR
2
2
  class CollectionItem
3
- attr_accessor :path, :body, :layout, :name
3
+ attr_accessor :body, :layout, :locale, :collection_name, :name
4
4
 
5
5
  def initialize path
6
- self.path = path
7
6
  self.body = Hash.new
8
7
  self.layout = "application"
9
8
  self.name = "Null"
@@ -13,15 +12,26 @@ module WOR
13
12
  return
14
13
  end
15
14
 
15
+ if (WOR::Configuration::USE_LOCALIZATION)
16
+ self.locale = WOR::Localization.get_locale
17
+ self.collection_name = WOR::Collection.get_folder_name_by_path(path: path, locale: self.locale)
18
+ else
19
+ self.collection_name = WOR::Collection.get_folder_name_by_path(path: path)
20
+ end
21
+
16
22
  if (File.file?(path))
17
- extn = File.extname path
18
- self.name = File.basename path, extn
19
- create_custom_properties(self.path, true)
23
+ file_initialize path
20
24
  else
21
- directory_initialize self.path
25
+ directory_initialize path
22
26
  end
23
27
  end
24
28
 
29
+ def file_initialize path
30
+ extn = File.extname path
31
+ self.name = File.basename path, extn
32
+ create_custom_properties(path, true)
33
+ end
34
+
25
35
  def directory_initialize path
26
36
  files = Dir.glob(path + "/*")
27
37
 
@@ -75,6 +85,24 @@ module WOR
75
85
  self.body[language] = file_data
76
86
  end
77
87
 
88
+ def url
89
+ if (self.collection_name == "pages")
90
+ translated_collection = ""
91
+ else
92
+ translated_collection = "/" + WOR::Localization.translate_path_to_language(path: self.collection_name, locale: self.locale)
93
+ end
94
+
95
+ if (self.name == "index")
96
+ translated_name = ""
97
+ else
98
+ translated_name = "/" + WOR::Localization.translate_path_to_language(path: self.name, locale: self.locale)
99
+ end
100
+
101
+ url = "/#{self.locale}#{translated_collection}#{translated_name}"
102
+
103
+ return url
104
+ end
105
+
78
106
  private
79
107
  def create_custom_properties file, get_body
80
108
  results = File.read(file).match(/(---.+---)(.*)/m)
@@ -0,0 +1,6 @@
1
+ module WOR
2
+ class Configuration
3
+ USE_LOCALIZATION = true
4
+ DEFAULT_LOCALE = "en"
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ module WOR
2
+ class Helpers
3
+ class << self
4
+ def get_parent_directory_name path
5
+ parent_path = File.expand_path("..", path)
6
+ return File.basename parent_path
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,73 @@
1
+ module WOR
2
+ class Localization
3
+
4
+ # Class Methods (Statics)
5
+ class << self
6
+ def translate_path_to_original params
7
+ if (!WOR::Configuration::USE_LOCALIZATION)
8
+ return params[:path]
9
+ end
10
+
11
+ locale_info = WOR::Localization.read_locale_file(params[:locale])
12
+
13
+ if (locale_info.nil?)
14
+ return params[:path]
15
+ end
16
+
17
+ locale_info['paths'].each do | key, value |
18
+ if (value == params[:path].to_s)
19
+ return key
20
+ end
21
+ end
22
+
23
+ return params[:path]
24
+ end
25
+
26
+ def translate_path_to_language params
27
+ if (!WOR::Configuration::USE_LOCALIZATION)
28
+ return params[:path]
29
+ end
30
+
31
+ locale_info = WOR::Localization.read_locale_file(params[:locale])
32
+
33
+ if (locale_info.nil?)
34
+ return params[:path]
35
+ end
36
+
37
+ locale_info['paths'].each do | key, value |
38
+ if (key == params[:path].to_s)
39
+ return value
40
+ end
41
+ end
42
+
43
+ return params[:path]
44
+ end
45
+
46
+ def get_locale
47
+ locale = I18n.locale
48
+
49
+ if (locale.nil?)
50
+ locale = Wor::Configuration::DEFAULT_LOCALE
51
+ end
52
+
53
+ return locale
54
+ end
55
+
56
+ def read_locale_file locale
57
+ require 'yaml'
58
+
59
+ path = "#{Rails.root}/config/locales/wor.#{locale}.yml"
60
+
61
+ locale_info = nil
62
+
63
+ begin
64
+ locale_info = YAML.load_file(path)
65
+ rescue
66
+ locale_info = nil
67
+ end
68
+
69
+ return locale_info
70
+ end
71
+ end
72
+ end
73
+ end
data/lib/wor.rb CHANGED
@@ -3,61 +3,39 @@ require 'rails'
3
3
  module WOR
4
4
  autoload :Collection, 'wor/collection'
5
5
  autoload :CollectionItem, 'wor/collection_item'
6
+ autoload :Configuration, 'wor/configuration'
7
+ autoload :Helpers, 'wor/helpers'
8
+ autoload :Localization, 'wor/localization'
6
9
 
7
10
  class Core
8
- def self.find params
9
- collection = self.get_collection(locale: params[:locale], collection: params[:collection])
10
-
11
- target_item = nil
12
-
13
- I18n.locale = params[:locale]
14
-
15
- collection.items.each do | item |
16
- if (item.name == params[:page_name] ||
17
- item.get_permalink(params[:locale].to_s) == params[:page_name].to_s)
18
- target_item = item
11
+ # Class Methods (Static)
12
+ class << self
13
+ def find params
14
+ # Build path
15
+ path = "#{Rails.root}/content"
16
+
17
+ if (params[:locale].present?)
18
+ if (WOR::Configuration::DEFAULT_LOCALE == params[:locale].to_s)
19
+ path += "/#{params[:collection]}"
20
+ else
21
+ collection_name = params[:collection]
22
+ translated_collection_name = WOR::Collection.get_folder_name(locale: params[:locale], collection_name: collection_name)
23
+ path += "/#{params[:collection]}"
24
+ end
25
+
26
+ path += "/#{WOR::Localization.translate_path_to_original(path: params[:page_name], locale: params[:locale])}.md"
27
+ else
28
+ path += "/#{params[:page_name]}.md"
19
29
  end
20
- end
21
-
22
- return target_item
23
- end
24
-
25
- def self.get_collection params
26
- folder_name = self.get_collection_folder_name(params[:locale], params[:collection])
27
-
28
- return WOR::Collection.new folder_name
29
- end
30
30
 
31
- def self.get_collection_folder_name(locale, collection_name)
32
- locale_info = self.read_locale_file(locale)
31
+ I18n.locale = params[:locale]
33
32
 
34
- if (locale_info.nil?)
35
- return collection_name
33
+ return CollectionItem.new path
36
34
  end
37
35
 
38
- locale_info['paths'].each do | key, value |
39
- if (value == collection_name)
40
- return key
41
- end
42
- end
43
-
44
- return collection_name
45
- end
36
+ def build_path
46
37
 
47
- def self.read_locale_file locale
48
- require 'yaml'
49
-
50
- path = "#{Rails.root}/config/locales/wor.#{locale}.yml"
51
-
52
- locale_info = nil
53
-
54
- begin
55
- locale_info = YAML.load_file(path)
56
- rescue
57
- locale_info = nil
58
38
  end
59
-
60
- return locale_info
61
39
  end
62
40
  end
63
41
  end
@@ -1,2 +1,3 @@
1
1
  paths:
2
- courses: 'cursos'
2
+ courses: 'cursos'
3
+ test: 'teste'
@@ -1,5 +1,4 @@
1
1
  ---
2
- name: Curso Teste
3
2
  permalink: test
4
3
  permalink_pt: teste
5
4
  ---
@@ -0,0 +1,8 @@
1
+ ---
2
+ layout: page
3
+ permalink: courses
4
+ permalink_pt: courses
5
+ ---
6
+ <div>
7
+ Content of courses.md
8
+ </div>
data/spec/wor_spec.rb CHANGED
@@ -2,55 +2,110 @@ require 'wor'
2
2
  require 'test_helper'
3
3
 
4
4
  describe WOR::Core do
5
- it 'get permalink' do
6
- item = WOR::CollectionItem.new "#{Rails.root}/content/courses/curso-teste.md"
7
-
8
- expect(item.get_permalink("en")).to eql("test")
5
+ it 'find' do
6
+ params = { collection: "pages", locale: "en", page_name: "index"}
7
+ target = WOR::Core.find(params)
8
+
9
+ expect(target.permalink).not_to be(nil)
9
10
  end
10
11
 
11
- it 'get permalink index in pt' do
12
- item_index = WOR::CollectionItem.new "#{Rails.root}/content/pages/index.md"
13
- expect(item_index.get_permalink("pt")).to eql("/")
12
+ it 'find with basename' do
13
+ params = { collection: "cursos", locale: "pt", page_name: "teste"}
14
+ target = WOR::Core.find(params)
15
+
16
+ expect(target).not_to be(nil)
17
+ end
18
+
19
+ it 'find with basename without item have permalink on language' do
20
+ params = { collection: "courses", locale: "en", page_name: "test" }
21
+ target = WOR::Core.find(params)
22
+
23
+ expect(target.permalink).not_to be(nil)
24
+ end
25
+
26
+ it 'get url of root in default language' do
27
+ params = { collection: "pages", locale: "en", page_name: "index" }
28
+ target = WOR::Core.find(params)
29
+
30
+ expect(target.url).to eq("/en")
31
+ end
32
+
33
+ it 'get url of root in pt language' do
34
+ params = { collection: "pages", locale: "pt", page_name: "index" }
35
+ target = WOR::Core.find(params)
36
+
37
+ expect(target.url).to eq("/pt")
38
+ end
39
+
40
+ it 'get url of page in default language' do
41
+ params = { collection: "pages", locale: "en", page_name: "courses" }
42
+ target = WOR::Core.find(params)
43
+
44
+ expect(target.url).to eq("/en/courses")
45
+ end
46
+
47
+ it 'get url of page in pt language' do
48
+ params = { collection: "pages", locale: "pt", page_name: "cursos" }
49
+ target = WOR::Core.find(params)
50
+
51
+ expect(target.url).to eq("/pt/cursos")
14
52
  end
53
+ end
15
54
 
16
- it 'get collection folder name in default language' do
17
- collection_folder_name = WOR::Core.get_collection_folder_name('en', 'courses')
18
- expect(collection_folder_name).to eql("courses")
55
+ describe WOR::Helpers do
56
+ it 'return parent directory of file' do
57
+ parent = WOR::Helpers.get_parent_directory_name "#{Rails.root}/content/courses/curso-teste.md"
58
+ expect(parent).to eq("courses")
19
59
  end
60
+ end
20
61
 
21
- it 'get collection folder name in pt language' do
22
- collection_folder_name = WOR::Core.get_collection_folder_name('pt', 'cursos')
23
- expect(collection_folder_name).to eql("courses")
62
+ describe WOR::Collection do
63
+ it 'return original collection name' do
64
+ locale = 'pt'
65
+ original_collection_name = WOR::Collection.get_folder_name(locale: locale, collection_name: 'cursos')
66
+ expect(original_collection_name).to eq("courses")
24
67
  end
68
+ end
25
69
 
70
+ describe WOR::Localization do
26
71
  it 'read locale file that exists' do
27
- locale_file = WOR::Core.read_locale_file('en')
72
+ locale_file = WOR::Localization.read_locale_file('en')
28
73
  expect(locale_file).not_to be(nil)
29
74
  end
30
75
 
31
76
  it 'read locale file that not exists' do
32
- locale_file = WOR::Core.read_locale_file('fr')
77
+ locale_file = WOR::Localization.read_locale_file('fr')
33
78
  expect(locale_file).to be(nil)
34
79
  end
35
80
 
36
- it 'find' do
37
- params = { collection: "pages", locale: "en", page_name: "index"}
38
- target = WOR::Core.find(params)
81
+ it 'get translated url in default language' do
82
+ url = WOR::Localization.translate_path_to_language(path: "courses", locale: "en")
39
83
 
40
- expect(target.permalink).not_to be(nil)
84
+ expect(url).to eq("courses")
41
85
  end
42
86
 
43
- it 'find with basename' do
44
- params = { collection: "cursos", locale: "pt", page_name: "teste"}
45
- target = WOR::Core.find(params)
87
+ it 'get translated url in pt language' do
88
+ url = WOR::Localization.translate_path_to_language(path: "courses", locale: "pt")
46
89
 
47
- expect(target).not_to be(nil)
90
+ expect(url).to eq("cursos")
48
91
  end
92
+ end
49
93
 
50
- it 'find with basename without item have permalink on language' do
51
- params = { collection: "courses", locale: "en", page_name: "test" }
52
- target = WOR::Core.find(params)
94
+ describe WOR::CollectionItem do
95
+ it 'get url in default language' do
96
+ I18n.locale = 'en'
97
+ item = WOR::CollectionItem.new "#{Rails.root}/content/courses/test.md"
98
+ expect(item.url).to eql("/en/courses/test")
99
+ end
53
100
 
54
- expect(target.permalink).not_to be(nil)
101
+ it 'get url in pt language' do
102
+ I18n.locale = 'pt'
103
+ item = WOR::CollectionItem.new "#{Rails.root}/content/courses/test.md"
104
+ expect(item.url).to eql("/pt/cursos/teste")
105
+ end
106
+
107
+ it 'get permalink index in pt' do
108
+ item_index = WOR::CollectionItem.new "#{Rails.root}/content/pages/index.md"
109
+ expect(item_index.get_permalink("pt")).to eql("/")
55
110
  end
56
111
  end
data/wor.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'wor'
3
- s.version = '0.0.4'
3
+ s.version = '0.0.5'
4
4
  s.date = '2017-05-07'
5
5
  s.summary = "To do summary!"
6
6
  s.description = "To do description"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Vargas
@@ -49,6 +49,9 @@ files:
49
49
  - lib/wor.rb
50
50
  - lib/wor/collection.rb
51
51
  - lib/wor/collection_item.rb
52
+ - lib/wor/configuration.rb
53
+ - lib/wor/helpers.rb
54
+ - lib/wor/localization.rb
52
55
  - log/test.log
53
56
  - spec/rails_app/Rakefile
54
57
  - spec/rails_app/config.ru
@@ -60,7 +63,8 @@ files:
60
63
  - spec/rails_app/config/environments/test.rb
61
64
  - spec/rails_app/config/locales/wor.en.yml
62
65
  - spec/rails_app/config/locales/wor.pt.yml
63
- - spec/rails_app/content/courses/curso-teste.md
66
+ - spec/rails_app/content/courses/test.md
67
+ - spec/rails_app/content/pages/courses.md
64
68
  - spec/rails_app/content/pages/index.md
65
69
  - spec/rails_app/log/test.log
66
70
  - spec/test_helper.rb