Module: Cassie::Statements::Statement::Assignments::ClassMethods

Included in:
Inserting, Updating
Defined in:
lib/cassie/statements/statement/assignments.rb

Instance Method Summary collapse

Instance Method Details

#assignments_argsObject

The enumeration of current assignments’ parameters that will be used to build Assignment objects when the statement is built



65
66
67
# File 'lib/cassie/statements/statement/assignments.rb', line 65

def assignments_args
  @assignments_args ||= []
end

#set(identifier, opts = {}) ⇒ Enumerable<Array<Object>>

DSL to set an assigment (SET or VALUES clause) for UPDATE and INSERT statements.

Defining an assigment also defines an attr_accessor with the same name as the identifier (or the :value option if a symbol is used). The underlying instance variable value for this accessor will be used when determining the value for the assigment.

Examples:

Assigment with implied accessor

set :username #<= gets assigment value from `:username` method

Assigment with explicit accessor

set :username, value: :name #<= gets assigment value from `:name` method

Parameters:

  • identifier (String, Symbol)

    The column name to set.

  • opts (Hash) (defaults to: {})

    options for the assigment

Options Hash (opts):

  • :value (Symbol, Object)

    The value to use for the assigment (constraint). If a [Symbol], a method with that name will be called when the statement is built. Otherwise, the value itself will be used.

  • :if (Symbol, Object)

    Determines if the assigment is applied to the statement or not. If a [Symbol], a method with that name will be called when the statement is built. Otherwise, the value itself will be used.

  • :term (String)

    The argument value to use instead of a positional placeholder (?). If a [Symbol], a method with that name will be called when the statement is built. Otherwise, the value itself will be used.

Returns:

  • (Enumerable<Array<Object>>)

    The enumeration of current assigments’ parameters

Raises:

  • (StandardError)

    if there is already a getter or setter method defined for the assigment value’s implied accessor (identifier or symbol :value option).



55
56
57
58
59
60
61
# File 'lib/cassie/statements/statement/assignments.rb', line 55

def set(identifier, opts={})
  opts[:value] ||= identifier.to_sym

  define_argument_accessor(opts[:value])

  assignments_args << [identifier, opts.delete(:value), opts]
end