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.1'.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. -
#initialize(env = {}) ⇒ Component
constructor
A new instance of Component.
Constructor Details
#initialize(env = {}) ⇒ Component
Returns a new instance of Component.
89 90 91 |
# File 'lib/rack/component.rb', line 89 def initialize(env = {}) @env = env end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
119 120 121 |
# File 'lib/rack/component.rb', line 119 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.
84 85 86 |
# File 'lib/rack/component.rb', line 84 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.
33 34 35 |
# File 'lib/rack/component.rb', line 33 def call(env = {}, &child) new(env).call env, &child end |
.flush ⇒ Object
Forget all memoized calls to this component.
59 60 61 |
# File 'lib/rack/component.rb', line 59 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.
54 55 56 |
# File 'lib/rack/component.rb', line 54 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.
73 74 75 |
# File 'lib/rack/component.rb', line 73 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.
115 116 117 |
# File 'lib/rack/component.rb', line 115 def call(*) block_given? ? yield(env) : env end |