ru.Bee 2.7.7 → 2.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,11 +47,11 @@ module Rubee
47
47
  if conditions_met?(options[:if], options[:unless])
48
48
  if handler.respond_to?(:call)
49
49
  result = nil
50
- safe_call(handler, [self, args]) do
50
+ handler_result = safe_call(handler, [self, args]) do
51
51
  result = super(*args, &block)
52
52
  end
53
53
 
54
- result
54
+ result || handler_result
55
55
  else
56
56
  send(handler) do
57
57
  super(*args, &block)
@@ -22,6 +22,14 @@ module Rubee
22
22
  @__model.where(*args, __query_dataset: @__query_dataset)
23
23
  end
24
24
 
25
+ def find_first(*args)
26
+ @__model.find_first(*args, __query_dataset: @__query_dataset)
27
+ end
28
+
29
+ def find_last(*args)
30
+ @__model.find_last(*args, __query_dataset: @__query_dataset)
31
+ end
32
+
25
33
  def order(*args)
26
34
  @__model.order(*args, __query_dataset: @__query_dataset)
27
35
  end
@@ -209,11 +209,15 @@ module Rubee
209
209
  end
210
210
 
211
211
  def find_first(args, options = {})
212
- where(args, options).order(:id).limit(1).last
212
+ query_dataset = options[:__query_dataset] || dataset
213
+
214
+ ::Rubee::AssocArray.new([], self, query_dataset.where(**args)).order(:id).limit(1).last
213
215
  end
214
216
 
215
217
  def find_last(args, options = {})
216
- where(args, options).order(id: :desc).limit(1).last
218
+ query_dataset = options[:__query_dataset] || dataset
219
+
220
+ ::Rubee::AssocArray.new([], self, query_dataset.where(**args)).order(id: :desc).limit(1).last
217
221
  end
218
222
 
219
223
  def order(args, options = {})
data/lib/rubee.rb CHANGED
@@ -20,7 +20,7 @@ module Rubee
20
20
  RUBEE_SUPPORT = { "Rubee::Support::Hash" => Hash, "Rubee::Support::String" => String }
21
21
  end
22
22
 
23
- VERSION = '2.7.7'
23
+ VERSION = '2.7.9'
24
24
 
25
25
  require_relative 'rubee/router'
26
26
  require_relative 'rubee/logger'
@@ -1,6 +1,14 @@
1
1
  require_relative '../test_helper'
2
2
 
3
3
  class TestRedirectController < Rubee::BaseController
4
+ around :test_me, ->(controller, &test_method) do
5
+ if true # We wnat to make sure that origianl method is replaced
6
+ controller.response_with(type: :json, object: { hijacked: :yes })
7
+ else
8
+ test_method.call
9
+ end
10
+ end
11
+
4
12
  def index
5
13
  response_with(type: :redirect, to: '/test')
6
14
  end
@@ -8,6 +16,10 @@ class TestRedirectController < Rubee::BaseController
8
16
  def test
9
17
  response_with(type: :json, object: { ok: :ok })
10
18
  end
19
+
20
+ def test_me
21
+ response_with(type: :json, object: { ok: :ok })
22
+ end
11
23
  end
12
24
 
13
25
  class BaseControllerTest < Minitest::Test
@@ -21,6 +33,7 @@ class BaseControllerTest < Minitest::Test
21
33
  Rubee::Router.draw do |route|
22
34
  route.get('/test', to: 'test_redirect#test')
23
35
  route.get('/index', to: 'test_redirect#index')
36
+ route.get('/test_me', to: 'test_redirect#test_me')
24
37
  end
25
38
  end
26
39
 
@@ -45,4 +58,11 @@ class BaseControllerTest < Minitest::Test
45
58
  assert_equal('/test', last_response.headers['Location'])
46
59
  assert_equal('', last_response.body)
47
60
  end
61
+
62
+ def test_hijacked_test_by_around
63
+ get('/test_me')
64
+
65
+ assert_equal(200, last_response.status)
66
+ assert_equal({ "hijacked" => "yes" }, JSON.parse(last_response.body))
67
+ end
48
68
  end
@@ -124,7 +124,7 @@ describe 'Comment model' do
124
124
  Comment.destroy_all
125
125
  comment_1 = Comment.create(text: 'test123123')
126
126
  comment_2 = Comment.create(text: 'test123123')
127
- _(Comment.find_first(text: 'test123123').id).must_equal(comment_1.id)
127
+ _(Comment.where(text: 'test123123').find_first(text: 'test123123').id).must_equal(comment_1.id)
128
128
  Comment.destroy_all
129
129
  end
130
130
  end
@@ -134,7 +134,7 @@ describe 'Comment model' do
134
134
  Comment.destroy_all
135
135
  comment_1 = Comment.create(text: 'test123123')
136
136
  comment_2 = Comment.create(text: 'test123123')
137
- _(Comment.find_last(text: 'test123123').id).must_equal(comment_2.id)
137
+ _(Comment.where(text: 'test123123').find_last(text: 'test123123').id).must_equal(comment_2.id)
138
138
  Comment.destroy_all
139
139
  end
140
140
  end
data/readme.md CHANGED
@@ -380,12 +380,12 @@ Get all records scoped by a field
380
380
  irb(main):005> User.where(email: "ok23@ok.com")
381
381
  => [#<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">]
382
382
  ```
383
- Get the first record. It is a shortcut for `User.where(email: "ok23@ok.com").order(:id).limit(1).last`
383
+ Get the first record, scoped by field. It is a shortcut for `User.where(email: "ok23@ok.com").order(:id).limit(1).last`
384
384
  ```ruby
385
385
  irb(main):006> User.find_first(email: "ok23@ok.com")
386
386
  => #<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">
387
387
  ```
388
- Get the last record. It is a shortcut for `User.where(email: "ok23@ok.com").order(id: :desc).limit(1).last`
388
+ Get the last record, scoped by field. It is a shortcut for `User.where(email: "ok23@ok.com").order(id: :desc).limit(1).last`
389
389
  ```ruby
390
390
  irb(main):007> User.find_last(email: "ok23@ok.com")
391
391
  => #<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">
@@ -940,11 +940,31 @@ class AnyClass
940
940
  end
941
941
  end
942
942
  ```
943
+ The "around" hook can be used to hijack origianl method execution:
944
+ ```ruby
945
+ class AnyClass
946
+ include Rubee::Hookable
947
+ around :print_world, ->(this_instance, &target_method) do
948
+ if condition_that_return_false
949
+ target_method.call
950
+ else
951
+ this_instance.print_hello
952
+ end
953
+ end
943
954
 
944
- Output:
955
+ def print_world
956
+ puts "world!"
957
+ end
958
+
959
+ def print_hello
960
+ puts "hello!"
961
+ end
962
+ end
963
+ ```
964
+
965
+ This will replace origianl method execution with `print_hello` if `condition_that_return_false` returns false.
945
966
  ```bash
946
967
  hello!
947
- world!
948
968
  ```
949
969
 
950
970
  [Back to content](#content)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ru.Bee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.7
4
+ version: 2.7.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov