Class: Bp3::String::TableControllerMap
- Inherits:
-
MapperBase
- Object
- MapperBase
- Bp3::String::TableControllerMap
- 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
-
#ac_hash ⇒ Object
readonly
private
Returns the value of attribute ac_hash.
-
#ar_hash ⇒ Object
readonly
private
Returns the value of attribute ar_hash.
-
#encoding ⇒ Object
readonly
private
Returns the value of attribute encoding.
Class Method Summary collapse
Instance Method Summary collapse
- #build_hash ⇒ Object
- #build_hash_from_action_controller ⇒ Object private
- #build_hash_from_active_record ⇒ Object private
- #build_hash_from_controller(controller) ⇒ Object private
Methods inherited from MapperBase
build_hash, #hash, hash, reset_cached_hash, #sti_subclass?, #subclassed?, testing?
Instance Attribute Details
#ac_hash ⇒ Object (readonly, private)
Returns the value of attribute ac_hash.
30 31 32 |
# File 'lib/bp3/string/table_controller_map.rb', line 30 def ac_hash @ac_hash end |
#ar_hash ⇒ Object (readonly, private)
Returns the value of attribute ar_hash.
30 31 32 |
# File 'lib/bp3/string/table_controller_map.rb', line 30 def ar_hash @ar_hash end |
#encoding ⇒ Object (readonly, private)
Returns the value of attribute encoding.
30 31 32 |
# File 'lib/bp3/string/table_controller_map.rb', line 30 def encoding @encoding end |
Class Method Details
.cached_hash ⇒ Object
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 |
Instance Method Details
#build_hash ⇒ Object
21 22 23 24 25 26 |
# File 'lib/bp3/string/table_controller_map.rb', line 21 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_controller ⇒ Object (private)
46 47 48 49 50 51 52 |
# File 'lib/bp3/string/table_controller_map.rb', line 46 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_record ⇒ Object (private)
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bp3/string/table_controller_map.rb', line 32 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)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bp3/string/table_controller_map.rb', line 54 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 |