Skip to content

Commit

Permalink
Rename 'many' option to 'collection'
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Feb 15, 2024
1 parent c73dab3 commit 3fe7ed6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benchmark/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SlotableList < Phlex::HTML
include Phlex::Slotable

slot :header
slot :item, many: true
slot :item, collection: true

def template
if header_slot
Expand Down
22 changes: 11 additions & 11 deletions lib/phlex/slotable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ def self.included(base)
end

module ClassMethods
def slot(slot_name, callable = nil, types: nil, many: false)
def slot(slot_name, callable = nil, types: nil, collection: false)
include Phlex::DeferredRender

if types
types.each do |type, callable|
define_setter_method(slot_name, callable, many: many, type: type)
define_setter_method(slot_name, callable, collection: collection, type: type)
end
else
define_setter_method(slot_name, callable, many: many)
define_setter_method(slot_name, callable, collection: collection)
end
define_predicate_method(slot_name, many: many)
define_getter_method(slot_name, many: many)
define_predicate_method(slot_name, collection: collection)
define_getter_method(slot_name, collection: collection)
end

private

def define_setter_method(slot_name, callable, many:, type: nil)
def define_setter_method(slot_name, callable, collection:, type: nil)
slot_name_with_type = type ? "#{type}_#{slot_name}" : slot_name

setter_method = if many
setter_method = if collection
<<-RUBY
def with_#{slot_name_with_type}(*args, **kwargs, &block)
@#{slot_name}_slots ||= []
Expand All @@ -52,8 +52,8 @@ def define_lambda_method(slot_name, callable)
private :"__call_#{slot_name}__"
end

def define_getter_method(slot_name, many:)
getter_method = if many
def define_getter_method(slot_name, collection:)
getter_method = if collection
<<-RUBY
def #{slot_name}_slots
@#{slot_name}_slots ||= []
Expand All @@ -72,8 +72,8 @@ def #{slot_name}_slot
class_eval(getter_method, __FILE__, __LINE__ + 1)
end

def define_predicate_method(slot_name, many:)
predicate_method = if many
def define_predicate_method(slot_name, collection:)
predicate_method = if collection
<<-RUBY
def #{slot_name}_slots?
#{slot_name}_slots.any?
Expand Down
4 changes: 2 additions & 2 deletions test/phlex/test_multi_polymorphic_slot copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def template
class UsersList < Phlex::HTML
include Phlex::Slotable

slot :avatar, types: {
slot :avatar, collection: true, types: {
image: ImageComponent,
icon: "IconComponent",
text: ->(size:, &content) do
span(class: "text-#{size}", &content)
end
}, many: true
}

def template
if avatar_slots?
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/test_multi_slot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Phlex::TestMultiSlot < Minitest::Test
class Blog < Phlex::HTML
include Phlex::Slotable

slot :post, many: true
slot :post, collection: true

def template
if post_slots?
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/test_multi_slot_with_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def template(&content)
class Blog < Phlex::HTML
include Phlex::Slotable

slot :post, PostComponent, many: true
slot :post, PostComponent, collection: true

def template
if post_slots?
Expand Down
4 changes: 2 additions & 2 deletions test/phlex/test_multi_slot_with_lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def template
class Blog < Phlex::HTML
include Phlex::Slotable

slot :post, ->(featured: false, &content) { p(class: featured ? "featured" : nil, &content) }, many: true
slot :post, ->(featured: false, &content) { p(class: featured ? "featured" : nil, &content) }, collection: true
slot :headline, ->(size:, &content) do
render HeadlineComponent.new(size: size, bg_color: @headline_bg_color), &content
end, many: true
end, collection: true

def initialize(headline_bg_color: nil)
@headline_bg_color = headline_bg_color
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/test_multi_slot_with_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Phlex::TestMultiSlotWithString < Minitest::Test
class Blog < Phlex::HTML
include Phlex::Slotable

slot :post, "PostComponent", many: true
slot :post, "PostComponent", collection: true

def template
if post_slots?
Expand Down

0 comments on commit 3fe7ed6

Please sign in to comment.