Module: Babushka::AcceptsListFor::ClassMethods

Defined in:
lib/babushka/accepts_list_for.rb

Instance Method Summary collapse

Instance Method Details

#accepts_list_for(method_name, *args) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/babushka/accepts_list_for.rb', line 8

def accepts_list_for method_name, *args
  opts = {:type => 'list'}.merge args.extract_options!
  default = args.shift

  file, line = caller.first.split(':', 2)
  line = line.to_i

  module_eval <<-LOL, file, line
    def #{method_name} *args, &block
      if !args.empty? && !block.nil?
        raise ArgumentError, "You can supply arguments or a block, but not both."
      elsif args.empty? && block.nil?
        #{opts[:type]}_for #{method_name.inspect}, #{default.inspect}
      else
        store_#{opts[:type]}_for #{method_name.inspect}, block || [*args].flatten, #{opts[:choose_with].inspect}
        self
      end
    end
  LOL
end