Module: Spare::Execution::ClassMethods

Defined in:
lib/spare/execution.rb

Instance Method Summary collapse

Instance Method Details

#execute(attributes = nil) ⇒ Object Also known as: call

Build an object (or multiple objects) and executes, if validations pass. The resulting object is returned whether the object was executed successfully to the database or not.

The attributes parameter can be either a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.



9
10
11
12
13
14
15
16
17
# File 'lib/spare/execution.rb', line 9

def execute(attributes = nil)
  if attributes.is_a?(Array)
    attributes.map { |attr| excute(attr) }
  else
    object = new(attributes)
    object.execute
    object
  end
end

#execute!(attributes = nil) ⇒ Object Also known as: call!

Build an object (or multiple objects) and executes, if validations pass. Raises a RecordInvalid error if validations fail, unlike Base#create.

The attributes parameter can be either a Hash or an Array of Hashes. These describe which attributes to be created on the object, or multiple objects when given an Array of Hashes.



27
28
29
30
31
32
33
34
35
# File 'lib/spare/execution.rb', line 27

def execute!(attributes = nil)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create!(attr) }
  else
    object = new(attributes)
    object.execute!
    object
  end
end