Module: Rugl::Dynamic

Defined in:
lib/rugl/dynamic.rb

Defined Under Namespace

Classes: Base, ContextVar, Prop, ThisVar

Class Method Summary collapse

Class Method Details

.dynamic?(value) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rugl/dynamic.rb', line 32

def dynamic?(value)
  value.is_a?(Base) || value.is_a?(Proc)
end

.resolve(value, context:, props:, this_obj: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rugl/dynamic.rb', line 36

def resolve(value, context:, props:, this_obj: nil)
  case value
  when Prop
    fetch_key!(props, value.name, scope: "prop")
  when ContextVar
    fetch_key!(context, value.name, scope: "context")
  when ThisVar
    raise CommandError, "Missing this object" if this_obj.nil?

    fetch_key!(this_obj, value.name, scope: "this")
  when Proc
    resolve_proc(value, context: context, props: props)
  when Array
    value.map { |entry| resolve(entry, context: context, props: props, this_obj: this_obj) }
  when Hash
    value.each_with_object({}) do |(key, entry), memo|
      memo[key] = resolve(entry, context: context, props: props, this_obj: this_obj)
    end
  else
    value
  end
end