Class: CompEx::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/compex/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Base

Returns a new instance of Base.



73
74
75
76
# File 'lib/compex/base.rb', line 73

def initialize(**args)
  @bag = Bag.new(descriptor, **args)
  @extra_hashes = []
end

Class Method Details

.arg(first, *rest) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/compex/base.rb', line 18

def arg(first, *rest)
  var_name = "@#{first}"
  name = first
  define_method(name) { instance_variable_get(var_name).to_s }
  define_method("#{name}=") do |value|
    instance_variable_set(var_name, UnsafeString.new(value))
  end
  descriptor.args.add(name)

  return self if rest.empty?

  warn "CompEx: #{self.name}: arg #{[first, *rest].join(", ")} should be args #{[first, *rest].join(", ")}"
  rest.each { arg(it) }
  self
end

.args(first, *rest) ⇒ Object



13
14
15
16
# File 'lib/compex/base.rb', line 13

def args(first, *rest)
  [first, *rest].each { arg(it) }
  self
end

.descriptorObject



9
10
11
# File 'lib/compex/base.rb', line 9

def descriptor
  @descriptor ||= ComponentDescriptor.new(self)
end

.expose(first, *rest) ⇒ Object



34
35
36
37
# File 'lib/compex/base.rb', line 34

def expose(first, *rest)
  descriptor.exposed.add(first, *rest)
  nil
end

.html(source) ⇒ Object



39
40
41
42
# File 'lib/compex/base.rb', line 39

def html(source)
  descriptor.raw_html = source
  self
end

.inherited(subclass) ⇒ Object



65
66
67
68
# File 'lib/compex/base.rb', line 65

def inherited(subclass)
  super
  CompEx::ComponentRegistry.register(subclass)
end

.js(source) ⇒ Object



49
50
51
52
# File 'lib/compex/base.rb', line 49

def js(source)
  descriptor.raw_js = source
  self
end

.js_class_idObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/compex/base.rb', line 54

def js_class_id
  return @js_class_id if @js_class_id

  class_id = name.split("::").map do |id|
    next id unless id.start_with? "#"

    id.hashed
  end.join
  @js_class_id ||= ["CompEx_", class_id, "_", component_id.upcase].join
end

.style(source) ⇒ Object



44
45
46
47
# File 'lib/compex/base.rb', line 44

def style(source)
  descriptor.raw_style = source
  self
end

Instance Method Details

#defined_argsObject



80
# File 'lib/compex/base.rb', line 80

def defined_args = self.class.descriptor.args

#descriptorObject



71
# File 'lib/compex/base.rb', line 71

def descriptor = self.class.descriptor

#exposedObject



78
# File 'lib/compex/base.rb', line 78

def exposed = self.class.descriptor.exposed

#render_html(children: nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/compex/base.rb', line 82

def render_html(children: nil)
  self.class.descriptor.html.render do |v|
    case v
    when :slot
      children
    when Hash
      inst = v[:component].new(**v[:args])
      ret = inst.render_html(children: v[:children])
      @extra_hashes << inst.required_hashes
      ret

    when Proc
      @bag.instance_exec(&v)
    else
      raise "Unexpected value of type #{v.class} provided by Renderer#render"
    end
  end
end

#required_hashesObject



101
102
103
104
105
106
# File 'lib/compex/base.rb', line 101

def required_hashes
  [descriptor.required_hashes, @bag.required_hashes, @extra_hashes]
    .flatten
    .compact
    .uniq
end