lib/sugar-high/alias.rb in sugar-high-0.2.1 vs lib/sugar-high/alias.rb in sugar-high-0.2.2

- old
+ new

@@ -1,7 +1,10 @@ require 'sugar-high/methods' require 'sugar-high/hash' +require 'sugar-high/arguments' +require 'sugar-high/array' +require 'active_support/inflector' class Module # multi_alias name, :create => :new, :insert_into => [:inject_into, :update], :read => :X_content # :options => :after @@ -50,12 +53,32 @@ raise ArgumentError, "Error creating alias for ##{original_name} with ##{new_alias}" end end end + def alias_hash(hash) + pluralize = hash.delete(:pluralize) + singularize = hash.delete(:singularize) + # option = :pluralize => pluralize, :singularize => singularize + + hash.each_pair do |original, alias_meth| + alias_for original, alias_meth + alias_for original.to_s.singularize, alias_meth.to_s.singularize, :singularize => true if singularize + alias_for original.to_s.pluralize, alias_meth.to_s.pluralize, :pluralize => true if pluralize + end + end + def alias_for(original, *aliases) - aliases.each do |alias_meth| - class_eval "alias_method :#{alias_meth}, :#{original} if respond_to? :#{original}" + pluralize = last_option(aliases)[:pluralize] + singularize = last_option(aliases)[:singularize] + + class_eval "alias_method :#{original.to_s.singularize}, :#{original}" if singularize + class_eval "alias_method :#{original.to_s.pluralize}, :#{original}" if pluralize + + aliases.flatten.select_labels.each do |alias_meth| + class_eval "alias_method :#{alias_meth}, :#{original}" + class_eval "alias_method :#{alias_meth.to_s.pluralize}, :#{original}" if pluralize + class_eval "alias_method :#{alias_meth.to_s.singularize}, :#{original}" if singularize end end alias_method :aliases_for, :alias_for protected \ No newline at end of file