Class: Alias::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/alias/manager.rb

Overview

This class manages creation, searching and saving of aliases. Aliases are created as follows:

  • Alias hashes are read in by Alias.create and/or input through the console via Alias::Console.create_aliases.

  • These alias hashes are passed to Alias::Manager.create_aliases. An Alias::Manager object passes each alias hash to to the correct Alias::Creator subclass by interpreting the creator type.

  • Each creator converts their alias hash to an alias hash array and runs them through their defined validators, Alias::Validator objects.

  • The aliases that meet the validation conditions are then created using Kernel.eval.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

:nodoc:



10
11
12
13
14
# File 'lib/alias/manager.rb', line 10

def initialize #:nodoc:
  @creators = {}
  @verbose = false
  @force = false
end

Instance Attribute Details

#created_aliasesObject (readonly)

A hash of created aliases by creator type.



23
24
25
# File 'lib/alias/manager.rb', line 23

def created_aliases
  @created_aliases
end

#creatorsObject (readonly)

A hash of creator objects that have been used by creator type.



21
22
23
# File 'lib/alias/manager.rb', line 21

def creators
  @creators
end

#forceObject

When true, optional validations will be skipped. Takes an array of creator type symbols or a boolean to set all creators.



19
20
21
# File 'lib/alias/manager.rb', line 19

def force
  @force
end

#verboseObject

When true, all failed validations print their message. Takes an array of creator type symbols or a boolean to set all creators.



17
18
19
# File 'lib/alias/manager.rb', line 17

def verbose
  @verbose
end

Instance Method Details

#aliases_of(creator_type) ⇒ Object



107
108
109
# File 'lib/alias/manager.rb', line 107

def aliases_of(creator_type)
  @creators[creator_type] && @creators[creator_type].aliases
end

#all_aliasesObject

Returns an array of all created alias hashes. The alias hash will have a :type key which contains the creator type it belongs to.



96
97
98
99
100
# File 'lib/alias/manager.rb', line 96

def all_aliases
  @all_aliases ||= @creators.inject([]) do |t, (type, creator)|
    t += creator.aliases.each {|e| e[:type] = type.to_s}
  end
end

#all_creator_typesObject

:stopdoc:



103
104
105
# File 'lib/alias/manager.rb', line 103

def all_creator_types
  Creator.creators.map {|e| Util.underscore(e.to_s[/::(\w+)Creator$/,1]) }
end

#console_create_aliases(creator_type, aliases_hash, options = {}) ⇒ Object

Creates aliases in the same way as create_aliases while keeping track of what’s created. But differs in that creator types can be accessed with just the first few unique letters of a type. For example, you can pass :in to mean :instance_method. Also, the verbose flag is set by default. Examples:

console_create_aliases :in, "String"=>{"to_s"=>"s"}
console_create_aliases :con, {"ActiveRecord::Base"=>"AB"}, :pretend=>true


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/alias/manager.rb', line 50

def console_create_aliases(creator_type, aliases_hash, options={})
  options = {:verbose=>true}.update(options)
  @created_aliases ||= {}
  creator_type = (all_creator_types.sort.find {|e| e[/^#{creator_type}/] } || creator_type).to_sym
  if create_aliases(creator_type, aliases_hash, options)
    @created_aliases[creator_type] = aliases_hash
    true
  else
    false
  end
end

#create_aliases(creator_type, aliases_hash, options = {}) ⇒ Object

The main method for creating aliases. Takes a creator type, a hash of aliases whose format is defined per creator and the following options:

:verbose

Sets the verbose flag to print a message whenever an alias validation fails. Default is the creator’s verbose flag.

:force

Sets the force flag to bypass optional validations. Default is the creator’s manager flag.

:pretend

Instead of creating aliases, prints out the ruby code that would be evaluated by Kernel.eval to create the aliases. Default is false.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/alias/manager.rb', line 32

def create_aliases(creator_type, aliases_hash, options={})
  return unless (creator = create_creator(creator_type))
  creator.verbose = options[:verbose] ? options[:verbose] : verbose_creator?(creator_type)
  creator.force = options[:force] ? options[:force] : force_creator?(creator_type)
  creator.create(aliases_hash.dup, options[:pretend] || false)
  true
rescue Creator::AbstractMethodError
  $stderr.puts $!.message
rescue Creator::FailedAliasCreationError
  $stderr.puts "'#{creator.class}' failed to create aliases with error:\n#{$!.message}"
end

#create_creator(creator_type) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/alias/manager.rb', line 119

def create_creator(creator_type)
  creator_class_string = "Alias::Creators::#{Util.camelize(creator_type.to_s)}Creator"
  if creator_class = Util.any_const_get(creator_class_string)
    @creators[creator_type.to_sym] ||= creator_class.new
  else
    $stderr.puts "Creator class '#{creator_class_string}' not found."
    nil
  end
end

#force_creator?(creator_type) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/alias/manager.rb', line 115

def force_creator?(creator_type)
  @force.is_a?(Array) ? @force.include?(creator_type.to_sym) : @force
end

#intersection_of_two_arrays(arr1, arr2) ⇒ Object



133
134
135
# File 'lib/alias/manager.rb', line 133

def intersection_of_two_arrays(arr1, arr2)
  arr2.nil? ? arr1 : arr1.select {|e| arr2.include?(e)}
end

#reset_all_aliasesObject



137
# File 'lib/alias/manager.rb', line 137

def reset_all_aliases; @all_aliases = nil; end

#save_aliases(file = nil) ⇒ Object

Saves aliases that were created by console_create_aliases. Can take an optional file to save to. See Alias::Console.save_aliases for default files this method saves to.



64
65
66
67
68
69
70
71
72
# File 'lib/alias/manager.rb', line 64

def save_aliases(file=nil)
  if @created_aliases
    Alias.add_to_config_file(@created_aliases, file)
    true
  else
    puts "Didn't save. No created aliases detected."
    false
  end
end

#search(search_hash) ⇒ Object

Searches all created alias hashes with a hash or a string. If a string, the alias key searched is :name. If a hash, the key should should be an alias key and the value the search term. All values are treated as regular expressions. Alias keys vary per creator but some of the common ones are :name, :class and :alias. Multiple keys for a hash will AND the searches. Examples:

search 'to_'
search :class=>"Array", :name=>'to'


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/alias/manager.rb', line 81

def search(search_hash)
  result = nil
  reset_all_aliases
  search_hash = {:name=>search_hash} unless search_hash.is_a?(Hash)
  search_hash.each do |k,v|
    new_result = simple_search(k,v)
    #AND's searches
    result = intersection_of_two_arrays(new_result, result)
  end
  #duplicate results in case they are modified
  result = result.map {|e| e.dup} if result
  result
end

#simple_search(field, search_term) ⇒ Object



129
130
131
# File 'lib/alias/manager.rb', line 129

def simple_search(field, search_term)
  all_aliases.select {|e| e[field] =~ /#{search_term}/ }
end

#verbose_creator?(creator_type) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/alias/manager.rb', line 111

def verbose_creator?(creator_type)
  @verbose.is_a?(Array) ? @verbose.include?(creator_type.to_sym) : @verbose
end