Module: TinySweeper::ClassMethods

Defined in:
lib/tiny_sweeper.rb

Instance Method Summary collapse

Instance Method Details

#sweep(field_names, *broom_names, &sweeper) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tiny_sweeper.rb', line 6

def sweep(field_names, *broom_names, &sweeper)
  Array(field_names).each do |field_name|
    stop_if_we_have_seen_this_before!(field_name)
    warn_about_missing_brooms(broom_names)

    overrides_module.module_eval do
      define_method("#{field_name}=") do |value|
        if value
          cleaned_up = broom_names.inject(value) { |accum, broom_name|
            ::TinySweeper::Brooms.fetch(broom_name).call(accum)
          }
          cleaned_up = sweeper.call(cleaned_up) if sweeper
          super(cleaned_up)
        else
          super(value)
        end
      end
    end
  end
end

#sweep_up!(instance) ⇒ Object



27
28
29
30
31
# File 'lib/tiny_sweeper.rb', line 27

def sweep_up!(instance)
  @swept_fields.each do |field|
    instance.send("#{field}=", instance.send(field))
  end
end