phlex-variants 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97bc72ef60ebb3ae5986bf833da453a8ce0f5768987cea06034ffa62bb1b087f
4
- data.tar.gz: a775ea5a27cfa083a357ed37c0f35c0b62a46c58abb5626d63b4b144fffc9d7d
3
+ metadata.gz: e87aa83193854c3623cb503ca7931d71b0c1f1bd453c33b84d5969f3079674e3
4
+ data.tar.gz: '085c09e554ba5aa090127306236100a543f5f94df6e23a37adc6feb28b3ab599'
5
5
  SHA512:
6
- metadata.gz: f099f9a914a18007e2afeed60747ad8e255d791874f265a2862a4e0ea2267f1333b38507bf80bac7e91cd82d039d3c27f4a88cb01e1679cb6d3d0f442a8c4325
7
- data.tar.gz: b0505e878bef8b833a2f831a50c30c9d0971f68b69669179ad1f721ba3502399456e98d8e6fd62d736e158e0c1b7a33c77f79f76694a0fd332a14194e4d3b62a
6
+ metadata.gz: 17d3ede3de7f5aee7f68f44d774d98873570400b24c68787b7fd2b93e9c6d0218d02029232e54e3a0f0d00e4f7991013171785485a98ee1e8e4aa52315d53196
7
+ data.tar.gz: '087150ea587c8a93254de4fe6df98218b52fb4bdc5079c787d841d87c5718532809657f44521ebded4fe598fb0a8d1881dd6d014c9ef130d418ce7c9b0ff21c5'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-09-29
4
+
5
+ - Allow only true/false for boolean variants
6
+ - Allow passing false for variants with only true option
7
+ - Improved error message when variant option doesn't exist
8
+ - Improved error message when method missing on StyleBuilder
9
+ - Improve performance of `build_style` method
10
+
3
11
  ## [0.1.0] - 2024-09-25
4
12
 
5
13
  - Initial release
data/README.md CHANGED
@@ -30,7 +30,7 @@ gem install phlex-variants
30
30
  ```
31
31
 
32
32
  > [!TIP]
33
- > If you prefer not to add another dependency to your project, you can simply copy the [Phlex::Variants](https://github.com/stephannv/phlex-variant/blob/main/lib/phlex/variants.rb) file into your project.
33
+ > If you prefer not to add another dependency to your project, you can simply copy the [Phlex::Variants](https://github.com/stephannv/phlex-variants/blob/main/lib/phlex/variants.rb) file into your project.
34
34
 
35
35
 
36
36
  ## Usage
@@ -66,7 +66,7 @@ class Button < Phlex::HTML
66
66
  defaults color: :default, size: :md
67
67
  end
68
68
 
69
- attr_reader :color, :size, :outline, :full
69
+ attr_reader :color, :size, :outline
70
70
 
71
71
  def initialize(color: nil, size: nil, outline: nil)
72
72
  @color = color
@@ -119,6 +119,74 @@ build_style(color: :primary, extra_classes: "disabled:hidden")
119
119
  # => "btn btn-danger btn-xs disabled:hidden"
120
120
  ```
121
121
 
122
+ #### Generated constants
123
+ The following code:
124
+ ```ruby
125
+ style do
126
+ base "btn"
127
+
128
+ variants do
129
+ color do
130
+ default "btn-default"
131
+ primary "btn-primary"
132
+ danger "btn-danger"
133
+ end
134
+
135
+ size do
136
+ xs "btn-xs"
137
+ md "btn-md"
138
+ lg "btn-lg"
139
+ end
140
+
141
+ outline do
142
+ yes "btn-outline"
143
+ end
144
+ end
145
+
146
+ defaults color: :default, size: :md
147
+ end
148
+ ```
149
+ Will generate the following constants in your view class:
150
+ ```ruby
151
+ STYLE_BASE = "btn"
152
+ STYLE_VARIANTS = {
153
+ color: {
154
+ default: "btn-default",
155
+ primary: "btn-primary",
156
+ danger: "btn-danger"
157
+ },
158
+ size: {
159
+ xs: "btn-xs",
160
+ md: "btn-md",
161
+ lg: "btn-lg"
162
+ },
163
+ outline: {
164
+ true => "btn-outline"
165
+ }
166
+ }
167
+ STYLE_DEFAULTS = { color: :default, size: :md }
168
+ ```
169
+
170
+ For example, if you're creating Lookbook previews, you can do something like this:
171
+ ```ruby
172
+ class ButtonPreview < Lookbook::Preview
173
+ # @param color select :color_options
174
+ def playground(color: nil)
175
+ render Button.new(color:)
176
+ end
177
+
178
+ private
179
+
180
+ def color_options
181
+ {
182
+ choices: Button::STYLE_VARIANTS[:color].keys,
183
+ include_blank: "default",
184
+ value_type: "Symbol"
185
+ }
186
+ end
187
+ end
188
+ ```
189
+
122
190
  ## Development
123
191
 
124
192
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Variants
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
@@ -27,16 +27,34 @@ module Phlex
27
27
 
28
28
  # @api private
29
29
  def build_variants_style(variants)
30
- self::STYLE_DEFAULTS.merge(variants.compact).map do |variant, option|
31
- value = self::STYLE_VARIANTS.dig(variant, option)
30
+ variants = variants.compact
31
+ variants = self::STYLE_DEFAULTS.merge(variants) unless self::STYLE_DEFAULTS.empty?
32
32
 
33
- if value.nil?
34
- raise VariantNotFoundError, "Variant `#{variant}: #{option.inspect}` doesn't exist"
33
+ variants.map do |variant, option|
34
+ options = self::STYLE_VARIANTS[variant]
35
+
36
+ if options
37
+ value = options[option]
38
+
39
+ next value if value
40
+
41
+ # doesn't raise error when passing false for variant with only true option
42
+ next if option == false && options.has_key?(true)
35
43
  end
36
44
 
37
- value
45
+ raise_variant_not_found_error(options, variant, option)
38
46
  end
39
47
  end
48
+
49
+ def raise_variant_not_found_error(options, variant, option)
50
+ message = if options
51
+ "Option #{option.inspect} for #{variant.inspect} variant doesn't exist. Valid options are: #{options.keys}"
52
+ else
53
+ "Variant #{variant.inspect} doesn't exist. Available variants are: #{self::STYLE_VARIANTS.keys}"
54
+ end
55
+
56
+ raise VariantNotFoundError, message
57
+ end
40
58
  end
41
59
 
42
60
  private
@@ -77,6 +95,11 @@ module Phlex
77
95
  def defaults(**variants)
78
96
  view_class::STYLE_DEFAULTS.merge!(variants)
79
97
  end
98
+
99
+ def method_missing(method, *args, &) # standard:disable Style/MissingRespondToMissing
100
+ message = "undefined method '#{method}' for an instance of Phlex::Variants::StyleBuilder. The available methods are: 'base', 'variants' and 'defaults'"
101
+ raise NoMethodError, message
102
+ end
80
103
  end
81
104
 
82
105
  # @api private
@@ -115,12 +138,13 @@ module Phlex
115
138
 
116
139
  def method_missing(name, *args) # standard:disable Style/MissingRespondToMissing
117
140
  option = name.to_sym
118
- view_class::STYLE_VARIANTS[variant_name][option] = args
119
141
 
120
142
  if option == :yes
121
143
  view_class::STYLE_VARIANTS[variant_name][true] = args
122
144
  elsif option == :no
123
145
  view_class::STYLE_VARIANTS[variant_name][false] = args
146
+ else
147
+ view_class::STYLE_VARIANTS[variant_name][option] = args
124
148
  end
125
149
  end
126
150
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-variants
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - stephann
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-09-26 00:00:00.000000000 Z
10
+ date: 2024-09-29 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: phlex
@@ -52,7 +51,6 @@ licenses:
52
51
  - MIT
53
52
  metadata:
54
53
  homepage_uri: https://github.com/stephannv/phlex-variants
55
- post_install_message:
56
54
  rdoc_options: []
57
55
  require_paths:
58
56
  - lib
@@ -67,8 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
65
  - !ruby/object:Gem::Version
68
66
  version: '0'
69
67
  requirements: []
70
- rubygems_version: 3.5.16
71
- signing_key:
68
+ rubygems_version: 3.6.0.dev
72
69
  specification_version: 4
73
70
  summary: Compose your Phlex component with style variants.
74
71
  test_files: []