Module: Familia::Features::Housekeeping::ModelInstanceMethods

Defined in:
lib/familia/features/housekeeping.rb

Overview

Housekeeping::ModelInstanceMethods

Instance Method Summary collapse

Instance Method Details

#do_chore!(name) ⇒ Object

Run a single registered chore by name.

Parameters:

  • name (Symbol, String)

    chore to run

Returns:

  • (Object)

    the block's return value

Raises:

  • (ArgumentError)

    if name is blank or not registered



172
173
174
175
176
177
178
179
180
# File 'lib/familia/features/housekeeping.rb', line 172

def do_chore!(name)
  raise ArgumentError, 'chore name required' if name.nil? || name.to_s.empty?

  key = name.to_sym
  registered = self.class.chores
  raise ArgumentError, "unknown chore #{name.inspect}" unless registered.key?(key)

  registered[key].call(self)
end

#do_chores!Hash{Symbol => Object} Also known as: tidy!

Run every registered chore against this instance.

Returns:

  • (Hash{Symbol => Object})

    chore name => block return value



185
186
187
188
189
# File 'lib/familia/features/housekeeping.rb', line 185

def do_chores!
  self.class.chores.each_with_object({}) do |(chore_name, block), results|
    results[chore_name] = block.call(self)
  end
end