Module: Abbreviations::ClassMethods
- Defined in:
- lib/arspy/operators/abbreviations.rb
Instance Method Summary collapse
- #abbreviate_method_name(method_name) ⇒ Object
- #quote_and_join(array) ⇒ Object
- #remove_ambiguities(descriptors) ⇒ Object
- #resolve_abbreviation_for_attributes_and_associations!(object, method_name) ⇒ Object
- #setup_abbreviations(object) ⇒ Object
Instance Method Details
#abbreviate_method_name(method_name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/arspy/operators/abbreviations.rb', line 46 def abbreviate_method_name(method_name) words = method_name.to_s.split('_') abbr=[] if words.first == '' abbr << '_' end words.reject!{|word| word == ''} abbr += words.map do |word| chars = word.split(//) first = chars.shift [first, chars.map{|ch| ch =~ /[0-9]/ ? ch : nil}].compact.flatten.join('') end abbr << '_' if (method_name.to_s =~ /_$/) abbr.join('').to_sym end |
#quote_and_join(array) ⇒ Object
62 63 64 65 66 |
# File 'lib/arspy/operators/abbreviations.rb', line 62 def quote_and_join(array) return "'#{array.first}'" if array.size == 1 last = array.pop "'#{array.join("', '")}' or '#{last}'" end |
#remove_ambiguities(descriptors) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/arspy/operators/abbreviations.rb', line 28 def remove_ambiguities(descriptors) list={} ambiguities = {} descriptors.each do |desc| if list.include?(desc[:abbr]) if ambiguities[desc[:abbr]] ambiguities[desc[:abbr]][:methods] << desc[:method_name] else ambiguities[desc[:abbr]] = {:abbr=>desc[:abbr], :methods=>[desc[:method_name]]} ambiguities[desc[:abbr]][:methods] << list[desc[:abbr]][:method_name] end else list[desc[:abbr]] = desc end end descriptors.reject!{|desc| ambiguities.map{|hash| hash.first}.include?(desc[:abbr])} ambiguities end |
#resolve_abbreviation_for_attributes_and_associations!(object, method_name) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/arspy/operators/abbreviations.rb', line 10 def resolve_abbreviation_for_attributes_and_associations!(object, method_name) klass = object.class setup_abbreviations(object) unless object.instance_variable_defined?('@arspy_abbreviations') if (ambiguity = klass.instance_variable_get('@arspy_ambiguous_abbreviations')[method_name]) raise "Ambiguous abbreviation '#{ambiguity[:abbr]}' could be #{quote_and_join(ambiguity[:methods])}" end klass.instance_variable_get('@arspy_abbreviations')[method_name] end |
#setup_abbreviations(object) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/arspy/operators/abbreviations.rb', line 19 def setup_abbreviations(object) associations = object.class.reflect_on_all_associations.map(&:name).map(&:to_sym) attributes = object.attribute_names.map(&:to_sym) assoc_descriptors = associations.map{|method_name| {:method_name=>method_name, :type=>:association, :abbr=>abbreviate_method_name(method_name)}} attrib_descriptors = attributes.map{|method_name| {:method_name=>method_name, :type=>:attribute, :abbr=>abbreviate_method_name(method_name)}} all_descriptors = assoc_descriptors + attrib_descriptors object.class.instance_variable_set('@arspy_ambiguous_abbreviations', remove_ambiguities(all_descriptors)) object.class.instance_variable_set('@arspy_abbreviations', Hash[*all_descriptors.map{|desc| [desc[:abbr], desc] }.flatten]) end |