Skip to content

Commit 2c242d7

Browse files
committed
added new active support methods from rails 6
1 parent 3b94282 commit 2c242d7

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

ruby/hyper-component/lib/hyper-component.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
require 'hyperstack/ext/component/kernel'
2626
require 'hyperstack/ext/component/number'
2727
require 'hyperstack/ext/component/boolean'
28+
require 'hyperstack/ext/component/array'
29+
require 'hyperstack/ext/component/enumerable'
2830
require 'hyperstack/component/isomorphic_helpers'
2931
require 'hyperstack/component/react_api'
3032
require 'hyperstack/internal/component/top_level_rails_component'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# from Rails 6.0 activesupport. Remove once defined by Opal activesupport
2+
class Array
3+
# Removes and returns the elements for which the block returns a true value.
4+
# If no block is given, an Enumerator is returned instead.
5+
#
6+
# numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7+
# odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9]
8+
# numbers # => [0, 2, 4, 6, 8]
9+
def extract!
10+
return to_enum(:extract!) { size } unless block_given?
11+
12+
extracted_elements = []
13+
14+
reject! do |element|
15+
extracted_elements << element if yield(element)
16+
end
17+
18+
extracted_elements
19+
end
20+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# from Rails 6.0 activesupport. Remove once defined by Opal activesupport
2+
module Enumerable
3+
INDEX_WITH_DEFAULT = Object.new
4+
private_constant :INDEX_WITH_DEFAULT
5+
def index_with(default = INDEX_WITH_DEFAULT)
6+
if block_given?
7+
result = {}
8+
each { |elem| result[elem] = yield(elem) }
9+
result
10+
elsif default != INDEX_WITH_DEFAULT
11+
result = {}
12+
each { |elem| result[elem] = default }
13+
result
14+
else
15+
to_enum(:index_with) { size if respond_to?(:size) }
16+
end
17+
end
18+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'spec_helper'
2+
3+
# rubocop:disable Metrics/BlockLength
4+
describe 'Active Support Helpers', js: true do
5+
it "Array#index_with" do
6+
on_client do
7+
Payment = Struct.new(:price)
8+
class GenericEnumerable
9+
include Enumerable
10+
def initialize(values = [1, 2, 3])
11+
@values = values
12+
end
13+
def each
14+
@values.each { |v| yield v }
15+
end
16+
end
17+
def payments
18+
GenericEnumerable.new([ Payment.new(5), Payment.new(15), Payment.new(10) ])
19+
end
20+
end
21+
expect_evaluate_ruby do
22+
{ Payment.new(5) => 5, Payment.new(15) => 15, Payment.new(10) => 10 } == payments.index_with(&:price)
23+
end.to be_truthy
24+
expect_evaluate_ruby do
25+
%i( title body ).index_with(nil)
26+
end.to eq({ title: nil, body: nil }.with_indifferent_access)
27+
expect_evaluate_ruby do
28+
%i( title body ).index_with([])
29+
end.to eq({ title: [], body: [] }.with_indifferent_access)
30+
expect_evaluate_ruby do
31+
%i( title body ).index_with({})
32+
end.to eq({ title: {}, body: {} }.with_indifferent_access)
33+
expect_evaluate_ruby do
34+
Enumerator == payments.index_with.class
35+
end.to be_truthy
36+
expect_evaluate_ruby do
37+
payments.index_with.size
38+
end.to be_nil
39+
expect_evaluate_ruby do
40+
(1..42).index_with.size
41+
end.to eq 42
42+
end
43+
44+
it "Enumerable#extract!" do
45+
# interesting could not find the test case in rails, this is from the comments:
46+
on_client do
47+
def numbers
48+
@numbers ||= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
49+
end
50+
end
51+
expect_evaluate_ruby do
52+
odd_numbers = numbers.extract! { |number| number.odd? }
53+
end.to eq [1, 3, 5, 7, 9]
54+
expect_evaluate_ruby do
55+
numbers.extract! { |number| number.odd? }
56+
numbers
57+
end.to eq [0, 2, 4, 6, 8]
58+
end
59+
end

0 commit comments

Comments
 (0)