Class: SolidObjects::ActorView
- Inherits:
-
Object
- Object
- SolidObjects::ActorView
show all
- Defined in:
- lib/solid_objects/actor_view.rb,
sig/generated/lib/solid_objects/actor_view.rbs
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(reference:, view_context:, authorization_context:) ⇒ ActorView
Returns a new instance of ActorView.
13
14
15
16
17
18
|
# File 'lib/solid_objects/actor_view.rb', line 13
def initialize(reference:, view_context:, authorization_context:)
@reference = reference
@view_context = view_context
@authorization_context = authorization_context
@snapshot = ActorSnapshot.new(reference)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments, **keywords) ⇒ Object
56
57
58
59
60
|
# File 'lib/solid_objects/actor_view.rb', line 56
def method_missing(name, *arguments, **keywords)
return value(name) if arguments.empty? && keywords.empty? && observable?(name)
super
end
|
Instance Attribute Details
#authorization_context ⇒ Object
Returns the value of attribute authorization_context.
69
70
71
|
# File 'lib/solid_objects/actor_view.rb', line 69
def authorization_context
@authorization_context
end
|
#reference ⇒ Object
10
11
12
|
# File 'lib/solid_objects/actor_view.rb', line 10
def reference
@reference
end
|
#snapshot ⇒ Object
Returns the value of attribute snapshot.
69
70
71
|
# File 'lib/solid_objects/actor_view.rb', line 69
def snapshot
@snapshot
end
|
#view_context ⇒ Object
Returns the value of attribute view_context.
69
70
71
|
# File 'lib/solid_objects/actor_view.rb', line 69
def view_context
@view_context
end
|
Instance Method Details
#authorize_read!(name) ⇒ void
This method returns an undefined value.
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/solid_objects/actor_view.rb', line 72
def authorize_read!(name)
authorized = SolidObjects.configuration.authorize_query.call(
actor_type: reference.actor_type,
actor_id: reference.actor_id,
message_name: name.to_s,
arguments: {},
authorization_context:
)
return if authorized
raise Unauthorized, "actor state query is not authorized"
end
|
#component(name, partial: nil) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/solid_objects/actor_view.rb', line 36
def component(name, partial: nil)
component_name = normalized_component_name(name)
authorize_read!(component_name)
rendered = view_context.render(
partial: partial || default_partial(component_name),
locals: { actor: self }
)
view_context.content_tag(
:div,
rendered,
id: DomIdentity.component(reference, component_name)
)
end
|
#default_partial(component_name) ⇒ String
110
111
112
113
114
115
|
# File 'lib/solid_objects/actor_view.rb', line 110
def default_partial(component_name)
actor_name = snapshot.actor_class.name
raise InvalidActor, "anonymous actors must provide an explicit component partial" unless actor_name
"actors/#{actor_name.underscore}/#{component_name}"
end
|
#display_value(value) ⇒ String
91
92
93
94
95
96
97
|
# File 'lib/solid_objects/actor_view.rb', line 91
def display_value(value)
return value if value.is_a?(String)
return value.to_s if value.is_a?(Numeric) || value == true || value == false
return "" if value.nil?
JSON.generate(value)
end
|
#normalized_component_name(name) ⇒ String
100
101
102
103
104
105
106
107
|
# File 'lib/solid_objects/actor_view.rb', line 100
def normalized_component_name(name)
component_name = name.to_s
unless component_name.match?(/\A[a-zA-Z0-9_]+\z/)
raise ArgumentError, "component names may contain only letters, digits, and underscores"
end
component_name
end
|
#observable?(name) ⇒ Boolean
86
87
88
|
# File 'lib/solid_objects/actor_view.rb', line 86
def observable?(name)
snapshot.actor_class.definition.observables.key?(name.to_sym)
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
63
64
65
|
# File 'lib/solid_objects/actor_view.rb', line 63
def respond_to_missing?(name, include_private = false)
observable?(name) || super
end
|
51
52
53
|
# File 'lib/solid_objects/actor_view.rb', line 51
def state
snapshot.actor.state
end
|
#value(name) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/solid_objects/actor_view.rb', line 21
def value(name)
observable_name = name.to_sym
handler = snapshot.actor_class.definition.observables[observable_name]
raise UnknownMessage, "unknown observable #{name.inspect}" unless handler
authorize_read!(observable_name)
value = snapshot.observable_values.fetch(observable_name.to_s)
view_context.content_tag(
:span,
display_value(value),
id: DomIdentity.observable(reference, observable_name)
)
end
|