unmagic-component-partial 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fee998ec6fa5aaa504dbb90e3a3a13f772fb09fe6fbc3f62615548a955ff54ec
4
- data.tar.gz: a981dfd0bef005c9c26d74354c4ec183dd7feef4380d7704a9a9791ad2ebfd47
3
+ metadata.gz: 529eef7a5ff2d79dd21a4b688f5cece6e3a90ffc93dc1300cfba899d91c70868
4
+ data.tar.gz: 8d35fb97bf0bfeb954cabac8a8bb8008cc602c261858aecb78f8c0ef1c78c44d
5
5
  SHA512:
6
- metadata.gz: d98c0a37483bd3dd1738313d1aba3eb8a7b1c4596a8478acde7a7f2ab423ce7a6bcf3760660650c962e0eaa588238e53b7282ca93f7b0e88319bd9c0dd38b956
7
- data.tar.gz: 14ab985618ec009ad9c75b43177cc7678ffb8c1b7fffc3615c6679d92844ad984bb534f6120d75097191e15f50123c290e670511213c97554bc46d31dfb3cbfd
6
+ metadata.gz: 4a467474e28eb41b2dd0aae1197aac4c68f63ea0cf1a71b6d2ab4c2990952cf35fe5a8073ee35ab41e902e677978c0107ce6837debfd77213674d3066db446dc
7
+ data.tar.gz: 24134491954e79fb9e9e325df051f6b752df5274fd4c3b2658e4008f81b81adb5b1579fd72a504c742332dcf95f419e24889bf79326670b5eb07431f171fbc8a
data/CHANGELOG.md CHANGED
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0]
11
+
12
+ ### Added
13
+ - Props on slots: `content_for` accepts keyword arguments
14
+ (`content_for :footer, align: :right do … end`) and the partial reads them
15
+ back with `props_for(:footer)`. Props merge across successive writes and may
16
+ be passed on their own to attach data without content. Existing `content_for`
17
+ read/write behaviour is unchanged.
18
+
10
19
  ## [0.1.0]
11
20
 
12
21
  ### Added
data/README.md CHANGED
@@ -59,13 +59,42 @@ Inside the partial, grab a handle with `component_partial`, render the body with
59
59
  The body block and the slot blocks are the *same* block, so the slots see the
60
60
  caller's locals and helpers exactly as the body does.
61
61
 
62
+ ## Props
63
+
64
+ A slot can carry **props** — structured data the partial reads back to decide
65
+ *how* to render the slot. Pass them as keyword arguments when filling the slot:
66
+
67
+ ```erb
68
+ <% card.content_for :footer, align: :right do %>
69
+ <%= form.submit "Save" %>
70
+ <% end %>
71
+ ```
72
+
73
+ Read them back with `props_for`:
74
+
75
+ ```erb
76
+ <% if card.content_for(:footer) %>
77
+ <footer class="card__footer card__footer--<%= card.props_for(:footer)[:align] %>">
78
+ <%= card.content_for(:footer) %>
79
+ </footer>
80
+ <% end %>
81
+ ```
82
+
83
+ `props_for` returns an empty hash for a slot that has no props, so indexing into
84
+ it is always safe.
85
+
62
86
  ## API
63
87
 
64
88
  - `component_partial` — returns a fresh `Partial` bound to the current view.
65
- - `Partial#content_for(name, content = nil, &block)`:
89
+ - `Partial#content_for(name, content = nil, **props, &block)`:
66
90
  - with a string or block, **writes** the slot (successive writes to the same
67
91
  slot append), and returns `nil`;
68
- - with no content, **reads** the slot back, or `nil` if it was never set.
92
+ - `props` attach data to the slot (successive writes merge, last key winning),
93
+ and may be passed on their own to write props without content;
94
+ - with no content, block, or props, **reads** the slot back, or `nil` if it was
95
+ never set.
96
+ - `Partial#props_for(name)` — reads the props attached to a slot, or `{}` if none
97
+ were set.
69
98
 
70
99
  ## License
71
100
 
@@ -7,24 +7,32 @@ module Unmagic
7
7
  module ComponentPartial
8
8
  # A handle yielded to the block of a partial rendered as a layout. The block
9
9
  # fills named slots; the partial reads them back wherever it likes — for
10
- # content that isn't the main body, e.g. a card footer.
10
+ # content that isn't the main body, e.g. a card footer. A slot can also
11
+ # carry props: structured data the partial reads back with #props_for.
11
12
  class Partial
12
13
  def initialize(view_context)
13
14
  @view_context = view_context
14
15
  @contents = Hash.new { |h, k| h[k] = ActiveSupport::SafeBuffer.new }
16
+ @props = {}
15
17
  end
16
18
 
17
- # Write a slot (string or block), or read it back when called without
18
- # content. Reading an unset slot returns nil.
19
- def content_for(name, content = nil, &block)
20
- if content || block
19
+ # Write a slot (string or block, with optional props), or read it back
20
+ # when called with nothing. Reading an unset slot returns nil.
21
+ def content_for(name, content = nil, **props, &block)
22
+ if content || block || props.any?
21
23
  content = @view_context.capture(&block) if block
22
24
  @contents[name] << content.to_s
25
+ (@props[name] ||= {}).merge!(props) if props.any?
23
26
  nil
24
27
  else
25
28
  @contents[name].presence
26
29
  end
27
30
  end
31
+
32
+ # Read the props attached to a slot, or an empty hash when none were set.
33
+ def props_for(name)
34
+ @props[name] || {}
35
+ end
28
36
  end
29
37
  end
30
38
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Unmagic
4
4
  module ComponentPartial
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unmagic-component-partial
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
  - Keith Pitt