Class: Bsm::Constrainable::Field::Base
- Inherits:
-
Object
- Object
- Bsm::Constrainable::Field::Base
- Defined in:
- lib/bsm/constrainable/field/base.rb
Overview
Abstract field type
Constant Summary collapse
- DEFAULT_OPERATORS =
[:eq, :not_eq, :gt, :gteq, :lt, :lteq, :between].freeze
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#operators ⇒ Object
readonly
Returns the value of attribute operators.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
-
.kind ⇒ Object
Returns the field type/kind, e.g.
Instance Method Summary collapse
- #convert(value) ⇒ Object
-
#initialize(name, options = {}) ⇒ Base
constructor
Accepts a name and options.
-
#merge(relation, operator, value) ⇒ Object
Merge params into a relation.
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, = {}) @name = name.to_s @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
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
14 15 16 |
# File 'lib/bsm/constrainable/field/base.rb', line 14 def attribute @attribute end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/bsm/constrainable/field/base.rb', line 14 def name @name end |
#operators ⇒ Object (readonly)
Returns the value of attribute operators.
14 15 16 |
# File 'lib/bsm/constrainable/field/base.rb', line 14 def operators @operators end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/bsm/constrainable/field/base.rb', line 14 def @options end |
#scope ⇒ Object (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
.kind ⇒ Object
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 |