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

Prepare for Phlex 2.0 💪 #12

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", "head"]
ruby: ["3.2", "3.3", "head"]
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## [Unreleased]

## [0.5.0] - 2025-09-08
- Prepare for Phlex 2.0 💪
- Drop Ruby 2.7, 3.0 and 3.1 support
- Add Phlex::Kit compatibility tests
- Make `Phlex::Slotable::VERSION` available by default

## [0.4.0] - 2024-02-14
- [BREAKING CHANGE] Rename `many` option to `collection`.

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source "https://rubygems.org"
# Specify your gem's dependencies in phlex-slotable.gemspec
gemspec

gem "benchmark-ips", "2.13.0"
gem "benchmark-ips", "2.14.0"
gem "minitest", "5.22.3"
gem "rake", "13.2.1"
gem "standard", "1.35.1"
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Phlex::Slotable enables slots feature to [Phlex](https://www.phlex.fun/) views.
- [Component slot](#component-slot)
- [Lambda slot](#lambda-slot)
- [Polymorphic slot](#polymorphic-slot)
- [Performance](#performance)
- [Development](#development)
- [Contributing](#contributing)

Expand Down Expand Up @@ -343,6 +344,29 @@ Note that you need to use `with_{type}_{slot_name}` to set slot content. In the
> }
> ```

## Performance
Using Phlex::Slotable you don't suffer a performance penalty compared to using Phlex::DeferredRender, sometimes it can even be a little faster.

```
Generated using `ruby benchmark/main.rb`

Phlex 1.11.0
Phlex::Slotable 0.5.0

ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin23]
Warming up --------------------------------------
Deferred 22.176k i/100ms
Slotable 23.516k i/100ms
Calculating -------------------------------------
Deferred 222.727k (± 0.8%) i/s (4.49 μs/i) - 1.131M in 5.078157s
Slotable 237.405k (± 0.6%) i/s (4.21 μs/i) - 1.199M in 5.051936s

Comparison:
Slotable: 237405.0 i/s
Deferred: 222726.8 i/s - 1.07x slower
```


## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
5 changes: 5 additions & 0 deletions benchmark/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
require "benchmark/ips"
require_relative "../lib/phlex/slotable"

require "phlex/version"

puts "Phlex #{Phlex::VERSION}"
puts "Phlex::Slotable #{Phlex::Slotable::VERSION}"

class DeferredList < Phlex::HTML
include Phlex::DeferredRender

Expand Down
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require "bundler/setup"
require "phlex/slotable"
require_relative "../lib/phlex/slotable"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
21 changes: 9 additions & 12 deletions lib/phlex/slotable.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "phlex"
require_relative "slotable/version"

module Phlex
module Slotable
Expand Down Expand Up @@ -56,16 +57,14 @@ def define_lambda_method(slot_name, callable)
def define_getter_method(slot_name, collection:)
getter_method = if collection
<<-RUBY
def #{slot_name}_slots
@#{slot_name}_slots ||= []
end
def #{slot_name}_slots = @#{slot_name}_slots ||= []

private :#{slot_name}_slots
RUBY
else
<<-RUBY
def #{slot_name}_slot
@#{slot_name}_slot
end
def #{slot_name}_slot = @#{slot_name}_slot

private :#{slot_name}_slot
RUBY
end
Expand All @@ -76,16 +75,14 @@ def #{slot_name}_slot
def define_predicate_method(slot_name, collection:)
predicate_method = if collection
<<-RUBY
def #{slot_name}_slots?
#{slot_name}_slots.any?
end
def #{slot_name}_slots? = #{slot_name}_slots.any?

private :#{slot_name}_slots?
RUBY
else
<<-RUBY
def #{slot_name}_slot?
!#{slot_name}_slot.nil?
end
def #{slot_name}_slot? = !#{slot_name}_slot.nil?

private :#{slot_name}_slot?
RUBY
end
Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/slotable/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Phlex
module Slotable
VERSION = "0.4.0"
VERSION = "0.5.0"
end
end
4 changes: 2 additions & 2 deletions phlex-slotable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
spec.summary = "Enable Slot API for Phlex views"
spec.homepage = "https://github.com/stephannv/phlex-slotable"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.7.0"
spec.required_ruby_version = ">= 3.2.0"

# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"

Expand All @@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
spec.add_dependency "phlex", "~> 1.9"
spec.add_dependency "phlex", ">= 1.9", "< 3"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
63 changes: 63 additions & 0 deletions test/phlex/test_kit_compatibility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require "test_helper"

class Phlex::TestKitCompatibility < Minitest::Test
class Headline < Phlex::HTML
include Phlex::Slotable

slot :icon
slot :title

def initialize(size:, bg_color:)
@size = size
@bg_color = bg_color
end

def view_template
div class: "headline text-#{@size} bg-#{@bg_color}" do
render icon_slot
render title_slot
end
end
end

class Header < Phlex::HTML
def view_template(&)
h1(&)
end
end

module Components
extend Phlex::Kit

Headline = Phlex::TestKitCompatibility::Headline
Header = Phlex::TestKitCompatibility::Header
end

class Page < Phlex::HTML
include Components

def view_template
Headline(size: :lg, bg_color: :red) do |h|
h.with_icon { h.i(class: "star") }
h.with_title do
Header { "Hello World!" }
end
end
end
end

def test_with_slots
output = Page.new.call

expected_html = <<~HTML.join_lines
<div class="headline text-lg bg-red">
<i class="star"></i>
<h1>Hello World!</h1>
</div>
HTML

assert_equal expected_html, output
end
end
9 changes: 9 additions & 0 deletions test/phlex/test_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "test_helper"

class Phlex::TestVersion < Minitest::Test
def test_version
refute_nil Phlex::Slotable::VERSION
end
end