Module: ViewComponentReducible::Component
- Defined in:
- lib/view_component_reducible/component.rb
Overview
Include to enable the component state DSL and helpers.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
- #vcr_envelope ⇒ Hash? readonly
- #vcr_state_token ⇒ String? readonly
Class Method Summary collapse
-
.included(base) ⇒ void
Hook to include DSL and class methods.
Instance Method Summary collapse
- #initialize(vcr_envelope: nil, vcr_state_token: nil, **kwargs) ⇒ Object
-
#render_in(view_context, &block) ⇒ String
Render component markup wrapped in a VCR boundary when available.
-
#vcr_dom_id(path:) ⇒ String
Optional DOM target id for updates.
-
#vcr_state ⇒ Hash{String=>Object}
Build state hash for rendering from the envelope.
Instance Attribute Details
#vcr_envelope ⇒ Hash? (readonly)
18 19 20 |
# File 'lib/view_component_reducible/component.rb', line 18 def vcr_envelope @vcr_envelope end |
#vcr_state_token ⇒ String? (readonly)
20 21 22 |
# File 'lib/view_component_reducible/component.rb', line 20 def vcr_state_token @vcr_state_token end |
Class Method Details
.included(base) ⇒ void
This method returns an undefined value.
Hook to include DSL and class methods.
12 13 14 15 |
# File 'lib/view_component_reducible/component.rb', line 12 def self.included(base) base.include(State::DSL) base.extend(ClassMethods) end |
Instance Method Details
#initialize(vcr_envelope: nil, vcr_state_token: nil, **kwargs) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/view_component_reducible/component.rb', line 24 def initialize(vcr_envelope: nil, vcr_state_token: nil, **kwargs) @vcr_envelope = vcr_envelope @vcr_state_token = vcr_state_token return unless defined?(super) kwargs.empty? ? super() : super(**kwargs) end |
#render_in(view_context, &block) ⇒ String
Render component markup wrapped in a VCR boundary when available.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/view_component_reducible/component.rb', line 51 def render_in(view_context, &block) ensure_vcr_state(view_context) if vcr_envelope.nil? path = vcr_envelope && vcr_envelope['path'] previous_path = view_context.instance_variable_get(:@vcr_current_path) view_context.instance_variable_set(:@vcr_current_path, path) rendered = super return rendered if path.nil? || path.to_s.empty? inject_vcr_path(rendered, path) ensure view_context.instance_variable_set(:@vcr_current_path, previous_path) end |
#vcr_dom_id(path:) ⇒ String
Optional DOM target id for updates.
44 45 46 |
# File 'lib/view_component_reducible/component.rb', line 44 def vcr_dom_id(path:) "vcr:#{self.class.vcr_id}:#{path}" end |
#vcr_state ⇒ Hash{String=>Object}
Build state hash for rendering from the envelope.
34 35 36 37 38 39 |
# File 'lib/view_component_reducible/component.rb', line 34 def vcr_state return {} if vcr_envelope.nil? schema = self.class.vcr_state_schema schema.build(vcr_envelope['data']) end |