fedux_org-stdlib 0.6.22 → 0.6.23

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: 22f3a0abb3429c10be240dbdc0a0816c9daf4462
4
- data.tar.gz: 8395b4d29686b3cd163acad23457539204295d79
3
+ metadata.gz: b41b05bdb8bfd2b58cb9934905750b608a6fe1a2
4
+ data.tar.gz: 9268708b36967f1756ee5131d08e7bffbee32833
5
5
  SHA512:
6
- metadata.gz: e6449d40125412606f0facdb8b0ae8268b7a9b71de23a3c7ac5f43b4d01e51ac57deb9a1a9073baec6cf347d6c8a252f1ba3b3b83c41f020c66ed509e750659e
7
- data.tar.gz: e7be42e048adaa99fb20cc09b494d4c8ee85aa0e8cef35ccd7d0f2698980cda74e5547fdc727d0564fc7013525e3bde8db9c887f698c56a480205db07b939631
6
+ metadata.gz: fd30198edf16f42727e8cfdd7f5bb1abdf9904d9326366c899c913ac9b154b0fd9779fa4d9003fa305af4c7afbe14419441a65bb143d1fd0191dee8f2806c80c
7
+ data.tar.gz: 9e04281d393f6fd512335008b11c8514b1b51b315a2b04695bfc371a610888179cd79afeb7957433205ec0c74af03d83a60501577053928626e76720ebae7578
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fedux_org-stdlib (0.6.21)
4
+ fedux_org-stdlib (0.6.22)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -0,0 +1,193 @@
1
+ # encoding: utf-8
2
+ require 'fedux_org_stdlib/require_files'
3
+ require 'fedux_org_stdlib/file_template/exceptions'
4
+ require 'fedux_org_stdlib/core_ext/array'
5
+ require 'fedux_org_stdlib/logging/logger'
6
+ require_library %w{ json active_support/core_ext/string/inflections }
7
+
8
+ module FeduxOrgStdlib
9
+ # This class makes a template file available as an object. You can use
10
+ # whatever template language you prefer. It's up to you to compile the
11
+ # template with a suitable template parser.
12
+ #
13
+ # By default it will look for a suitable template file in the given order:
14
+ #
15
+ # 1. $HOME/.config/<application_name>/<template_file>.yaml
16
+ # 2. $HOME/.<application_name>/<template_file>.yaml
17
+ # 2. $HOME/.<template_file>.yaml
18
+ # 2. $HOME/.<template_file>rc
19
+ # 3. /etc/.<application_name>/<template_file>.yaml
20
+ #
21
+ # Please keep in mind
22
+ #
23
+ # * application_name: Module of your class, e.g. "MyApplication" becomes
24
+ # "my_application"
25
+ # * template_file: Singular name of your class and "Template" strip
26
+ # off, e.g "ClientTemplate" becomes "client.tt"
27
+ #
28
+ # Most conventions defined by me are implemented as separate methods. If one convention
29
+ # is not suitable for your use case, just overwrite the method.
30
+ #
31
+ # If you prefer to use a different path to the template file or name of the
32
+ # template file one of the following methods needs to be overwritten:
33
+ #
34
+ # * template_file
35
+ # * template_name
36
+ # * application_name
37
+ #
38
+ # If you want the class to look for your template file at a different place
39
+ # overwrite the following method
40
+ #
41
+ # * allowed_template_file_paths
42
+ #
43
+ # Below you find some examples for the usage of the class:
44
+ #
45
+ # @example Create template with one writer and reader
46
+ # module MyApplication
47
+ # class ClientTemplate < FileTemplate
48
+ # end
49
+ # end
50
+ #
51
+ # @example Template yaml file for the classes above: clients.yaml
52
+ # <%= hello %>
53
+ # ---
54
+ # option1: 'data2'
55
+ class FileTemplate
56
+ # Create a new instance of template
57
+ #
58
+ # It tries to find a suitable template file. If it doesn't find one
59
+ # the template is empty
60
+ #
61
+ # @param [String] file
62
+ # Path where template file is stored.
63
+ #
64
+ # @raise [Exceptions::TemplateFileNotReadable]
65
+ # If an avaiable template file could not be read by the template engine
66
+ #
67
+ # @return [AppTemplate]
68
+ # The template instance. If the resulting data structure created by the
69
+ # template_engine does not respond to `:[]` an empty template object will be
70
+ # created.
71
+ def initialize(
72
+ file: _available_template_file,
73
+ logger: FeduxOrgStdlib::Logging::Logger.new
74
+ )
75
+ @logger = logger
76
+
77
+ unless file
78
+ logger.debug "No template file found at #{_allowed_template_file_paths.to_list}, therefor I'm going to use an empty template object instead."
79
+ @__template = ''
80
+
81
+ return
82
+ end
83
+
84
+ begin
85
+ @__template = File.read(file).chomp
86
+ rescue StandardError => e
87
+ fail Exceptions::TemplateFileNotReadable, JSON.dump(message: e.message, file: file)
88
+ end
89
+ end
90
+
91
+ # Return the path to the preferred template file
92
+ # @return [String]
93
+ # The path to the preferred template file
94
+ def preferred_template_file
95
+ _allowed_template_file_paths.first
96
+ end
97
+
98
+ def content
99
+ @__template
100
+ end
101
+
102
+ private
103
+
104
+ # The name of the template file
105
+ #
106
+ # @return [String]
107
+ # The name of the template file. It defaults to `<template_name>.yaml`. If
108
+ # you want to use a different file name you need to overwrite this
109
+ # method.
110
+ def _template_file
111
+ "#{_template_name}#{_template_file_suffix}"
112
+ end
113
+
114
+ # The suffix of the template file
115
+ #
116
+ # @return [String]
117
+ # The suffix of the template file
118
+ def _template_file_suffix
119
+ '.tt'
120
+ end
121
+
122
+ # The base name of the template
123
+ #
124
+ # @return [String]
125
+ # This one returns the base name of the template file (without the file
126
+ # extension). It uses the class name of the template class
127
+ #
128
+ # @example Determine the base name of the template
129
+ #
130
+ # class ClientTemplate; end
131
+ #
132
+ # This will result in `client` as base name for the template file.
133
+ def _template_name
134
+ unless (name = _class_name.sub(/Template/, '').underscore.singularize).blank?
135
+ return name
136
+ end
137
+
138
+ fail Exceptions::ClassNameIsMissing, JSON.dump(klass: _class_name)
139
+ end
140
+
141
+ # The name of your application
142
+ #
143
+ # @return [String]
144
+ # This will strip of the class part of fully qualified class name and
145
+ # converted it to a path.
146
+ #
147
+ # @example Determine application name
148
+ #
149
+ # class MyApplication::MyTemplate; end
150
+ #
151
+ # This will be converted to
152
+ #
153
+ # my_application
154
+ def _application_name
155
+ _module_name.underscore
156
+ end
157
+
158
+ # The paths where to look for the template file
159
+ #
160
+ # @return [Array]
161
+ # A list of paths where the template object should look for its template
162
+ # file.
163
+ def _allowed_template_file_paths
164
+ paths = []
165
+ paths << ::File.expand_path(::File.join('~', '.config', _application_name, 'templates', _template_file))
166
+ paths << ::File.expand_path(::File.join('~', format('.%s', _application_name), 'templates', _template_file))
167
+ paths << ::File.expand_path(::File.join('/etc', _application_name, 'templates', _template_file))
168
+ paths << ::File.expand_path(_fallback_template_path) if _fallback_template_path
169
+
170
+ paths
171
+ end
172
+
173
+
174
+ # Use this path as fall back path
175
+ def _fallback_template_path; end
176
+
177
+ def _class_name
178
+ self.class.name.to_s.demodulize
179
+ end
180
+
181
+ def _module_name
182
+ self.class.to_s.deconstantize
183
+ end
184
+
185
+ def _available_template_file
186
+ _allowed_template_file_paths.find { |f| ::File.exists? f }
187
+ end
188
+
189
+ def self._reserved_key_words
190
+ (methods | instance_methods | private_methods | private_instance_methods ) - (Class.methods | Class.private_methods ) | [:to_s]
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ module FeduxOrgStdlib
3
+ class FileTemplate
4
+ module Exceptions
5
+ # Found config file is not readable
6
+ class TemplateFileNotReadable < StandardError; end
7
+
8
+ # No allowed config file could be found
9
+ class NoTemplateFileFound < StandardError; end
10
+
11
+ # If no module is given on class
12
+ class NamespaceIsMissing < StandardError; end
13
+
14
+ # If no class name is present
15
+ class ClassNameIsMissing < StandardError; end
16
+
17
+ # If one tries to define an option name which is forbbiden
18
+ class OptionNameForbidden < StandardError; end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  # FeduxOrgStdlib
3
3
  module FeduxOrgStdlib
4
- VERSION = '0.6.22'
4
+ VERSION = '0.6.23'
5
5
  end
@@ -0,0 +1,111 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'fedux_org_stdlib/file_template'
4
+
5
+ RSpec.describe FileTemplate do
6
+
7
+ context '#preferred_template_file' do
8
+ it 'has a default template file which is the preferred place to store the template' do
9
+ with_environment 'HOME' => working_directory do
10
+ template_klass = Class.new(FileTemplate) do
11
+
12
+ def _class_name
13
+ 'TestTemplate'
14
+ end
15
+
16
+ def _module_name
17
+ 'MyApplication'
18
+ end
19
+ end
20
+
21
+ template = template_klass.new
22
+ expect(template.preferred_template_file).to eq File.expand_path('~/.config/my_application/templates/test.tt')
23
+ end
24
+ end
25
+
26
+ it 'works with nested module names' do
27
+ with_environment 'HOME' => working_directory do
28
+ template_klass = Class.new(FileTemplate) do
29
+
30
+ def _class_name
31
+ 'TestTemplate'
32
+ end
33
+
34
+ def _module_name
35
+ 'MyApplication::MySub'
36
+ end
37
+ end
38
+
39
+ template = template_klass.new
40
+ expect(template.preferred_template_file).to eq File.expand_path('~/.config/my_application/my_sub/templates/test.tt')
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'template files' do
46
+ it 'looks at ~/.config/my_application/templates/test.tt' do
47
+ with_environment 'HOME' => working_directory do
48
+ create_file '.config/my_application/templates/test.tt', <<-EOS.strip_heredoc
49
+ <%= hello world %>
50
+ EOS
51
+
52
+ template_klass = Class.new(FileTemplate) do
53
+ def _class_name
54
+ 'TestTemplate'
55
+ end
56
+
57
+ def _module_name
58
+ 'MyApplication'
59
+ end
60
+ end
61
+
62
+ template = template_klass.new
63
+ expect(template.content).to include 'hello world'
64
+ end
65
+ end
66
+
67
+ it 'looks at ~/.my_application/templates/test.tt' do
68
+ with_environment 'HOME' => working_directory do
69
+ create_file '.my_application/templates/test.tt', <<-EOS.strip_heredoc
70
+ <%= hello world %>
71
+ EOS
72
+
73
+ template_klass = Class.new(FileTemplate) do
74
+ def _class_name
75
+ 'TestTemplate'
76
+ end
77
+
78
+ def _module_name
79
+ 'MyApplication'
80
+ end
81
+ end
82
+
83
+ template = template_klass.new
84
+ expect(template.content).to include 'hello world'
85
+ end
86
+ end
87
+ end
88
+
89
+ context '#content' do
90
+ it 'returns content of template file' do
91
+ with_environment 'HOME' => working_directory do
92
+ create_file '.my_application/templates/test.tt', <<-EOS.strip_heredoc
93
+ <%= hello world %>
94
+ EOS
95
+
96
+ template_klass = Class.new(FileTemplate) do
97
+ def _class_name
98
+ 'TestTemplate'
99
+ end
100
+
101
+ def _module_name
102
+ 'MyApplication'
103
+ end
104
+ end
105
+
106
+ template = template_klass.new
107
+ expect(template.content).to include 'hello world'
108
+ end
109
+ end
110
+ end
111
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fedux_org-stdlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.22
4
+ version: 0.6.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Meyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-28 00:00:00.000000000 Z
11
+ date: 2014-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -59,6 +59,8 @@ files:
59
59
  - lib/fedux_org_stdlib/core_ext/array.rb
60
60
  - lib/fedux_org_stdlib/core_ext/string.rb
61
61
  - lib/fedux_org_stdlib/environment.rb
62
+ - lib/fedux_org_stdlib/file_template.rb
63
+ - lib/fedux_org_stdlib/file_template/exceptions.rb
62
64
  - lib/fedux_org_stdlib/filesystem.rb
63
65
  - lib/fedux_org_stdlib/filesystem/exceptions.rb
64
66
  - lib/fedux_org_stdlib/logging.rb
@@ -164,6 +166,7 @@ files:
164
166
  - spec/support/reporting.rb
165
167
  - spec/support/rspec.rb
166
168
  - spec/support/string.rb
169
+ - spec/template_file_spec.rb
167
170
  - spec/version_management/library_builder_spec.rb
168
171
  - spec/version_management/ruby_library_spec.rb
169
172
  - spec/version_management/rubygem_version_file_parser_spec.rb
@@ -230,6 +233,7 @@ test_files:
230
233
  - spec/support/reporting.rb
231
234
  - spec/support/rspec.rb
232
235
  - spec/support/string.rb
236
+ - spec/template_file_spec.rb
233
237
  - spec/version_management/library_builder_spec.rb
234
238
  - spec/version_management/ruby_library_spec.rb
235
239
  - spec/version_management/rubygem_version_file_parser_spec.rb