Module: Clean::Support

Defined in:
lib/clean/support.rb

Class Method Summary collapse

Class Method Details

.actions_hash(options = {}) ⇒ Object

Lists all actions of all controller by loading them and list action_methods


213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/clean/support.rb', line 213

def actions_hash(options = {})
  controllers = controllers_in_file
  if options[:except]
    options[:except] = [options[:except]] unless options[:except].is_a?(Array)
    controllers.reject! { |c| options[:except].any? { |e| c <= e } }
  end
  ref = HashWithIndifferentAccess.new
  controllers.each do |controller|
    ref[controller.controller_path] = controller.action_methods.to_a.sort.delete_if { |a| a.to_s.start_with?('_') }
  end
  ref
end

.browse(paths) ⇒ Object

Raises:

  • (ArgumentError)

186
187
188
189
190
191
192
193
# File 'lib/clean/support.rb', line 186

def browse(paths)
  raise ArgumentError, 'Missing block' unless block_given?
  paths.each do |path|
    Dir.glob(path).each do |file|
      yield file
    end
  end
end

.concepts_in_fileObject

Lists concepts paths


286
287
288
289
290
291
292
# File 'lib/clean/support.rb', line 286

def concepts_in_file
  dir = Rails.root.join('app', 'concepts')
  list = Dir.glob(dir.join('**', '*.rb')).collect do |h|
    Pathname.new(h).relative_path_from(dir).to_s[0..-4]
  end
  list
end

.controllers_in_fileObject

Lists all controller that inherits of ApplicationController included


238
239
240
241
242
243
244
245
246
247
# File 'lib/clean/support.rb', line 238

def controllers_in_file
  Dir.glob(Rails.root.join('app', 'controllers', '**', '*.rb')).each do |file|
    next if file.start_with? Rails.root.join('app', 'controllers', 'concerns').to_s
    require file
  end
  ObjectSpace
    .each_object(Class)
    .select { |klass| klass <= ::ApplicationController || klass <= ::ApiController }
    .sort_by(&:name)
end

.deep_symbolize_keys(hash) ⇒ Object


71
72
73
74
75
76
77
78
# File 'lib/clean/support.rb', line 71

def deep_symbolize_keys(hash)
  hash.each_with_object({}) do |(key, value), result|
    value = deep_symbolize_keys(value) if value.is_a? Hash
    key = key[2..-3] if key.to_s =~ /^__(yes|no|true|false)__$/
    result[key.to_sym] = value
    result
  end
end

.default_action_title(controller_path, action_name) ⇒ Object


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/clean/support.rb', line 195

def default_action_title(controller_path, action_name)
  action_name = action_name.to_sym unless action_name.is_a?(Symbol)
  controller_name = controller_path.split('/').last
  text = if action_name == :index
           controller_name.humanize
         elsif action_name == :show
           controller_name.humanize.singularize + ': %{name}'
         elsif [:new].include? action_name
           "#{action_name} #{controller_name.humanize.singularize}".humanize
         elsif %i[list import export].include? action_name
           "#{action_name} #{controller_name}".humanize
         else
           "#{action_name} #{controller_name.humanize.singularize}: %{name}".humanize
         end
  text
end

.exchangers_in_fileObject

Lists exchangers paths


268
269
270
271
272
273
274
# File 'lib/clean/support.rb', line 268

def exchangers_in_file
  dir = Rails.root.join('app', 'exchangers')
  list = Dir.glob(dir.join('**', '*.rb')).collect do |h|
    Pathname.new(h).relative_path_from(dir).to_s[0..-4]
  end
  list
end

.hash_count(hash) ⇒ Object


65
66
67
68
69
# File 'lib/clean/support.rb', line 65

def hash_count(hash)
  hash.reduce(0) do |count, (_key, value)|
    count += (value.is_a?(Hash) ? hash_count(value) : 1)
  end
end

.hash_diff(hash, ref, mode = nil, _depth = nil) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/clean/support.rb', line 96

def hash_diff(hash, ref, mode = nil, _depth = nil)
  hash ||= {}
  ref ||= {}
  keys = (ref.keys + hash.keys).uniq.sort_by { |a| a.to_s.tr('_', ' ').strip }
  count = 0
  total = 0
  code = keys.map do |key|
    h = hash[key]
    r = ref[key]
    if r.is_a?(Hash) && (h.is_a?(Hash) || h.nil?)
      scode, scount, stotal = hash_diff(h, r, mode)
      count += scount
      total += stotal
      key.to_s + ":\n" + scode.indent
    elsif r && h.nil?
      count += 1
      total += 1
      (key.to_s + ': ' + (mode == :humanize ? key.to_s.humanize : value_to_yaml(r))).gsub(/^/, missing_prompt)
    elsif r && h && r.class == h.class
      total += 1
      pair_to_yaml(key, h)
    elsif r && h && r.class != h.class
      total += 1
      pair_to_yaml(key, h).gsub(/$/, ' #?').sub(/$/, " #{r.class.name} expected (#{r.inspect})")
    elsif h && r.nil?
      pair_to_yaml(key, h).gsub(/$/, ' #?')
    elsif r.nil?
      key.to_s + ': #?'
    end
  end.join("\n")
  [code, count, total]
end

.hash_sort_and_count(hash) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/clean/support.rb', line 49

def hash_sort_and_count(hash)
  hash ||= {}
  count = 0
  code = hash.sort_by { |k, _| k.to_s }.map do |key, value|
    if value.is_a? Hash
      scode, scount = hash_sort_and_count(value)
      count += scount
      key.to_s + ":\n" + scode.indent
    else
      count += 1
      key.to_s + ': ' + value_to_yaml(value)
    end
  end.join("\n")
  [code, count]
end

.hash_to_yaml(hash, depth = nil) ⇒ Object


12
13
14
15
16
17
18
19
# File 'lib/clean/support.rb', line 12

def hash_to_yaml(hash, depth = nil)
  code = hash.sort_by { |a| a[0].to_s.tr('_', ' ').strip }.map do |k, v|
    next unless v
    pair_to_yaml(k, v)
  end.join("\n")
  code = "\n" + code.indent(depth).gsub(/^\s+$/, '') unless depth.nil?
  code
end

.helpers_in_fileObject

Lists helpers paths


250
251
252
253
254
255
256
# File 'lib/clean/support.rb', line 250

def helpers_in_file
  dir = Rails.root.join('app', 'helpers')
  list = Dir.glob(dir.join('**', '*.rb')).collect do |h|
    Pathname.new(h).relative_path_from(dir).to_s[0..-4]
  end
  list
end

.jobs_in_fileObject

Lists jobs paths


259
260
261
262
263
264
265
# File 'lib/clean/support.rb', line 259

def jobs_in_file
  dir = Rails.root.join('app', 'jobs')
  list = Dir.glob(dir.join('**', '*.rb')).collect do |h|
    Pathname.new(h).relative_path_from(dir).to_s[0..-4]
  end
  list
end

.look_for_labels(*paths) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/clean/support.rb', line 129

def look_for_labels(*paths)
  list = []
  browse(paths) do |file|
    source = File.read(file)
    source.gsub(/(\'[^\']+\'|\"[^\"]+\"|\:\w+)\.(tl|th)/) do |exp|
      exp.gsub!(/\.tl\z/, '')
      exp.gsub!(/\A\:/, '')
      exp = exp[1..-2] if exp =~ /\A\'.*\'\z/ || exp =~ /\A\".*\"\z/
      exp.gsub!(/\#\{[^\}]+\}/, '*')
      list << exp
    end
    source.gsub(/(\'labels\.[^\']+\'|\"labels\.[^\"]+\")\.t/) do |exp|
      exp.gsub!(/\.t\z/, '')
      exp = exp[1..-2] if exp =~ /\A\'.*\'\z/ || exp =~ /\A\".*\"\z/
      exp.gsub!(/\Alabels\./, '')
      exp.gsub!(/\#\{[^\}]+\}/, '*')
      list << exp
    end
    source.gsub(/(tl|field_set|cell|cobble|subheading)\s*\(?\s*(\:?\'[^\w+\.]+\'|\:?\"[^\"]+\"|\:\w+)[^\n\z]*(\n|\z)/) do |exp|
      keys = exp.split(/[\s\(\)\:\'\"\,]+/)
      key = keys[1].gsub(/\#\{[^\}]+\}/, '*')
      next if keys[2..-1].include?('title') || keys[2..-1].include?('label')
      list << key
    end
  end
  list += Ekylibre::Navigation.parts.collect { |p| p.index.keys }.flatten.compact.map(&:to_s)
  list += Ekylibre::Navigation.parts.map(&:name).map(&:to_s)

  # list += actions_hash.delete_if{|k,v| k == "backend/dashboards" }.values.flatten.uniq.delete_if{|a| a =~ /\Alist\_/ }
  list.delete_if { |l| l == '*' || l.underscore != l || l =~ /\n/ }.uniq.sort
end

.look_for_notifications(*paths) ⇒ Object


165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/clean/support.rb', line 165

def look_for_notifications(*paths)
  list = []
  browse(paths) do |file|
    source = File.read(file)
    source.gsub(/notify(_error|_warning|_success)?(_now)?(\(\s*|\s+)\:\w+/) do |exp|
      list << exp.split(/\:/)[1].to_sym
    end
    source.gsub(/\:\w+\.tn/) do |exp|
      list << exp[1..-4].to_sym
    end
  end
  list.sort
end

.look_for_rest_actionsObject


161
162
163
# File 'lib/clean/support.rb', line 161

def look_for_rest_actions
  actions_hash.delete_if { |k, _v| k == 'backend/dashboards' }.values.flatten.uniq.delete_if { |a| a.start_with?('list_') }
end

.missing_promptObject


4
5
6
# File 'lib/clean/support.rb', line 4

def missing_prompt
  '# '
end

.models_in_fileObject

Lists all models that inherits of ActiveRecord but are not system


227
228
229
230
231
232
233
234
235
# File 'lib/clean/support.rb', line 227

def models_in_file
  Dir.glob(Rails.root.join('app', 'models', '*.rb')).each { |file| require file }
  ObjectSpace
    .each_object(Class)
    .select { |klass| klass < ActiveRecord::Base }
    .select { |x| !x.name.start_with?('ActiveRecord::') && !x.abstract_class? && !x.name.start_with?('HABTM_') && !x.name.start_with?('Apartment::') }
    .uniq
    .sort_by(&:name)
end

.pair_to_yaml(k, v) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/clean/support.rb', line 36

def pair_to_yaml(k, v)
  if v.is_a?(Hash)
    k.to_s + ":\n" + hash_to_yaml(v).indent
  else
    k.to_s + ': ' + value_to_yaml(v)
  end
end

.services_in_fileObject

Lists services paths


277
278
279
280
281
282
283
# File 'lib/clean/support.rb', line 277

def services_in_file
  dir = Rails.root.join('app', 'services')
  list = Dir.glob(dir.join('**', '*.rb')).collect do |h|
    Pathname.new(h).relative_path_from(dir).to_s[0..-4]
  end
  list
end

.set_search_path!Object


8
9
10
# File 'lib/clean/support.rb', line 8

def set_search_path!
  Ekylibre::Tenant.reset_search_path!
end

.text_found?(exp, *paths) ⇒ Boolean

Returns:

  • (Boolean)

179
180
181
182
183
184
# File 'lib/clean/support.rb', line 179

def text_found?(exp, *paths)
  browse(paths) do |file|
    return true if File.read(file) =~ exp
  end
  false
end

.value_to_yaml(value) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/clean/support.rb', line 21

def value_to_yaml(value)
  if value.is_a?(Array)
    '[' + value.map { |x| value_to_yaml(x) }.join(', ') + ']'
  elsif value.is_a?(Symbol)
    ':' + value.to_s
  elsif value.is_a?(Hash)
    hash_to_yaml(value)
  elsif value.is_a?(Numeric)
    value.to_s
  else
    v = value.to_s.gsub("\u00A0", '\\_')
    value =~ /\n/ ? "|\n" + v.strip.indent : '"' + v + '"'
  end
end

.yaml_to_hash(filename) ⇒ Object


44
45
46
47
# File 'lib/clean/support.rb', line 44

def yaml_to_hash(filename)
  hash = YAML.safe_load(IO.read(filename).gsub(/^(\s*)(no|yes|false|true):(.*)$/, '\1__\2__:\3'))
  deep_symbolize_keys(hash)
end

.yaml_value(value, depth = 0) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/clean/support.rb', line 80

def yaml_value(value, depth = 0)
  if value.is_a?(Array)
    '[' + value.collect { |x| yaml_value(x) }.join(', ') + ']'
  elsif value.is_a?(Symbol)
    ':' + value.to_s
  elsif value.is_a?(Hash)
    hash_to_yaml(value, depth + 1)
  elsif value.is_a?(Numeric)
    value.to_s
  else
    # "'"+value.to_s.gsub("'", "''")+"'"
    v = value.to_s.gsub("\u00A0", '\\_')
    value =~ /\n/ ? "|\n" + v.strip.dig(depth) : '"' + v + '"'
  end
end