-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
context.rb
55 lines (44 loc) · 1.14 KB
/
context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require "dry/equalizer"
module Dry
module View
class Context
include Dry::Equalizer(:_options)
attr_reader :_rendering, :_options
def initialize(rendering: nil, **options)
@_rendering = rendering
@_options = options
end
def for_rendering(rendering)
return self if rendering == self._rendering
self.class.new(**_options.merge(rendering: rendering))
end
def rendering?
!!_rendering
end
def self.decorate(*names, **options)
mod = DecoratedAttributes.new(names) do
names.each do |name|
define_method name do
attribute = super()
if rendering? && attribute
_rendering.part(name, attribute, **options)
else
attribute
end
end
end
end
prepend mod
end
class DecoratedAttributes < Module
def initialize(names, &block)
@names = names
super(&block)
end
def inspect
%(#<#{self.class.name}#{@names.inspect}>)
end
end
end
end
end