Module: Main::Cast

Defined in:
lib/main/cast.rb

Constant Summary collapse

List =
[]

Class Method Summary collapse

Class Method Details

.[](sym) ⇒ Object

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
160
161
# File 'lib/main/cast.rb', line 152

def self.[] sym
  prefix = sym.to_s.downcase.to_sym
  candidates = List.select{|m| m =~ %r/^#{ prefix }/i}
  m = candidates.shift
  raise ArgumentError, "unsupported cast: #{ sym.inspect } (#{ List.join ',' })" unless
    m
  raise ArgumentError, "ambiguous cast: #{ sym.inspect } (#{ List.join ',' })" unless
    candidates.empty? or m.to_s == sym.to_s
  lambda{|obj| method(m).call obj}
end

.cast(*args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/main/cast.rb', line 10

def Cast.cast(*args, &block)
  if args.first && block
    method = args.shift
    define_method(method, &block)
    export(method)
    List << (method.to_s)
  else
    Cast.send(*args)
  end
end

.export(m) ⇒ Object



3
4
5
6
# File 'lib/main/cast.rb', line 3

def self.export m
  module_function m
  public m
end