Class: Metka::Model
- Inherits:
-
Module
- Object
- Module
- Metka::Model
- Defined in:
- lib/metka/model.rb
Instance Method Summary collapse
- #included(base) ⇒ Object
-
#initialize(column:, **options) ⇒ Model
constructor
A new instance of Model.
Constructor Details
#initialize(column:, **options) ⇒ Model
Returns a new instance of Model.
9 10 11 12 |
# File 'lib/metka/model.rb', line 9 def initialize(column: , **) @column = column @options = end |
Instance Method Details
#included(base) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/metka/model.rb', line 14 def included(base) column = @column parser = ->() { @options[:parser] ? @options[:parser].call() : Metka.config.parser.instance.call() } = ->(model, , column, **) { parsed_tag_list = parser.call() if [:without].present? model.where.not(::Metka::QueryBuilder.new.call(model, column, parsed_tag_list, )) else return model.none if parsed_tag_list.empty? model.where(::Metka::QueryBuilder.new.call(model, column, parsed_tag_list, )) end } base.class_eval do scope "with_all_#{column}", ->() { .call(self, , column) } scope "with_any_#{column}", ->() { .call(self, , column, { any: true }) } scope "without_all_#{column}", ->() { .call(self, , column, { exclude_all: true, without: true }) } scope "without_any_#{column}", ->() { .call(self, , column, { exclude_any: true, without: true }) } end base.define_method(column.singularize + '_list=') do |v| self.write_attribute(column, parser.call(v).to_a) self.write_attribute(column, nil) if self.send(column).empty? end base.define_method(column.singularize + '_list') do parser.call(self.send(column)) end end |