Class: Bsm::Constrainable::Field::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bsm/constrainable/field/base.rb

Overview

Abstract field type

Direct Known Subclasses

Boolean, Date, Number, String, Timestamp

Constant Summary collapse

DEFAULT_OPERATORS =
[:eq, :not_eq, :gt, :gteq, :lt, :lteq, :between].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



14
15
16
# File 'lib/bsm/constrainable/field/base.rb', line 14

def attribute
  @attribute
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/bsm/constrainable/field/base.rb', line 14

def name
  @name
end

#operatorsObject (readonly)

Returns the value of attribute operators.



14
15
16
# File 'lib/bsm/constrainable/field/base.rb', line 14

def operators
  @operators
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/bsm/constrainable/field/base.rb', line 14

def options
  @options
end

#scopeObject (readonly)

Returns the value of attribute scope.



14
15
16
# File 'lib/bsm/constrainable/field/base.rb', line 14

def scope
  @scope
end

Class Method Details

.kindObject

Returns the field type/kind, e.g. :string or :integer



10
11
12
# File 'lib/bsm/constrainable/field/base.rb', line 10

def self.kind
  @kind ||= name.demodulize.underscore.to_sym
end

Instance Method Details

#convert(value) ⇒ Object



50
51
52
# File 'lib/bsm/constrainable/field/base.rb', line 50

def convert(value)
  value.is_a?(Array) ? value.map {|v| convert(v) } : _convert(value)
end

#merge(relation, operator, value) ⇒ Object

Merge params into a relation



39
40
41
42
43
44
45
46
47
48
# File 'lib/bsm/constrainable/field/base.rb', line 39

def merge(relation, operator, value)
  operator = operator.to_sym
  return relation unless operators.include?(operator)

  operation = Bsm::Constrainable::Operation.new(operator, value, relation, self)
  return relation if operation.clause.nil?

  relation = relation.instance_eval(&scope) if scope
  relation.where(operation.clause)
end