Class: Module

Inherits:
Object show all
Defined in:
lib/freighthopper/lazy_alias.rb,
lib/freighthopper/antonym_accessor.rb,
lib/freighthopper/define_and_alias.rb

Instance Method Summary collapse

Instance Method Details

#antonym_accessor(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/freighthopper/antonym_accessor.rb', line 5

def antonym_accessor(*args)
  if 2 == args.length
    from, to = args
    define_method("#{to}?") { not send "#{from}?" }
    define_method("#{to}=") {|val| send "#{from}=", ! val }
  elsif args.singular.is_a?(Hash)
    args.singular.each {|from, to| antonym_accessor from, to}
  else
    message = <<-EXCEPTION
      antonym_accessor expects either
        "antonym_accessor :from, :to"
      or "antonym_accessor :from => :to"
    EXCEPTION

    raise message.unindent
  end
end

#define_and_alias(target, feature, &blk) ⇒ Object



4
5
6
7
8
# File 'lib/freighthopper/define_and_alias.rb', line 4

def define_and_alias(target, feature, &blk)
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
  define_method :"#{aliased_target}_with_#{feature}#{punctuation}", &blk
  alias_method_chain target, feature
end

#lazy_alias(to, from) ⇒ Object



2
3
4
# File 'lib/freighthopper/lazy_alias.rb', line 2

def lazy_alias(to, from)
  define_method(to){|*args| send from, *args}
end