Class: Rack::Component
- Inherits:
-
Object
- Object
- Rack::Component
- Defined in:
- lib/rack/component.rb,
lib/rack/component/version.rb,
lib/rack/component/memory_cache.rb
Overview
Subclass Rack::Component to compose functional, declarative responses to HTTP requests.
Defined Under Namespace
Classes: MemoryCache
Constant Summary collapse
- VERSION =
'0.4.2'.freeze
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
-
.cache ⇒ Object
Find or initialize a cache store for a Component class.
-
.call(env = {}, &child) ⇒ Object
Instantiate a new component with given
envreturn its rendered output. -
.flush ⇒ Object
Forget all memoized calls to this component.
-
.memoized(env = {}, &child) ⇒ Object
Use
memoizedinstead ofcallto memoize the result of call(env) and return it. -
.render(&block) ⇒ Object
Use a
renderblock define what a component will do when youcallit.
Instance Method Summary collapse
-
#call ⇒ Object
Out of the box, a
Rack::Componentjust returns whateverenvyou call it with, or yields withenvif you call it with a block. - #h(obj) ⇒ Object
-
#initialize(env = {}) ⇒ Component
constructor
A new instance of Component.
Constructor Details
#initialize(env = {}) ⇒ Component
Returns a new instance of Component.
90 91 92 |
# File 'lib/rack/component.rb', line 90 def initialize(env = {}) @env = env end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
120 121 122 |
# File 'lib/rack/component.rb', line 120 def env @env end |
Class Method Details
.cache ⇒ Object
Find or initialize a cache store for a Component class. With no configuration, the store is a threadsafe in-memory cache, capped at 100 keys in length to avoid leaking RAM.
85 86 87 |
# File 'lib/rack/component.rb', line 85 def cache @cache ||= (block_given? ? yield : MemoryCache.new(length: 100)) end |
.call(env = {}, &child) ⇒ Object
Instantiate a new component with given env return its rendered output.
34 35 36 |
# File 'lib/rack/component.rb', line 34 def call(env = {}, &child) new(env).call env, &child end |
.flush ⇒ Object
Forget all memoized calls to this component.
60 61 62 |
# File 'lib/rack/component.rb', line 60 def flush cache.flush end |
.memoized(env = {}, &child) ⇒ Object
Use memoized instead of call to memoize the result of call(env) and return it. Subsequent uses of memoized(env) with the same env will be read from a threadsafe in-memory cache, not computed.
55 56 57 |
# File 'lib/rack/component.rb', line 55 def memoized(env = {}, &child) cache.fetch(env.hash) { call(env, &child) } end |
.render(&block) ⇒ Object
Use a render block define what a component will do when you call it.
74 75 76 |
# File 'lib/rack/component.rb', line 74 def render(&block) define_method :call, &block end |
Instance Method Details
#call ⇒ Object
Out of the box, a Rack::Component just returns whatever env you call it with, or yields with env if you call it with a block. Use a class-level render block when wiriting your Components to override this method with more useful behavior.
116 117 118 |
# File 'lib/rack/component.rb', line 116 def call(*) block_given? ? yield(env) : env end |
#h(obj) ⇒ Object
127 128 129 |
# File 'lib/rack/component.rb', line 127 def h(obj) CGI.escapeHTML(obj.to_s) end |