Sha256: 36d785effeaee15154d06296c3a95596ebd61ee838c6f22252e1b04bc37125c9

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

# Creates instance methods which call class methods. These are delegations rather than aliases.
# Expects a hash of classes/modules of the instance method mapped
# to a hash of the class methods and the instance method names. 
# For example, the hash {"MyDate"=>{'Date.today'=>'t'}} would create a MyDate.t instance method
# which directly calls Date.today.
class Alias::Creators::ClassToInstanceMethodCreator < Alias::Creator
  map do |config|
    config.inject([]) {|t,(klass,hash)|
      t += hash.map {|k,v|
        {:class=>klass, :to_class=>k.split('.')[0], :name=>k.split('.')[1], :alias=>v}
      }
    }
  end

  valid :class, :if=>:class
  valid :to_class, :if=>:class
  valid :alias, :unless=>:instance_method, :with=>[:class, :alias], :optional=>true
  valid :to_method, :if=>:class_method, :with=>[:to_class, :name]

  generate do |aliases|
    aliases.map {|e|
      %[#{class_or_module(e[:class])} ::#{e[:class]}; def #{e[:alias]}(*args, &block); #{e[:to_class]}.__send__(:#{e[:name]}, *args, &block); end; end]
    }.join("\n")
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
cldwalker-alias-0.2.0 lib/alias/creators/class_to_instance_method_creator.rb
cldwalker-alias-0.2.1 lib/alias/creators/class_to_instance_method_creator.rb
alias-0.2.3 lib/alias/creators/class_to_instance_method_creator.rb
alias-0.2.2 lib/alias/creators/class_to_instance_method_creator.rb
alias-0.2.0 lib/alias/creators/class_to_instance_method_creator.rb
alias-0.2.1 lib/alias/creators/class_to_instance_method_creator.rb