Class: Cuprum::Rails::Commands::BuildOne

Inherits:
Cuprum::Rails::Command show all
Defined in:
lib/cuprum/rails/commands/build_one.rb

Overview

Command for generating an ActiveRecord model from an attributes hash.

Instance Attribute Summary

Attributes inherited from Cuprum::Rails::Command

#collection_name, #member_name, #options, #record_class

Instance Method Summary collapse

Methods inherited from Cuprum::Rails::Command

#initialize, #primary_key_name, #primary_key_type, subclass

Constructor Details

This class inherits a constructor from Cuprum::Rails::Command

Instance Method Details

#call(attributes: ) ⇒ ActiveRecord::Base

Builds a new record with the given attributes.

Examples:

Building a record

attributes = {
  'title'    => 'The Hobbit',
  'author'   => 'J.R.R. Tolkien',
  'series'   => nil,
  'category' => 'Science Fiction and Fantasy'
}
command    = Build.new(record_class: Book)
result     = command.call(attributes: attributes)
result.value.attributes
#=> {
  'id'       => nil,
  'title'    => 'The Silmarillion',
  'author'   => 'J.R.R. Tolkien',
  'series'   => nil,
  'category' => 'Science Fiction and Fantasy'
}

Parameters:

  • attributes (Hash) (defaults to: )

    The attributes and values to assign.

Returns:

  • (ActiveRecord::Base)

    the newly built record.



37
38
39
40
# File 'lib/cuprum/rails/commands/build_one.rb', line 37

validate_parameters :call do
  keyword :attributes,
    Stannum::Constraints::Types::HashWithIndifferentKeys.new
end