Method: Bsm::Constrainable::Field::Base#initialize

Defined in:
lib/bsm/constrainable/field/base.rb

#initialize(name, options = {}) ⇒ Base

Accepts a name and options. Valid options are:

  • :using - a Symbol or a Proc pointing to a DB column, optional (uses name by default)

  • :with - a list of operators to use

  • :scope - a Proc containing additional scopes

Examples:

Field::Integer.new :id
Field::Integer.new :uid, :using => :id
Field::Integer.new :uid, :using => proc { Model.scoped.table[:col_name] }
Field::String.new :name, :with => [:matches]
Field::String.new :author, :with => [:matches], :using => proc { Author.scoped.table[:name] }, :scope => proc { includes(:author) }


29
30
31
32
33
34
35
36
# File 'lib/bsm/constrainable/field/base.rb', line 29

def initialize(name, options = {})
  @name      = name.to_s
  @options   = options.dup
  @attribute = @options.delete(:using) || name
  @operators = Set.new(self.class.operators & Array.wrap(@options.delete(:with)))
  @operators = Set.new(self.class.defaults) if @operators.empty?
  @scope     = @options.delete(:scope)
end