Class: SolidObjects::ActorView
- Inherits:
-
Object
- Object
- SolidObjects::ActorView
- Defined in:
- lib/solid_objects/actor_view.rb,
sig/generated/lib/solid_objects/actor_view.rbs
Instance Attribute Summary collapse
-
#authorization_context ⇒ Object
readonly
Returns the value of attribute authorization_context.
-
#component_registrations ⇒ Object
readonly
Returns the value of attribute component_registrations.
-
#observable_names ⇒ Object
readonly
Returns the value of attribute observable_names.
- #reference ⇒ Object readonly
-
#snapshot ⇒ Object
readonly
Returns the value of attribute snapshot.
-
#view_context ⇒ Object
readonly
Returns the value of attribute view_context.
Instance Method Summary collapse
- #authorize_read!(name) ⇒ void
- #batched_components? ⇒ Boolean
- #component(name, observes: nil, partial: nil, key: nil, locals: {}, refresh: :replace, batch: nil) ⇒ Object
- #component_path_resolver ⇒ Proc
- #component_tokens ⇒ Array[String]
- #default_partial(component_name) ⇒ String
- #display_value(value) ⇒ String
- #ensure_unique_component!(registration) ⇒ void
-
#initialize(reference:, view_context:, authorization_context:) ⇒ ActorView
constructor
A new instance of ActorView.
- #method_missing(name, *arguments, **keywords) ⇒ Object
- #morph_components? ⇒ Boolean
- #normalized_component_name(name) ⇒ String
- #normalized_dependencies(observes) ⇒ Array[String]
- #observable?(name) ⇒ Boolean
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #scalar_observable_names ⇒ Array[String]
- #state ⇒ State
- #static_component(component_name, partial:) ⇒ Object
- #validate_dependencies!(dependencies) ⇒ void
- #validate_static_options!(key:, locals:, refresh:, batch:) ⇒ void
- #value(name) ⇒ Object
Constructor Details
#initialize(reference:, view_context:, authorization_context:) ⇒ ActorView
Returns a new instance of ActorView.
15 16 17 18 19 20 21 22 |
# File 'lib/solid_objects/actor_view.rb', line 15 def initialize(reference:, view_context:, authorization_context:) @reference = reference @view_context = view_context @authorization_context = @snapshot = ActorSnapshot.new(reference) @component_registrations = [] @observable_names = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments, **keywords) ⇒ Object
119 120 121 122 123 |
# File 'lib/solid_objects/actor_view.rb', line 119 def method_missing(name, *arguments, **keywords) return value(name) if arguments.empty? && keywords.empty? && observable?(name) super end |
Instance Attribute Details
#authorization_context ⇒ Object (readonly)
Returns the value of attribute authorization_context.
132 133 134 |
# File 'lib/solid_objects/actor_view.rb', line 132 def @authorization_context end |
#component_registrations ⇒ Object (readonly)
Returns the value of attribute component_registrations.
132 133 134 |
# File 'lib/solid_objects/actor_view.rb', line 132 def component_registrations @component_registrations end |
#observable_names ⇒ Object (readonly)
Returns the value of attribute observable_names.
132 133 134 |
# File 'lib/solid_objects/actor_view.rb', line 132 def observable_names @observable_names end |
#reference ⇒ Object (readonly)
12 13 14 |
# File 'lib/solid_objects/actor_view.rb', line 12 def reference @reference end |
#snapshot ⇒ Object (readonly)
Returns the value of attribute snapshot.
132 133 134 |
# File 'lib/solid_objects/actor_view.rb', line 132 def snapshot @snapshot end |
#view_context ⇒ Object (readonly)
Returns the value of attribute view_context.
132 133 134 |
# File 'lib/solid_objects/actor_view.rb', line 132 def view_context @view_context end |
Instance Method Details
#authorize_read!(name) ⇒ void
This method returns an undefined value.
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/solid_objects/actor_view.rb', line 153 def (name) = SolidObjects.configuration..call( actor_type: reference.actor_type, actor_id: reference.actor_id, message_name: name.to_s, arguments: {}, authorization_context: ) return if raise Unauthorized, "actor state query is not authorized" end |
#batched_components? ⇒ Boolean
104 105 106 |
# File 'lib/solid_objects/actor_view.rb', line 104 def batched_components? component_registrations.any?(&:batch) end |
#component(name, observes: nil, partial: nil, key: nil, locals: {}, refresh: :replace, batch: nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/solid_objects/actor_view.rb', line 41 def component( name, observes: nil, partial: nil, key: nil, locals: {}, refresh: :replace, batch: nil ) component_name = normalized_component_name(name) unless observes (key:, locals:, refresh:, batch:) return static_component(component_name, partial:) end if partial raise ArgumentError, "reactive components resolve partials by actor and component name" end dependencies = normalized_dependencies(observes) validate_dependencies!(dependencies) refresh_path = component_path_resolver.call(view_context:) registration = ComponentRegistration.issue( reference:, component_name:, component_key: key, dependencies:, locals:, refresh_method: refresh, snapshot:, refresh_path:, batch: batch&.to_s ) ensure_unique_component!(registration) rendered = ComponentRenderer.new( snapshot:, registration:, view_context:, authorization_context: ).call component_registrations << registration view_context.content_tag( :"turbo-frame", rendered, id: registration.dom_id, data: { solid_objects_revision: "#{snapshot.instance_id}:#{snapshot.revision}", solid_objects_refresh: registration.refresh_method } ) end |
#component_path_resolver ⇒ Proc
231 232 233 234 |
# File 'lib/solid_objects/actor_view.rb', line 231 def component_path_resolver SolidObjects.configuration.component_path_resolver || ComponentPathResolver.new end |
#component_tokens ⇒ Array[String]
94 95 96 |
# File 'lib/solid_objects/actor_view.rb', line 94 def component_tokens component_registrations.map(&:token) end |
#default_partial(component_name) ⇒ String
237 238 239 240 241 242 |
# File 'lib/solid_objects/actor_view.rb', line 237 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
172 173 174 175 176 177 178 |
# File 'lib/solid_objects/actor_view.rb', line 172 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 |
#ensure_unique_component!(registration) ⇒ void
This method returns an undefined value.
212 213 214 215 216 217 218 219 220 |
# File 'lib/solid_objects/actor_view.rb', line 212 def ensure_unique_component!(registration) return unless component_registrations.any? do |existing_registration| existing_registration.dom_id == registration.dom_id end raise ArgumentError, "component #{registration.component_name.inspect} with key " \ "#{registration.component_key.inspect} is already rendered in this solid_object scope" end |
#morph_components? ⇒ Boolean
109 110 111 |
# File 'lib/solid_objects/actor_view.rb', line 109 def morph_components? component_registrations.any?(&:morph?) end |
#normalized_component_name(name) ⇒ String
181 182 183 184 185 186 187 188 |
# File 'lib/solid_objects/actor_view.rb', line 181 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 |
#normalized_dependencies(observes) ⇒ Array[String]
191 192 193 194 195 196 197 198 |
# File 'lib/solid_objects/actor_view.rb', line 191 def normalized_dependencies(observes) dependencies = Array(observes).map(&:to_s).uniq if dependencies.empty? raise ArgumentError, "reactive components require at least one observable" end dependencies end |
#observable?(name) ⇒ Boolean
167 168 169 |
# File 'lib/solid_objects/actor_view.rb', line 167 def observable?(name) snapshot.actor_class.definition.observables.key?(name.to_sym) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
126 127 128 |
# File 'lib/solid_objects/actor_view.rb', line 126 def respond_to_missing?(name, include_private = false) observable?(name) || super end |
#scalar_observable_names ⇒ Array[String]
99 100 101 |
# File 'lib/solid_objects/actor_view.rb', line 99 def scalar_observable_names observable_names.dup end |
#state ⇒ State
114 115 116 |
# File 'lib/solid_objects/actor_view.rb', line 114 def state snapshot.actor.state end |
#static_component(component_name, partial:) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/solid_objects/actor_view.rb', line 139 def static_component(component_name, partial:) (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 |
#validate_dependencies!(dependencies) ⇒ void
This method returns an undefined value.
201 202 203 204 205 206 207 208 209 |
# File 'lib/solid_objects/actor_view.rb', line 201 def validate_dependencies!(dependencies) unknown = dependencies.find do |dependency| !snapshot.actor_class.definition.observables.key?(dependency.to_sym) end return unless unknown raise UnknownComponentDependency, "unknown observable dependency #{unknown.inspect} for #{reference.actor_type}" end |
#validate_static_options!(key:, locals:, refresh:, batch:) ⇒ void
This method returns an undefined value.
223 224 225 226 227 228 |
# File 'lib/solid_objects/actor_view.rb', line 223 def (key:, locals:, refresh:, batch:) return if key.nil? && locals.empty? && refresh.to_s == "replace" && batch.nil? raise ArgumentError, "key, locals, and refresh require an observable component" end |
#value(name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/solid_objects/actor_view.rb', line 25 def value(name) observable_name = name.to_sym handler = snapshot.actor_class.definition.observables[observable_name] raise UnknownMessage, "unknown observable #{name.inspect}" unless handler (observable_name) value = snapshot.observable_value(observable_name) observable_names << observable_name.to_s unless observable_names.include?(observable_name.to_s) view_context.content_tag( :span, display_value(value), id: DomIdentity.observable(reference, observable_name) ) end |