Skip to content

Commit

Permalink
feature: added slavable extension
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskorobicyn committed Oct 26, 2015
1 parent 4b8b0f3 commit b67f807
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,48 @@ Connection will be switched only for required class.
OtherModel.where(name: 'me').first
end
```

### Execute whole method on any class on other connection
```ruby
class Some
extend Slavable

def some_method
Foo.create
f = Foo.where(...).first
other = SomeModel.where(...).first
f.update_attributes(...)
other.update_attributes(...)
...
end

def self.class_method
b = Bar.create
b.update_attributes(...)
Foo.where(bar: b)
....
end

switch :some_method, to: :other

# it also can be called on multiple methods and works with class_methods

switch :some_method, ..., to: :other

# for switching class method just use singleton class pattern:
class << self
extend Slavable

switch :class_method, ..., to: :other
end
....
end

# it''ll be executed with :other connection
Some.class_method
Some.new.some_method
```

### ACTUNG!!!!

If you connection does not exists, behavior may change dependent of you current Rails environment:
Expand Down
26 changes: 26 additions & 0 deletions lib/slavable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Slavable
def switch(*method_names)
options = method_names.pop

unless options.is_a?(Hash)
raise ArgumentError, 'Unable to detect "to" option, usage: "switch :method, :other, ..., to: :connection_name"'
end

method_names.each do |method|
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
with_name = "#{aliased_method}_with_connection#{punctuation}"
without_name = "#{aliased_method}_without_connection#{punctuation}"
connection = options.with_indifferent_access.fetch(:to)

class_eval <<-eoruby, __FILE__, __LINE__ + 1
def #{with_name}(*args)
::ActiveRecord::Base.within(:#{connection}) { #{without_name}(*args) }
end
eoruby

alias_method without_name, method
alias_method method, with_name
end
end
end
1 change: 1 addition & 0 deletions lib/slaver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'slaver/proxy_methods'
require 'slaver/connection'
require 'slaver/railtie'
require 'slavable'

module Slaver
end
52 changes: 52 additions & 0 deletions spec/lib/slavable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'spec_helper'

class Some
extend Slavable

def some_method
Bar.create
end

def method_wtih_args(name)
Foo.create(name: name)
end

def self.class_method(name)
Foo.create(name: name)
end

switch :some_method, :method_wtih_args, to: :other

class << self
extend Slavable

switch :class_method, to: :other
end
end

describe Slaver do
it 'switches some_method to other connection' do
s = Some.new

s.some_method

expect(Bar.count).to eq 0
expect(Bar.on(:other).count).to eq 1
end

it 'switches method_with_args to other connection' do
s = Some.new

s.method_wtih_args('test')

expect(Foo.where(name: 'test').count).to eq 0
expect(Foo.on(:other).where(name: 'test').count).to eq 1
end

it 'switches class_method to other connection' do
Some.class_method('test')

expect(Foo.where(name: 'test').count).to eq 0
expect(Foo.on(:other).where(name: 'test').count).to eq 1
end
end

0 comments on commit b67f807

Please sign in to comment.