Class: ExpressTemplates::Components::Base

Inherits:
Arbre::Component
  • Object
show all
Defined in:
lib/express_templates/components/base.rb

Overview

ExpressTemplates::Components::Base is the base class for ExpressTemplates view components.

Subclasses can attach hooks.

An ET component may adopt other capabilities (e.g. support RESTful options).

Constant Summary collapse

MAP =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



66
67
68
69
70
71
72
# File 'lib/express_templates/components/base.rb', line 66

def initialize(*)
  super
  _default_attributes.each do |name, value|
    set_attribute(name, value)
  end
  add_class _default_classes
end

Class Method Details

.abstract_component(value = true) ⇒ Object



29
30
31
# File 'lib/express_templates/components/base.rb', line 29

def self.abstract_component(value = true)
  @is_abstract = value
end

.abstract_component?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/express_templates/components/base.rb', line 33

def self.abstract_component?
  @is_abstract
end

.before_build(proc_or_symbol = nil, exclusive: false, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/express_templates/components/base.rb', line 90

def self.before_build(proc_or_symbol = nil, exclusive: false, &block)
  hook = (proc_or_symbol || block)
  unless hook.kind_of?(Symbol) or hook.respond_to?(:call)
    raise "before_build requires symbol (method_name), proc or block"
  end
  if exclusive
    self.before_build_hooks = [hook]
  else
    self.before_build_hooks += [hook]
  end
end

.builder_method(name) ⇒ Object



115
116
117
# File 'lib/express_templates/components/base.rb', line 115

def self.builder_method(name)
  builder_method_and_class name, self
end

.builder_method_and_class(method_name, klass) ⇒ Object

Defines an instance method using Arbre’s BuilderMethods

Parameters:

  • String

    method_name (component)

  • Class

    klass



56
57
58
59
60
61
62
63
64
# File 'lib/express_templates/components/base.rb', line 56

def self.builder_method_and_class(method_name, klass)
  ExpressTemplates::Components::Registry[method_name] = klass
  Arbre::Element::BuilderMethods.class_eval <<-EOF, __FILE__, __LINE__
    def #{method_name}(*args, &block)             # def checkbox(*args, &block)
      insert_tag ::#{klass.name}, *args, &block   #   insert_tag ::Checkbox, *args, &block
    end                                           # end
  EOF
  self.builder_method_name = method_name
end

.contains(proc = nil, &block) ⇒ Object



74
75
76
# File 'lib/express_templates/components/base.rb', line 74

def self.contains(proc = nil, &block)
  define_method(:_build_body, &(proc || block))
end

.descendantsObject



119
120
121
# File 'lib/express_templates/components/base.rb', line 119

def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

.has_attributes(attribs) ⇒ Object

Provide default attributes for the enclosing tag of the component



85
86
87
88
# File 'lib/express_templates/components/base.rb', line 85

def self.has_attributes(attribs)
  self._default_classes = attribs.delete(:class)
  _default_attributes.merge!(attribs)
end

.inherited(subclass) ⇒ Object



110
111
112
113
# File 'lib/express_templates/components/base.rb', line 110

def self.inherited(subclass)
  method_name = subclass.to_s.demodulize.underscore
  subclass.builder_method_and_class(method_name, subclass)
end

.require_parent(component) ⇒ Object



42
43
44
45
# File 'lib/express_templates/components/base.rb', line 42

def self.require_parent(component)
  raise "must pass a sublcass of ExpressTemplates::Components::Base" if !component.kind_of? ExpressTemplates::Components::Base
  @parent_component = component
end

.required_parentObject



47
48
49
# File 'lib/express_templates/components/base.rb', line 47

def self.required_parent
  @parent_component
end

.tag(tag) ⇒ Object

Override the tag_name method for other than <div>



79
80
81
# File 'lib/express_templates/components/base.rb', line 79

def self.tag(tag)
  define_method(:tag_name) { tag }
end

Instance Method Details

#build(*args, &block) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/express_templates/components/base.rb', line 102

def build(*args, &block)
  _extract_class!(args)
  _run_before_build_hooks
  super(*args) {
    _build_body(&block) if respond_to?(:_build_body)
  }
end