Module: Babushka::AcceptsListFor

Included in:
DepDefiner
Defined in:
lib/babushka/accepts_list_for.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


3
4
5
# File 'lib/babushka/accepts_list_for.rb', line 3

def self.included base
  base.send :extend, ClassMethods
end

Instance Method Details

#list_for(method_name, default) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/babushka/accepts_list_for.rb', line 38

def list_for method_name, default
  if payload.has_key? method_name
    payload[method_name].map {|i| i.respond_to?(:call) ? i.call : i }.compact
  else
    # Splatting on expressions instead of just a single token seems
    # to break the result in rubinius. https://gist.github.com/1173301
    values = if default.is_a?(Symbol)
      send(default)
    elsif !default.nil?
      default
    else
      []
    end

    [*values]
  end
end

#store_list_for(method_name, data, choose_with) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/babushka/accepts_list_for.rb', line 30

def store_list_for method_name, data, choose_with
  if data.respond_to? :call
    store_list_for method_name, LambdaChooser.new(self, *chooser_choices, &data).choose(chooser, choose_with), choose_with
  else
    (payload[method_name] ||= []).concat(data || [])
  end
end