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
124 125 126 127 128 |
# File 'lib/solid_objects/actor_view.rb', line 124 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.
137 138 139 |
# File 'lib/solid_objects/actor_view.rb', line 137 def @authorization_context end |
#component_registrations ⇒ Object (readonly)
Returns the value of attribute component_registrations.
137 138 139 |
# File 'lib/solid_objects/actor_view.rb', line 137 def component_registrations @component_registrations end |
#observable_names ⇒ Object (readonly)
Returns the value of attribute observable_names.
137 138 139 |
# File 'lib/solid_objects/actor_view.rb', line 137 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.
137 138 139 |
# File 'lib/solid_objects/actor_view.rb', line 137 def snapshot @snapshot end |
#view_context ⇒ Object (readonly)
Returns the value of attribute view_context.
137 138 139 |
# File 'lib/solid_objects/actor_view.rb', line 137 def view_context @view_context end |
Instance Method Details
#authorize_read!(name) ⇒ void
This method returns an undefined value.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/solid_objects/actor_view.rb', line 158 def (name) = SolidObjects.configuration..call( actor_type: reference.actor_type, actor_id: reference.actor_id, operation: name.to_s, arguments: {}, authorization_context: ) return if raise Unauthorized, "actor state query is not authorized" end |
#batched_components? ⇒ Boolean
109 110 111 |
# File 'lib/solid_objects/actor_view.rb', line 109 def batched_components? component_registrations.any?(&:batch) end |
#component(name, observes: nil, partial: nil, key: nil, locals: {}, refresh: :replace, batch: nil) ⇒ Object
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 92 93 94 95 96 |
# File 'lib/solid_objects/actor_view.rb', line 46 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
236 237 238 239 |
# File 'lib/solid_objects/actor_view.rb', line 236 def component_path_resolver SolidObjects.configuration.component_path_resolver || ComponentPathResolver.new end |
#component_tokens ⇒ Array[String]
99 100 101 |
# File 'lib/solid_objects/actor_view.rb', line 99 def component_tokens component_registrations.map(&:token) end |
#default_partial(component_name) ⇒ String
242 243 244 245 246 247 |
# File 'lib/solid_objects/actor_view.rb', line 242 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
177 178 179 180 181 182 183 |
# File 'lib/solid_objects/actor_view.rb', line 177 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.
217 218 219 220 221 222 223 224 225 |
# File 'lib/solid_objects/actor_view.rb', line 217 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
114 115 116 |
# File 'lib/solid_objects/actor_view.rb', line 114 def morph_components? component_registrations.any?(&:morph?) end |
#normalized_component_name(name) ⇒ String
186 187 188 189 190 191 192 193 |
# File 'lib/solid_objects/actor_view.rb', line 186 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]
196 197 198 199 200 201 202 203 |
# File 'lib/solid_objects/actor_view.rb', line 196 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
172 173 174 |
# File 'lib/solid_objects/actor_view.rb', line 172 def observable?(name) snapshot.actor_class.definition.observables.key?(name.to_sym) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
131 132 133 |
# File 'lib/solid_objects/actor_view.rb', line 131 def respond_to_missing?(name, include_private = false) observable?(name) || super end |
#scalar_observable_names ⇒ Array[String]
104 105 106 |
# File 'lib/solid_objects/actor_view.rb', line 104 def scalar_observable_names observable_names.dup end |
#state ⇒ State
119 120 121 |
# File 'lib/solid_objects/actor_view.rb', line 119 def state snapshot.actor.state end |
#static_component(component_name, partial:) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/solid_objects/actor_view.rb', line 144 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.
206 207 208 209 210 211 212 213 214 |
# File 'lib/solid_objects/actor_view.rb', line 206 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.
228 229 230 231 232 233 |
# File 'lib/solid_objects/actor_view.rb', line 228 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 39 40 41 42 43 |
# File 'lib/solid_objects/actor_view.rb', line 25 def value(name) observable_name = name.to_sym definition = snapshot.actor_class.definition handler = definition.observables[observable_name] raise UnknownMessage, "unknown observable #{name.inspect}" unless handler unless definition.broadcasts_observable_value?(observable_name) raise ArgumentError, "invalidation-only observable #{observable_name.inspect} cannot render as a scalar target" end (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 |