rack-component 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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/rack/component.rb +14 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4a43f4454b5d988c4745d26c61bb46b8cbe21e56e3aa74d6b84427d4f110ab7
|
4
|
+
data.tar.gz: f49b907249aef3ebc79c5168c785eeb7ea977152c41278896e5cd4432467f2c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a48bcaf0c62f6d83071d6a99ce5517898d28d498dc8f23ef61392641ea7a71a6b921f8f65521f35c4c79a001c90978594a9d439f4e9ab67dc0094840b43cfafb
|
7
|
+
data.tar.gz: 87cd31cacc2c95b330e68cc07574f586c45777c69b0e4c5b6810fba0ff8325f4cb85b467fb718d35a201b3cbb7a09cf816c3ea7d2e05e5f0059d532c100e15be
|
data/Gemfile.lock
CHANGED
data/lib/rack/component.rb
CHANGED
@@ -4,7 +4,7 @@ module Rack
|
|
4
4
|
# Subclass Rack::Component to compose declarative, component-based responses
|
5
5
|
# to HTTP requests
|
6
6
|
class Component
|
7
|
-
VERSION = '0.
|
7
|
+
VERSION = '0.2.0'.freeze
|
8
8
|
|
9
9
|
EMPTY = ''.freeze # components render an empty body by default
|
10
10
|
attr_reader :props
|
@@ -17,22 +17,22 @@ module Rack
|
|
17
17
|
# props[:world]
|
18
18
|
# end
|
19
19
|
#
|
20
|
-
# def
|
20
|
+
# def call
|
21
21
|
# %(<h1>Hello #{world}</h1>)
|
22
22
|
# end
|
23
23
|
# end
|
24
24
|
#
|
25
25
|
# MyComponent.call(world: 'Earth') #=> '<h1>Hello Earth</h1>'
|
26
|
-
# @return [String, Object] the
|
27
|
-
def self.call(props
|
28
|
-
new(props).render(&block)
|
26
|
+
# @return [String, Object] the output of instance#render
|
27
|
+
def self.call(*props, &block)
|
28
|
+
new(*props).render(&block)
|
29
29
|
end
|
30
30
|
|
31
31
|
def initialize(props = {})
|
32
32
|
@props = props
|
33
33
|
end
|
34
34
|
|
35
|
-
# Override
|
35
|
+
# Override call to make your component do work.
|
36
36
|
# @return [String, Object] usually a string, but really whatever
|
37
37
|
def render
|
38
38
|
block_given? ? yield(self) : EMPTY
|
@@ -40,7 +40,7 @@ module Rack
|
|
40
40
|
|
41
41
|
# Rack::Component::Memoized is just like Component, only it
|
42
42
|
# caches its rendered output in memory and only rerenders
|
43
|
-
# when called with new props
|
43
|
+
# when called with new props.
|
44
44
|
class Memoized < self
|
45
45
|
CACHE_SIZE = 100 # limit cache to 100 keys by default so we don't leak RAM
|
46
46
|
|
@@ -57,7 +57,7 @@ module Rack
|
|
57
57
|
# "#{props[:id]} was expensive"
|
58
58
|
# end
|
59
59
|
#
|
60
|
-
# def
|
60
|
+
# def call
|
61
61
|
# %(<h1>#{work}</h1>)
|
62
62
|
# end
|
63
63
|
# end
|
@@ -70,25 +70,25 @@ module Rack
|
|
70
70
|
# Expensive.call(id: 2) #=> <h1>2 was expensive</h1>
|
71
71
|
#
|
72
72
|
# @return [String, Object] the cached (or computed) output of render
|
73
|
-
def self.call(props
|
74
|
-
memoized(props) { super }
|
73
|
+
def self.call(*props, &block)
|
74
|
+
memoized(*props) { super }
|
75
75
|
end
|
76
76
|
|
77
77
|
# Check the class-level cache, set it to &miss if nil.
|
78
78
|
# @return [Object] the output of &miss.call
|
79
|
-
def self.memoized(props, &miss)
|
80
|
-
cache.fetch(key(props), &miss)
|
79
|
+
def self.memoized(*props, &miss)
|
80
|
+
cache.fetch(key(*props), &miss)
|
81
81
|
end
|
82
82
|
|
83
83
|
# @return [Integer] a cache key for this component
|
84
|
-
def self.key(props)
|
84
|
+
def self.key(*props)
|
85
85
|
props.hash
|
86
86
|
end
|
87
87
|
|
88
88
|
# Clear the cache of each descendant class.
|
89
89
|
# Generally you'll call this on Rack::Component::Memoized directly.
|
90
90
|
# @example Clear all caches:
|
91
|
-
# Rack::Component::Memoized.
|
91
|
+
# Rack::Component::Memoized.clear_caches
|
92
92
|
def self.clear_caches
|
93
93
|
ObjectSpace.each_object(singleton_class) do |descendant|
|
94
94
|
descendant.cache.flush
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Frank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: benchmark-ips
|