Module: Quince

Defined in:
lib/quince.rb,
lib/quince/types.rb,
lib/quince/config.rb,
lib/quince/sinatra.rb,
lib/quince/version.rb,
lib/quince/callback.rb,
lib/quince/component.rb,
lib/quince/serialiser.rb,
lib/quince/struct/typed.rb,
lib/quince/struct/untyped.rb,
lib/quince/singleton_methods.rb,
lib/quince/html_tag_components.rb,
lib/quince/attributes_by_element.rb

Defined Under Namespace

Modules: HtmlTagComponents, Types Classes: Callback, Component, Config, Serialiser, SinatraApp, Struct, TypedStruct

Constant Summary collapse

VERSION =
"0.6.1"

Class Method Summary collapse

Class Method Details

.define_constructor(const, constructor_name = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/quince/singleton_methods.rb', line 3

def define_constructor(const, constructor_name = nil)
  if const.name
    parts = const.name.split("::")
    parent_namespace = Object.const_get(parts[0...-1].join("::")) if parts.length > 1
    constructor_name ||= parts.last
  end
  constructor_name ||= const.to_s

  HtmlTagComponents.instance_eval do
    mthd = lambda do |*children, **props, &block_children|
      new_props = {
        **props,
        Quince::Component::PARENT_SELECTOR_ATTR => __id,
      }
      const.create(*children, **new_props, &block_children)
    end

    if parent_namespace
      parent_namespace.instance_exec do
        define_method(constructor_name, &mthd)
      end
    else
      define_method(constructor_name, &mthd)
    end
  end
end

.load_config!Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/quince.rb', line 17

def self.load_config!
  Quince::Config.base
  app_dir = File.dirname(File.expand_path($0))
  conf = Pathname(File.join(app_dir, "config.rb"))
  require conf.to_s if conf.exist?

  rack_env = ENV["RACK_ENV"]
  if Quince::Config.respond_to? rack_env
    Quince::Config.send rack_env
  end
end

.to_html(component) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/quince/singleton_methods.rb', line 30

def to_html(component)
  output = component

  until output.is_a? String
    case output
    when Array
      output = output.map { |c| to_html(c) }.join
    when String
      break
    when Proc
      output = to_html(output.call)
    when NilClass
      output = ""
    when Component
      tmp = output
      render_with = output.instance_variable_get(:@render_with) || :render
      output = output.send render_with
      case render_with
      when :render
        if output.is_a?(Array)
          raise "#render in #{tmp.class} should not return multiple elements. Consider wrapping it in a div"
        end
      else
        internal = Quince::Serialiser.serialise tmp
        updated_state = CGI.escapeHTML(internal).to_json
        selector = tmp.instance_variable_get :@state_container
        event = tmp.instance_variable_get :@callback_event

        scr = to_html(HtmlTagComponents::Script.create(<<~JS, type: "text/javascript"))
          var stateContainer = document.querySelector(`#{selector}`);
          stateContainer.dataset.quOn#{event}State = #{updated_state};
        JS
        output = output.render if output.is_a?(Component)

        output += (output.is_a?(String) ? scr : [scr])
      end
    else
      raise "don't know how to render #{output.class} (#{output.inspect})"
    end
  end

  output
end