Class: ExpressTemplates::Components::Base

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

Overview

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

Direct Known Subclasses

Configurable

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



27
28
29
30
31
32
33
# File 'lib/express_templates/components/base.rb', line 27

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

Class Method Details

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



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/express_templates/components/base.rb', line 51

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_and_class(method_name, klass) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/express_templates/components/base.rb', line 18

def self.builder_method_and_class(method_name, klass)
  Arbre::Element::BuilderMethods.class_eval <<-EOF, __FILE__, __LINE__
    def #{method_name}(*args, &block)
      insert_tag ::#{klass.name}, *args, &block
    end
  EOF
  # puts "added #{method_name} -> #{klass.name}"
end

.builder_method_nameObject



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

def self.builder_method_name
  to_s.demodulize.underscore
end

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



35
36
37
# File 'lib/express_templates/components/base.rb', line 35

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

.descendantsObject



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

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



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

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

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  builder_method_and_class subclass.builder_method_name, subclass
end

.tag(tag) ⇒ Object

Override the tag_name method for other than <div>



40
41
42
# File 'lib/express_templates/components/base.rb', line 40

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

Instance Method Details

#build(*args, &block) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/express_templates/components/base.rb', line 63

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

#builder_method_nameObject



83
84
85
# File 'lib/express_templates/components/base.rb', line 83

def builder_method_name
  self.class.builder_method_name
end

#resourceObject



71
72
73
# File 'lib/express_templates/components/base.rb', line 71

def resource
  helpers.resource
end