Class: SolidObjects::ActorView

Inherits:
Object
  • Object
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.

RBS:

  • (reference: Reference, view_context: untyped, authorization_context: untyped) -> void

Parameters:

  • reference: (Reference)
  • view_context: (Object)
  • authorization_context: (Object)


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

RBS:

  • (Symbol, *untyped, **untyped) -> untyped

Parameters:

  • (Symbol)
  • (Object)
  • (Object)

Returns:

  • (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_contextObject (readonly)

Returns the value of attribute authorization_context.

Returns:

  • (Object)


69
70
71
# File 'lib/solid_objects/actor_view.rb', line 69

def authorization_context
  @authorization_context
end

#referenceObject (readonly)

RBS:

  • @reference: Reference

  • @view_context: untyped

  • @authorization_context: untyped

  • @snapshot: ActorSnapshot

Returns:

  • (Object)


10
11
12
# File 'lib/solid_objects/actor_view.rb', line 10

def reference
  @reference
end

#snapshotObject (readonly)

Returns the value of attribute snapshot.

Returns:

  • (Object)


69
70
71
# File 'lib/solid_objects/actor_view.rb', line 69

def snapshot
  @snapshot
end

#view_contextObject (readonly)

Returns the value of attribute view_context.

Returns:

  • (Object)


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.

RBS:

  • (Symbol | String) -> void

Parameters:

  • (Symbol, String)


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

RBS:

  • (Symbol | String, ?partial: String?) -> untyped

Parameters:

  • (Symbol, String)
  • partial: (String, nil) (defaults to: nil)

Returns:

  • (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.(
    :div,
    rendered,
    id: DomIdentity.component(reference, component_name)
  )
end

#default_partial(component_name) ⇒ String

RBS:

  • (String) -> String

Parameters:

  • (String)

Returns:

  • (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

RBS:

  • (untyped) -> String

Parameters:

  • (Object)

Returns:

  • (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

RBS:

  • (Symbol | String) -> String

Parameters:

  • (Symbol, String)

Returns:

  • (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

RBS:

  • (Symbol | String) -> bool

Parameters:

  • (Symbol, String)

Returns:

  • (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

RBS:

  • (Symbol, bool) -> bool

Parameters:

  • (Symbol)
  • (Boolean)

Returns:

  • (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

#stateState

RBS:

  • () -> State

Returns:



51
52
53
# File 'lib/solid_objects/actor_view.rb', line 51

def state
  snapshot.actor.state
end

#value(name) ⇒ Object

RBS:

  • (Symbol | String) -> untyped

Parameters:

  • (Symbol, String)

Returns:

  • (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.(
    :span,
    display_value(value),
    id: DomIdentity.observable(reference, observable_name)
  )
end