Module: TableSync::ORMAdapter::Sequel
- Defined in:
- lib/table_sync/orm_adapter/sequel.rb
Class Method Summary collapse
- .attributes(object) ⇒ Object
- .find(dataset, conditions) ⇒ Object
- .model ⇒ Object
- .setup_sync(klass, **opts) ⇒ Object
- .to_predicate(val, default) ⇒ Object
Class Method Details
.attributes(object) ⇒ Object
15 16 17 |
# File 'lib/table_sync/orm_adapter/sequel.rb', line 15 def attributes(object) object.values end |
.find(dataset, conditions) ⇒ Object
11 12 13 |
# File 'lib/table_sync/orm_adapter/sequel.rb', line 11 def find(dataset, conditions) dataset.find(conditions) end |
.model ⇒ Object
7 8 9 |
# File 'lib/table_sync/orm_adapter/sequel.rb', line 7 def model ::TableSync::Model::Sequel end |
.setup_sync(klass, **opts) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/table_sync/orm_adapter/sequel.rb', line 19 def setup_sync(klass, **opts) if_predicate = to_predicate(opts.delete(:if), true) unless_predicate = to_predicate(opts.delete(:unless), false) raise "Only :if and :unless options are currently supported for Sequel hooks" if opts.any? { create: :created, update: :updated }.each do |event, state| klass.send(:define_method, :"after_#{event}") do if instance_eval(&if_predicate) && !instance_eval(&unless_predicate) db.after_commit do TableSync::Publisher.new(self.class.name, values, state: state).publish end end super() end end klass.send(:define_method, :after_destroy) do # publish anyway db.after_commit do TableSync::Publisher.new(self.class.name, values, state: :destroyed).publish end super() end end |
.to_predicate(val, default) ⇒ Object
44 45 46 47 48 |
# File 'lib/table_sync/orm_adapter/sequel.rb', line 44 def to_predicate(val, default) return val.to_proc if val.respond_to?(:to_proc) -> (*) { default } end |