Module: ModuleHelper

Defined in:
lib/module_helper.rb

Constant Summary collapse

CLIENT_METHODS_FOLDER =
'mapper_methods'

Class Method Summary collapse

Class Method Details

.deep_transform_values_with_path(object, path = [], &block) ⇒ Object

Example of usage:

my_hash = 0, b: {c: 1, d: 2, e: {f: 3}} values = %w(mother washed the ceiling)

result = deep_transform_values_with_path(my_hash) do |value, path|

path.join('/') + '/' + values[value]

end

:b=>{:c=>“b/c/washed”, :d=>“b/d/the”, :e=>{:f=>“b/e/f/ceiling”}}



45
46
47
48
49
50
51
52
53
54
# File 'lib/module_helper.rb', line 45

def self.deep_transform_values_with_path(object, path=[], &block)
  case object
  when Hash
    object.map { |k, v| [k, deep_transform_values_with_path(v, path + [k], &block)] }.to_h
  when Array
    object.map { |e| deep_transform_values_with_path(e, path, &block) }
  else
    yield(object, path) unless object.is_a?(Hash)
  end
end

.needs_method?(signature) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/module_helper.rb', line 30

def self.needs_method?(signature)
  !!(signature =~ /\(.*\)/)
end

.parse_signature(signature, client_name = nil, table_name = nil, field_name = nil) ⇒ Object

‘shared/phone/number/normalize(phonenumber)’ -> [‘shared’, ‘phone’, ‘number’], ‘normalize’, [‘phonenumber’]

‘(status)’, client_name: ‘reliable’, field_name: ‘active’ -> [‘reliable’, ‘mapper_methods’], ‘active’, [‘status’]



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/module_helper.rb', line 8

def self.parse_signature(signature, client_name=nil, table_name=nil, field_name=nil)
  match = signature.match(/^(.*)\((.*)\)/)

  result = {arguments: match[2].gsub(/,\s+/, ',').split(',')}

  if shared_method?(signature)
    path = match[1].split('/')

    result[:method_name] = path.pop.to_sym
    result[:path] = path
  else
    result[:method_name] = field_name
    result[:path] = [client_name, CLIENT_METHODS_FOLDER, table_name]
  end

  result
end

.shared_method?(signature) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/module_helper.rb', line 26

def self.shared_method?(signature)
  !!(signature =~ /^shared\//)
end