Module: Shaf::Utils
Defined Under Namespace
Classes: ProjectRootNotFound
Constant Summary
collapse
- SHAF_VERSION_FILE =
'.shaf'.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.deep_symbolize_keys(value) ⇒ Object
67
68
69
|
# File 'lib/shaf/utils.rb', line 67
def deep_symbolize_keys(value)
deep_transform_keys(value) { |key| key.to_sym }
end
|
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/shaf/utils.rb', line 71
def deep_transform_keys(hash, &block)
case hash
when Hash
hash.each_with_object({}) do |(k, v), h|
key = block.call(k)
h[key] = deep_transform_keys(v, &block)
end
when Array
hash.map { |v| deep_transform_keys(v, &block) }
else
hash
end
end
|
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/shaf/utils.rb', line 85
def deep_transform_values(hash, &block)
case hash
when Hash
hash.each_with_object({}) do |(k, v), h|
h[k] = deep_transform_values(v, &block)
end
when Array
hash.map { |v| deep_transform_values(v, &block) }
else
block.call(hash)
end
end
|
.environment ⇒ Object
55
56
57
58
|
# File 'lib/shaf/utils.rb', line 55
def environment
require 'sinatra/base'
Sinatra::Application.settings.environment
end
|
.gem_root ⇒ Object
23
24
25
|
# File 'lib/shaf/utils.rb', line 23
def gem_root
File.expand_path('../..', __dir__)
end
|
.iana_link_relations ⇒ Object
110
111
112
113
|
# File 'lib/shaf/utils.rb', line 110
def iana_link_relations
zip_file = File.join(gem_root, 'iana_link_relations.csv.gz')
Zlib::GzipReader.open(zip_file) { |content| content.read }
end
|
.model_name(name) ⇒ Object
19
20
21
|
# File 'lib/shaf/utils.rb', line 19
def model_name(name)
name.capitalize.gsub(/[_-](\w)/) { $1.upcase }
end
|
.pluralize(noun) ⇒ Object
28
29
30
31
|
# File 'lib/shaf/utils.rb', line 28
def pluralize(noun)
return pluralize(noun.to_s).to_sym if noun.is_a? Symbol
noun + 's'
end
|
60
61
62
63
64
65
|
# File 'lib/shaf/utils.rb', line 60
def (str)
return if str.nil?
str.upcase.tr('-', '_').tap do |key|
key.prepend('HTTP_') unless key.start_with? 'HTTP_'
end
end
|
.read_config(file, erb: false, erb_binding: nil) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/shaf/utils.rb', line 98
def read_config(file, erb: false, erb_binding: nil)
return {} unless File.exist? file
yaml = File.read(file)
yaml = erb(yaml, binding: erb_binding) if erb || erb_binding
if RUBY_VERSION < '2.6.0'
deep_symbolize_keys(YAML.safe_load(yaml, [], [], true))
else
YAML.safe_load(yaml, aliases: true, symbolize_names: true)
end
end
|
.singularize(noun) ⇒ Object
34
35
36
37
38
|
# File 'lib/shaf/utils.rb', line 34
def singularize(noun)
return singularize(noun.to_s).to_sym if noun.is_a? Symbol
return noun unless noun.end_with? 's'
noun[0..-2]
end
|
.symbol_or_quoted_string(obj) ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/shaf/utils.rb', line 44
def symbol_or_quoted_string(obj)
case obj
when Symbol
obj.inspect
when Numeric
obj
else
"'#{obj.to_s}'"
end
end
|
.symbol_string(str) ⇒ Object
40
41
42
|
# File 'lib/shaf/utils.rb', line 40
def symbol_string(str)
str.to_sym.inspect
end
|
Instance Method Details
#bootstrap(env: 'development') ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/shaf/utils.rb', line 155
def bootstrap(env: 'development')
in_project_root do
ENV['RACK_ENV'] = env
require 'config/bootstrap'
yield if block_given?
end
end
|
#in_project_root ⇒ Object
147
148
149
150
151
152
153
|
# File 'lib/shaf/utils.rb', line 147
def in_project_root
return unless block_given?
Dir.chdir(project_root!) do
$:.unshift Dir.pwd
yield
end
end
|
#in_project_root? ⇒ Boolean
143
144
145
|
# File 'lib/shaf/utils.rb', line 143
def in_project_root?
is_project_root?(Dir.pwd)
end
|
#is_project_root?(dir) ⇒ Boolean
139
140
141
|
# File 'lib/shaf/utils.rb', line 139
def is_project_root?(dir)
File.exist? File.expand_path(".shaf", dir)
end
|
#project_root ⇒ Object
126
127
128
129
130
131
132
133
|
# File 'lib/shaf/utils.rb', line 126
def project_root
return @project_root if defined? @project_root
dir = Dir.pwd
while dir != '/' do
return @project_root = dir if is_project_root?(dir)
dir = File.expand_path("..", dir)
end
end
|
#project_root! ⇒ Object
135
136
137
|
# File 'lib/shaf/utils.rb', line 135
def project_root!
project_root or raise ProjectRootNotFound
end
|
#read_shaf_file ⇒ Object
163
164
165
166
167
|
# File 'lib/shaf/utils.rb', line 163
def read_shaf_file
return {} unless File.exist? SHAF_VERSION_FILE
str = File.read(SHAF_VERSION_FILE)
YAML.load(str) || {}
end
|
#read_shaf_file! ⇒ Object
169
170
171
172
173
|
# File 'lib/shaf/utils.rb', line 169
def read_shaf_file!
in_project_root do
read_shaf_file
end
end
|
#read_shaf_upgrade_failure_version ⇒ Object
188
189
190
|
# File 'lib/shaf/utils.rb', line 188
def read_shaf_upgrade_failure_version
read_shaf_file['failed_upgrade']
end
|
#read_shaf_version ⇒ Object
184
185
186
|
# File 'lib/shaf/utils.rb', line 184
def read_shaf_version
read_shaf_file['version']
end
|
#write_shaf_file(data = {}) ⇒ Object
175
176
177
|
# File 'lib/shaf/utils.rb', line 175
def write_shaf_file(data = {})
write_shaf_file! read_shaf_file.merge(data)
end
|
#write_shaf_file!(data = {}) ⇒ Object
179
180
181
182
|
# File 'lib/shaf/utils.rb', line 179
def write_shaf_file!(data = {})
File.write SHAF_VERSION_FILE,
YAML.dump(data)
end
|
#write_shaf_upgrade_failure(version) ⇒ Object
199
200
201
|
# File 'lib/shaf/utils.rb', line 199
def write_shaf_upgrade_failure(version)
write_shaf_file 'failed_upgrade' => version.to_s
end
|
#write_shaf_version(version = nil) ⇒ Object
192
193
194
195
196
197
|
# File 'lib/shaf/utils.rb', line 192
def write_shaf_version(version = nil)
version ||= Shaf::VERSION
data = read_shaf_file.merge('version' => version.to_s)
data.delete('failed_upgrade')
write_shaf_file! data
end
|