Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support :each_as option for specifying part class for array members #36

Merged
merged 1 commit into from May 1, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -12,9 +12,10 @@ def call(name, value, renderer:, context:, **options)

if value.respond_to?(:to_ary)
singular_name = Dry::Core::Inflector.singularize(name).to_sym
singular_options = singularize_options(options)

arr = value.to_ary.map { |obj|
call(singular_name, obj, renderer: renderer, context: context, **options)
call(singular_name, obj, renderer: renderer, context: context, **singular_options)
}

klass.new(name: name, value: arr, renderer: renderer, context: context)
@@ -27,6 +28,15 @@ def call(name, value, renderer:, context:, **options)
def part_class(name, value, **options)
options.fetch(:as) { Part }
end

private

# @api private
def singularize_options(**options)
options = options.dup

This comment has been minimized.

Copy link
@flash-gordon

flash-gordon May 1, 2017

Member

when you capture values with ** ruby creates a new hash instance so calling .dup is not needed

2.3.4 :001 > def foo(**options) options.object_id; end
 => :foo
2.3.4 :002 > {}.tap { |h| puts foo(h) == h.object_id }
false

This comment has been minimized.

Copy link
@timriley

timriley May 1, 2017

Author Member

TIL! Thanks @flash-gordon, I'll adjust this now and I'll internalise this knowledge for the future :)

options[:as] = options.delete(:each_as) if options.key?(:each_as)
options
end
end
end
end
@@ -1,2 +1,4 @@
- customs.each do |custom|
p = custom
p = custom
p = ordinary
@@ -10,20 +10,21 @@ def to_s
end

describe 'default decorator' do
it 'supports wrapping in custom part classes provided to exposure :as option' do
it 'supports wrapping in custom part classes provided to exposure :as and :each_as options' do
vc = Class.new(Dry::View::Controller) do
configure do |config|
config.paths = SPEC_ROOT.join('fixtures/templates')
config.layout = nil
config.template = 'decorated_parts'
end

expose :customs, each_as: Test::CustomPart
expose :custom, as: Test::CustomPart
expose :ordinary
end.new

expect(vc.(custom: 'custom thing', ordinary: 'ordinary thing')).to eql(
'<p>Custom part wrapping custom thing</p><p>ordinary thing</p>'
expect(vc.(customs: ['many things'], custom: 'custom thing', ordinary: 'ordinary thing')).to eql(
'<p>Custom part wrapping many things</p><p>Custom part wrapping custom thing</p><p>ordinary thing</p>'
)
end
end
@@ -44,11 +45,11 @@ def part_class(name, value, **options)
config.template = 'decorated_parts'
end

expose :custom, :ordinary
expose :customs, :custom, :ordinary
end.new

expect(vc.(custom: 'custom thing', ordinary: 'ordinary thing')).to eql(
'<p>Custom part wrapping custom thing</p><p>ordinary thing</p>'
expect(vc.(customs: ['many things'], custom: 'custom thing', ordinary: 'ordinary thing')).to eql(
'<p>Custom part wrapping many things</p><p>Custom part wrapping custom thing</p><p>ordinary thing</p>'
)
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.