Class: Showcase::Sample

Inherits:
Object
  • Object
show all
Defined in:
app/models/showcase/sample.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, name, description: nil, id: name.parameterize, syntax: :erb, events: nil, **details) ⇒ Sample

Returns a new instance of Sample.



5
6
7
8
9
10
# File 'app/models/showcase/sample.rb', line 5

def initialize(view_context, name, description: nil, id: name.parameterize, syntax: :erb, events: nil, **details)
  @view_context = view_context
  @name, @id, @syntax, @details = name, id, syntax, details
  @events = Array(events)
  description description if description
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



2
3
4
# File 'app/models/showcase/sample.rb', line 2

def details
  @details
end

#eventsObject (readonly)

Returns the value of attribute events.



2
3
4
# File 'app/models/showcase/sample.rb', line 2

def events
  @events
end

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'app/models/showcase/sample.rb', line 2

def id
  @id
end

#instrumentedObject (readonly)

Returns the value of attribute instrumented.



3
4
5
# File 'app/models/showcase/sample.rb', line 3

def instrumented
  @instrumented
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'app/models/showcase/sample.rb', line 2

def name
  @name
end

#renderedObject (readonly)

Returns the value of attribute rendered.



3
4
5
# File 'app/models/showcase/sample.rb', line 3

def rendered
  @rendered
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'app/models/showcase/sample.rb', line 3

def source
  @source
end

Instance Method Details

#consume(&block) ⇒ Object



25
26
27
28
# File 'app/models/showcase/sample.rb', line 25

def consume(&block)
  render(&block)
  extract_source(&block)
end

#description(content = nil, &block) ⇒ Object



12
13
14
15
# File 'app/models/showcase/sample.rb', line 12

def description(content = nil, &block)
  @description = content || @view_context.capture(&block) if content || block_given?
  @description
end

#evaluate(&block) ⇒ Object



17
18
19
20
21
22
23
# File 'app/models/showcase/sample.rb', line 17

def evaluate(&block)
  if block.arity.zero?
    consume(&block)
  else
    @view_context.capture(self, &block)
  end
end

#extract_source(&block) ⇒ Object



38
39
40
41
# File 'app/models/showcase/sample.rb', line 38

def extract_source(&block)
  source = extract_source_block_via_matched_indentation_from(*block.source_location)
  @source = @view_context.instance_exec(source, @syntax, &Showcase.sample_renderer)
end

#render(&block) ⇒ Object



30
31
32
33
34
35
36
# File 'app/models/showcase/sample.rb', line 30

def render(&block)
  # TODO: Remove `is_a?` check when Rails 6.1 support is dropped.
  assigns = proc { @instrumented = _1 if _1.is_a?(ActiveSupport::Notifications::Event) }
  ActiveSupport::Notifications.subscribed(assigns, "render_partial.action_view") do
    @rendered = @view_context.capture(&block)
  end
end