Skip to content

Commit

Permalink
Add support for Ruby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
lowang committed Jan 27, 2021
1 parent 94746fa commit ee5f199
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 18 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ workflows:
- rspec-ruby-27-activerecord60
- rspec-ruby-27-activerecord52
- rspec-ruby-27-mongoid
- rspec-ruby-30-activerecord60
# TODO elastic 7
# TODO sequel?

Expand Down Expand Up @@ -215,3 +216,15 @@ jobs:
GEMFILE: gemfiles/rails.5.1.mongoid.6.3.gemfile
steps:
- rspec-test

rspec-ruby-30-activerecord60:
docker:
- image: circleci/ruby:3.0
- image: docker.elastic.co/elasticsearch/elasticsearch:5.6.7
environment:
<<: *es56-env
working_directory: ~/repo
environment:
GEMFILE: gemfiles/rails.6.0.activerecord.gemfile
steps:
- rspec-test
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ gem 'guard-rspec'

gem 'redcarpet'
gem 'yard'

eval(File.read('gemfiles/ruby3.gemfile'), nil, 'gemfiles/ruby3.gemfile') if RUBY_VERSION >= '3.0.0'
2 changes: 1 addition & 1 deletion chewy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| # rubocop:disable BlockLength
spec.add_development_dependency 'elasticsearch-extensions'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'resque_spec'
spec.add_development_dependency 'rspec', '~> 3.7.0'
spec.add_development_dependency 'rspec', '>= 3.7.0'
spec.add_development_dependency 'rspec-collection_matchers'
spec.add_development_dependency 'rspec-its'
spec.add_development_dependency 'rubocop', '0.52.1'
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails.6.0.activerecord.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ gem "will_paginate", require: false
gem "parallel", require: false
gem 'rspec_junit_formatter', '~> 0.4.1'

eval(File.read('gemfiles/ruby3.gemfile'), nil, 'gemfiles/ruby3.gemfile') if RUBY_VERSION >= '3.0.0'

gemspec path: "../"
10 changes: 10 additions & 0 deletions gemfiles/ruby3.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# kwargs support was added here:
# https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/method_double.rb#L66
# but this change was not released yet so for the time being use main branch
gem 'rspec', github: 'rspec/rspec', branch: 'main'
gem 'rspec-core', github: 'rspec/rspec-core', branch: 'main'
gem 'rspec-expectations', github: 'rspec/rspec-expectations', branch: 'main'
gem 'rspec-mocks', github: 'rspec/rspec-mocks', branch: 'main'
gem 'rspec-support', github: 'rspec/rspec-support', branch: 'main'
# extracted from stdlib
gem 'rexml'
4 changes: 2 additions & 2 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def exists?
# Suffixed index names might be used for zero-downtime mapping change, for example.
# Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
#
def create(*args)
create!(*args)
def create(*args, **kwargs)
create!(*args, **kwargs)
rescue Elasticsearch::Transport::Transport::Errors::BadRequest
false
end
Expand Down
3 changes: 2 additions & 1 deletion lib/chewy/search/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def self.storages
# limit: Chewy::Search::Parameters::Offset.new(10)
# )
# @param initial [{Symbol => Object, Chewy::Search::Parameters::Storage}]
def initialize(initial = {})
def initialize(initial = {}, **kinitial)
@storages = Hash.new do |hash, name|
hash[name] = self.class.storages[name].new
end
initial = initial.deep_dup.merge(kinitial)
initial.each_with_object(@storages) do |(name, value), result|
storage_class = self.class.storages[name]
storage = value.is_a?(storage_class) ? value : storage_class.new(value)
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/type/witchcraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def brew(object, crutches = nil)
private

def alicorn
@alicorn ||= class_eval <<-RUBY, __FILE__, __LINE__ + 1
@alicorn ||= singleton_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
-> (locals, object0, crutches) do
#{composed_values(@type.root, 0)}
end
Expand Down
18 changes: 9 additions & 9 deletions spec/chewy/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,36 +181,36 @@
end

specify do
expect(EventsIndex::Event.root.compose(
id: 1, category: {id: 2, licenses: {id: 3, name: 'Name'}}
)).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => {'id' => 3, 'name' => 'Name'}})
expect(EventsIndex::Event.root.compose({
id: 1, category: {id: 2, licenses: {id: 3, name: 'Name'}}
})).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => {'id' => 3, 'name' => 'Name'}})
end

specify do
expect(EventsIndex::Event.root.compose(id: 1, category: [
expect(EventsIndex::Event.root.compose({id: 1, category: [
{id: 2, 'licenses' => {id: 3, name: 'Name1'}},
{id: 4, licenses: nil}
])).to eq('id' => 1, 'category' => [
]})).to eq('id' => 1, 'category' => [
{'id' => 2, 'licenses' => {'id' => 3, 'name' => 'Name1'}},
{'id' => 4, 'licenses' => nil.as_json}
])
end

specify do
expect(EventsIndex::Event.root.compose('id' => 1, category: {id: 2, licenses: [
expect(EventsIndex::Event.root.compose({'id' => 1, category: {id: 2, licenses: [
{id: 3, name: 'Name1'}, {id: 4, name: 'Name2'}
]})).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => [
]}})).to eq('id' => 1, 'category' => {'id' => 2, 'licenses' => [
{'id' => 3, 'name' => 'Name1'}, {'id' => 4, 'name' => 'Name2'}
]})
end

specify do
expect(EventsIndex::Event.root.compose(id: 1, category: [
expect(EventsIndex::Event.root.compose({id: 1, category: [
{id: 2, licenses: [
{id: 3, 'name' => 'Name1'}, {id: 4, name: 'Name2'}
]},
{id: 5, licenses: []}
])).to eq('id' => 1, 'category' => [
]})).to eq('id' => 1, 'category' => [
{'id' => 2, 'licenses' => [
{'id' => 3, 'name' => 'Name1'}, {'id' => 4, 'name' => 'Name2'}
]},
Expand Down
6 changes: 4 additions & 2 deletions spec/chewy/search/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,20 @@
describe '#merge!' do
let(:first) { described_class.new(offset: 10, order: 'foo') }
let(:second) { described_class.new(limit: 20, offset: 20, order: 'bar') }
let(:first_clone) { first.clone }
let(:second_clone) { second.clone }

specify do
expect { first.merge!(second) }.to change { first.clone }
.to(described_class.new(limit: 20, offset: 20, order: %w[foo bar]))
end
specify { expect { first.merge!(second) }.not_to change { second.clone } }
specify { expect { first.merge!(second) }.not_to change { second_clone } }

specify do
expect { second.merge!(first) }.to change { second.clone }
.to(described_class.new(limit: 20, offset: 10, order: %w[bar foo]))
end
specify { expect { second.merge!(first) }.not_to change { first.clone } }
specify { expect { second.merge!(first) }.not_to change { first_clone } }

context 'spawns new storages for the merge' do
let(:names) { %i[limit offset order] }
Expand Down
4 changes: 2 additions & 2 deletions spec/chewy/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ def self.by_index(index)
filter { match name: "Name#{index}" }
end

field :name, KEYWORD_FIELD
field :name, **KEYWORD_FIELD
field :rating, type: :integer
end

define_type Country do
field :name, KEYWORD_FIELD
field :name, **KEYWORD_FIELD
field :rating, type: :integer
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/chewy/type/observe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
before { stub_index(:cities) { define_type City } }

specify do
pending if RUBY_VERSION >= '3.0.0'
Chewy.strategy(:urgent) do
ActiveRecord::Base.transaction do
expect { City.create! }.to update_index('cities#city')
Expand Down

0 comments on commit ee5f199

Please sign in to comment.