Class: Alias::Creator
- Inherits:
-
Object
- Object
- Alias::Creator
- Defined in:
- lib/alias/creator.rb
Overview
This is the base creator class. To be a valid subclass, the creator must define Alias::Creator.map and Alias::Creator.generate. Although not required, creators should enforce validation of their aliases with Alias::Creator.valid. Also, the creator should be named in the format Alias::Creators::*Creator where the asterisk stands for any unique string. Since that string is converted to an underscored version when referenced in the console, it’s recommended to make it camel case. For example, Alias::Creators::InstanceMethodCreator is referenced by it’s underscored version :instance_method.
To better understand how a creator works, here’s the steps a creator goes through when creating aliases:
-
map() : Maps the hash from a config file or console input into an array of alias hashes.
-
valid() : Defines a validation that each alias hash must pass.
-
generate() : Given the array of alias hashes, generates the string of ruby code to be evaled for alias creation.
Direct Known Subclasses
Alias::Creators::AnyToInstanceMethodCreator, Alias::Creators::ClassMethodCreator, Alias::Creators::ClassToInstanceMethodCreator, Alias::Creators::ConstantCreator, Alias::Creators::InstanceMethodCreator
Defined Under Namespace
Classes: AbstractMethodError, FailedAliasCreationError
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Array of alias hashes that have been created.
-
#force ⇒ Object
Same purpose as Alias::Manager.verbose and Alias::Manager.force but unlike them these only take a boolean.
-
#verbose ⇒ Object
Same purpose as Alias::Manager.verbose and Alias::Manager.force but unlike them these only take a boolean.
Class Method Summary collapse
-
.class_or_module(klass) ⇒ Object
:nodoc:.
-
.creators ⇒ Object
Array of all Creator subclasses.
-
.generate(&block) ⇒ Object
Takes a block which converts aliases to a string of ruby code to run through Kernel#eval.
-
.generates_aliases(aliases) ⇒ Object
:nodoc:.
-
.inherited(subclass) ⇒ Object
:nodoc:.
-
.map(&block) ⇒ Object
Takes a block which converts the creator’s config to an array of aliases.
-
.maps_config(config) ⇒ Object
:nodoc:.
-
.valid(key, options = {}) ⇒ Object
Creates a validation expectation for the creator by giving it an Alias::Validator object aka validator.
-
.validators ⇒ Object
Stores validators per alias attribute/key.
Instance Method Summary collapse
-
#create(aliases_hash, pretend = false) ⇒ Object
Main method used to create aliases.
-
#delete_invalid_aliases(arr) ⇒ Object
Deletes invalid alias hashes that fail defined validators for a creator.
-
#initialize(options = {}) ⇒ Creator
constructor
:nodoc:.
Constructor Details
#initialize(options = {}) ⇒ Creator
:nodoc:
83 84 85 86 87 |
# File 'lib/alias/creator.rb', line 83 def initialize(={}) #:nodoc: @verbose = false @force = false @aliases = [] end |
Instance Attribute Details
#aliases ⇒ Object
Array of alias hashes that have been created.
81 82 83 |
# File 'lib/alias/creator.rb', line 81 def aliases @aliases end |
#force ⇒ Object
Same purpose as Alias::Manager.verbose and Alias::Manager.force but unlike them these only take a boolean.
79 80 81 |
# File 'lib/alias/creator.rb', line 79 def force @force end |
#verbose ⇒ Object
Same purpose as Alias::Manager.verbose and Alias::Manager.force but unlike them these only take a boolean.
79 80 81 |
# File 'lib/alias/creator.rb', line 79 def verbose @verbose end |
Class Method Details
.class_or_module(klass) ⇒ Object
:nodoc:
65 66 67 |
# File 'lib/alias/creator.rb', line 65 def class_or_module(klass) #:nodoc: Util.any_const_get(klass).is_a?(Class) ? 'class' : 'module' end |
.creators ⇒ Object
Array of all Creator subclasses.
75 |
# File 'lib/alias/creator.rb', line 75 def creators; @creators; end |
.generate(&block) ⇒ Object
Takes a block which converts aliases to a string of ruby code to run through Kernel#eval.
61 62 63 |
# File 'lib/alias/creator.rb', line 61 def generate(&block) @generate = block end |
.generates_aliases(aliases) ⇒ Object
:nodoc:
56 57 58 |
# File 'lib/alias/creator.rb', line 56 def generates_aliases(aliases) #:nodoc: @generate ? @generate.call(aliases) : raise(AbstractMethodError, "No generate() defined for #{self}") end |
.inherited(subclass) ⇒ Object
:nodoc:
69 70 71 72 |
# File 'lib/alias/creator.rb', line 69 def inherited(subclass) #:nodoc: @creators ||= [] @creators << subclass end |
.map(&block) ⇒ Object
Takes a block which converts the creator’s config to an array of aliases.
52 53 54 |
# File 'lib/alias/creator.rb', line 52 def map(&block) @map = block end |
.maps_config(config) ⇒ Object
:nodoc:
47 48 49 |
# File 'lib/alias/creator.rb', line 47 def maps_config(config) #:nodoc: @map ? @map.call(config) : raise(AbstractMethodError, "No map() defined for #{self}") end |
.valid(key, options = {}) ⇒ Object
Creates a validation expectation for the creator by giving it an Alias::Validator object aka validator. This method must be given an :if or :unless option. If the :if option returns false or :unless option returns true for an alias, the alias is skipped.
Options:
- :if
-
Takes a proc or a symbol referencing a registered validator. This proc must evaluate to true for the validation to pass. See Alias::Validator.validate for what arguments this proc receives by default. See Alias::Validator.default_validators for validators that can be referenced by symbol.
- :unless
-
Same as :if option but the result is negated.
- :message
-
A proc to print a message if the creator’s verbose flag is set. Receives same arguments as :if and :unless procs. If a previous validator is referenced in :unless or :if, then their :message is inherited.
- :with
-
An array of alias attributes/keys which specify the current alias attributes to pass to the validator procs. Overrides default argument a validator proc receives.
- :optional
-
When set to true, this option can be overridden in conjunction with a creator’s force flag. Default is false.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/alias/creator.rb', line 31 def valid(key, ={}) begin validators[key] = Validator.new(.merge(:key=>key, :creator=>self)) rescue Validator::MissingConditionError raise ArgumentError, "A :unless or :if option is required." rescue Validator::InvalidValidatorError $stderr.puts "Validator not set for #{key}" @validators.delete(key) end end |
.validators ⇒ Object
Stores validators per alias attribute/key.
43 44 45 |
# File 'lib/alias/creator.rb', line 43 def validators #:nodoc: @validators ||= {} end |
Instance Method Details
#create(aliases_hash, pretend = false) ⇒ Object
Main method used to create aliases. Handles mapping, validation and creation of aliases.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/alias/creator.rb', line 90 def create(aliases_hash, pretend=false) aliases_array = self.class.maps_config(aliases_hash) delete_invalid_aliases(aliases_array) self.aliases = aliases + aliases_array unless pretend begin #td: create method for efficiently removing constants/methods in any namespace eval_string = Util.silence_warnings { self.class.generates_aliases(aliases_array) } pretend ? puts("\n", eval_string) : Kernel.eval(eval_string) rescue raise FailedAliasCreationError, $! end end |
#delete_invalid_aliases(arr) ⇒ Object
Deletes invalid alias hashes that fail defined validators for a creator.
104 105 106 107 108 109 110 |
# File 'lib/alias/creator.rb', line 104 def delete_invalid_aliases(arr) arr.delete_if {|alias_hash| !self.class.validators.all? {|attribute, validator| validator.validate(self, alias_hash, attribute) } } end |