Module: Alias

Extended by:
Alias
Included in:
Alias
Defined in:
lib/alias.rb,
lib/alias/util.rb,
lib/alias/console.rb,
lib/alias/creator.rb,
lib/alias/manager.rb,
lib/alias/version.rb,
lib/alias/validator.rb

Overview

Most of the core Alias actions are run through Alias::Manager except for Alias.create. See Alias::Manager for an explanation of how aliases are created.

Defined Under Namespace

Modules: Console, Util Classes: Creator, Creators, Manager, Validator

Constant Summary collapse

VERSION =
'0.2.3'

Instance Method Summary collapse

Instance Method Details

#add_to_config_file(new_aliases, file) ⇒ Object

:stopdoc:



55
56
57
58
59
60
61
# File 'lib/alias.rb', line 55

def add_to_config_file(new_aliases, file)
  file ||= File.directory?('config') ? 'config/alias.yml' : "#{ENV['HOME']}/.alias.yml"
  existing_aliases = read_config_file(file)
  existing_aliases[:aliases] = Util.recursive_hash_merge existing_aliases[:aliases], new_aliases
  save_to_file file, existing_aliases.to_yaml
  puts "Saved created aliases to #{file}."
end

#configObject



80
81
82
# File 'lib/alias.rb', line 80

def config
  @config ||= {}
end

#config_fileObject

By default, looks for existing files in config/alias.yml and then ~/.alias.yml. A config file has the following keys:

:aliases

This takes a hash mapping creators to their config hashes. Valid creators are :instance_method, :class_method, :constant, :class_to_instance_method and :any_to_instance_method.

:verbose

Sets whether creators are verbose with boolean or array of creator symbols. A boolean sets verbosity for all creators whereas the array specifies which creators. Default is false.

:force

Sets whether creators force optional validations with boolean or array of creator symbols. Works the same as :verbose. Default is false.



45
46
47
# File 'lib/alias.rb', line 45

def config_file
  @config_file ||= File.exists?("config/alias.yml") ? 'config/alias.yml' : "#{ENV['HOME']}/.alias.yml"
end

#create(options = {}) ⇒ Object

Creates aliases from Alias.config_file if it exists and merges them with any explicit aliases. This method takes the same keys used by config files (see Alias.config_file) and also the following options:

  • :file : Specifies a config file to override Alias.config_file. If set to false, no config file is loaded.

Examples:

# Loads any default files and the ones in :aliases. 
# Sets global verbosity for creators.
create :aliases=>{:constant=>{"Array"=>"A"}}, :verbose=>true
# Loads the given file and sets verbosity just for the :instance_method creator.
create :file=>"some file", :verbose=>[:instance_method]


28
29
30
31
32
33
34
35
36
37
# File 'lib/alias.rb', line 28

def create(options={})
  file_config = load_config_file(options.delete(:file))
  new_config = Util.recursive_hash_merge(file_config, options)
  manager.verbose = new_config[:verbose] if new_config[:verbose]
  manager.force = new_config[:force] if new_config[:force]
  (new_config[:aliases] || {}).each do |creator_type, aliases|
    manager.create_aliases(creator_type, aliases)
  end
  @config = Util.recursive_hash_merge(config, new_config)
end

#load_config_file(file) ⇒ Object



74
75
76
77
78
# File 'lib/alias.rb', line 74

def load_config_file(file)
  return {} if file == false
  @config_file = file if file
  read_config_file(config_file)
end

#managerObject

Contains primary Alias::Manager object which is used throughout Alias.



50
51
52
# File 'lib/alias.rb', line 50

def manager
  @manager ||= Manager.new
end

#read_config_file(file) ⇒ Object



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

def read_config_file(file)
  file_config = File.exists?(File.expand_path(file)) ? YAML::load_file(File.expand_path(file)) : {}
  file_config = Util.symbolize_keys file_config
  file_config[:aliases] = file_config[:aliases] ? Util.symbolize_keys(file_config.delete(:aliases)) : {}
  file_config
end

#save_to_file(file, string) ⇒ Object



63
64
65
# File 'lib/alias.rb', line 63

def save_to_file(file, string)
  File.open(File.expand_path(file), 'w') {|f| f.write string }
end