Class: Bp3::String::TableControllerMap

Inherits:
MapperBase
  • Object
show all
Defined in:
lib/bp3/string/table_controller_map.rb

Overview

TableControllerMap provides for mappings between table-like strings and controller class names

Constant Summary collapse

CACHED_HASH_MUTEX =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MapperBase

build_hash, hash, #hash, #sti_subclass?, #subclassed?, testing?

Instance Attribute Details

#ac_hashObject (readonly, private)

Returns the value of attribute ac_hash.



36
37
38
# File 'lib/bp3/string/table_controller_map.rb', line 36

def ac_hash
  @ac_hash
end

#ar_hashObject (readonly, private)

Returns the value of attribute ar_hash.



36
37
38
# File 'lib/bp3/string/table_controller_map.rb', line 36

def ar_hash
  @ar_hash
end

#encodingObject (readonly, private)

Returns the value of attribute encoding.



36
37
38
# File 'lib/bp3/string/table_controller_map.rb', line 36

def encoding
  @encoding
end

Class Method Details

.cached_hashObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/bp3/string/table_controller_map.rb', line 10

def self.cached_hash
  return build_hash if testing?
  return @cached_hash if @cached_hash.present?

  CACHED_HASH_MUTEX.synchronize do
    return @cached_hash if @cached_hash.present?

    @cached_hash = build_hash
  end
end

.reset_cached_hashObject



21
22
23
24
25
# File 'lib/bp3/string/table_controller_map.rb', line 21

def self.reset_cached_hash
  CACHED_HASH_MUTEX.synchronize do
    @cached_hash = nil
  end
end

Instance Method Details

#build_hashObject



27
28
29
30
31
32
# File 'lib/bp3/string/table_controller_map.rb', line 27

def build_hash
  @encoding = 'string'.encoding # TODO: not sure why encoding sometimes doesn't match
  @ar_hash = build_hash_from_active_record
  @ac_hash = build_hash_from_action_controller
  @ar_hash.merge(@ac_hash)
end

#build_hash_from_action_controllerObject (private)



52
53
54
55
56
57
58
# File 'lib/bp3/string/table_controller_map.rb', line 52

def build_hash_from_action_controller
  hash = {}
  ActionController::Base.descendants.each do |controller|
    hash.merge!(build_hash_from_controller(controller))
  end
  hash
end

#build_hash_from_active_recordObject (private)



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bp3/string/table_controller_map.rb', line 38

def build_hash_from_active_record
  hash = {}
  ActiveRecord::Base.descendants.each do |model|
    table_name = model.table_name
    next if table_name.blank?

    table_name = model.name.tableize.tr('/', '_') if sti_subclass?(model)
    controller_name = "#{model.name.pluralize}Controller"
    hash[table_name] = controller_name
    hash[table_name.singularize] = controller_name
  end
  hash
end

#build_hash_from_controller(controller) ⇒ Object (private)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bp3/string/table_controller_map.rb', line 60

def build_hash_from_controller(controller)
  hash = {}
  if controller.descendants.empty?
    if controller.name.present?
      controller_name = controller.name.encode(encoding)
      tableish_name = controller_name.gsub(/Controller\z/, '').underscore.tr('/', '_').pluralize
      hash[tableish_name] = controller_name unless @ar_hash.key?(tableish_name)
      hash[tableish_name.singularize] = controller_name unless @ar_hash.key?(tableish_name.singularize)
    end
  else
    controller.descendants.each do |ctrlr|
      hash.merge!(build_hash_from_controller(ctrlr))
    end
  end
  hash
end