Class: CafeCar::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/cafe_car/component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, name, *args, **attributes, &block) ⇒ Component

Returns a new instance of Component.



58
59
60
61
62
63
64
65
66
67
# File 'lib/cafe_car/component.rb', line 58

def initialize(template, name, *args, **attributes, &block)
  @template   = template
  @names      = [ *name ].map(&:underscore)
  @flags      = args.extract! { _1.is_a? Symbol }
  @args       = args.flatten.compact_blank
  @attributes = attributes.with_defaults!(attribute_defaults)
  @children   = attributes.extract_if! { _1 =~ /^[A-Z]\w*$/ }
  @block      = block
  extract_options!(@attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/cafe_car/component.rb', line 147

def method_missing(name, ...)
  if name =~ /^[A-Z]/
    child(name, ...)
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



48
49
50
# File 'lib/cafe_car/component.rb', line 48

def attributes
  @attributes
end

#flagsObject (readonly)

Returns the value of attribute flags.



48
49
50
# File 'lib/cafe_car/component.rb', line 48

def flags
  @flags
end

#optionsObject (readonly)

Returns the value of attribute options.



48
49
50
# File 'lib/cafe_car/component.rb', line 48

def options
  @options
end

Class Method Details

.method_missing(name, v) ⇒ Object



44
45
46
# File 'lib/cafe_car/component.rb', line 44

def self.method_missing(name, v)
  attribute_defaults[name] = v
end

Instance Method Details

#+(o) ⇒ Object



130
# File 'lib/cafe_car/component.rb', line 130

def +(o)  = safe_join([ self, o ])

#<<(o) ⇒ Object



131
# File 'lib/cafe_car/component.rb', line 131

def <<(o) = @template.concat(o)

#ancestor_href?Boolean

Returns:

  • (Boolean)


78
# File 'lib/cafe_car/component.rb', line 78

def ancestor_href? = options[:href]&.then { @template.ancestor_href?(_1) }

#blank?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/cafe_car/component.rb', line 109

def blank?
  @block and body and content.blank? || !content.match?(/^.*?[^<\s]/) || content.gsub(/<!--.*?-->/, "").blank?
end

#bodyObject



113
114
115
# File 'lib/cafe_car/component.rb', line 113

def body
  @body ||= context { partial? ? render_partial : content }
end

#child(name) ⇒ Object



142
143
144
145
# File 'lib/cafe_car/component.rb', line 142

def child(name, ...)
  c = self.class.try { const_defined?(name) ? const_get(name) : Component }
  c.new(@template, [ *@names, name ], ...)
end

#childrenObject



105
106
107
# File 'lib/cafe_car/component.rb', line 105

def children
  @children.map { |name, content| send(name) { content } }
end

#class_nameObject



95
96
97
98
99
# File 'lib/cafe_car/component.rb', line 95

def class_name
  @template.ui_class(class_names, *flags, *options[:class], options[:tag].to_s => href?,
                                                    current: current_href?,
                                                    ancestor: ancestor_href? && !current_href?)
end

#class_namesObject



93
# File 'lib/cafe_car/component.rb', line 93

def class_names = @names.map(&:camelize)

#contentObject



117
118
119
# File 'lib/cafe_car/component.rb', line 117

def content
  @content ||= safe_join [ *children, *@args, *(capture(self, &@block) if @block) ]
end

#contextObject



121
122
123
# File 'lib/cafe_car/component.rb', line 121

def context(&)
  href? ? @template.context(:a, &) : capture(&)
end

#current_href?Boolean

Returns:

  • (Boolean)


77
# File 'lib/cafe_car/component.rb', line 77

def current_href? = options[:href]&.then { @template.current_href?(_1, check_parameters: true) }

#dataObject



80
81
82
# File 'lib/cafe_car/component.rb', line 80

def data
  super.merge({ tip: }.compact_blank)
end

#hrefObject



73
74
75
# File 'lib/cafe_car/component.rb', line 73

def href
  @template.href_for(super) if href?
end

#href?Boolean

Returns:

  • (Boolean)


71
# File 'lib/cafe_car/component.rb', line 71

def href?    = super && !context?(:a) && !current_href?

#html_safe?Boolean

Returns:

  • (Boolean)


133
# File 'lib/cafe_car/component.rb', line 133

def html_safe? = true

#nameObject



69
# File 'lib/cafe_car/component.rb', line 69

def name     = @names.last

#partial?Boolean

Returns:

  • (Boolean)


84
# File 'lib/cafe_car/component.rb', line 84

def partial?     = @template.partial?(partial_name)

#partial_nameObject



85
# File 'lib/cafe_car/component.rb', line 85

def partial_name = "ui/" + @names.join("_")

#render_partialObject



87
88
89
# File 'lib/cafe_car/component.rb', line 87

def render_partial
  render(partial_name, options:, flags:, name => self, **options) { content }
end

#selectorObject



91
# File 'lib/cafe_car/component.rb', line 91

def selector = class_names.join(?_).then { ?. + _1 }

#tagObject



70
# File 'lib/cafe_car/component.rb', line 70

def tag      = href? ? :a : super

#to_htmlObject



137
138
139
140
# File 'lib/cafe_car/component.rb', line 137

def to_html
  return "" if blank?
  wrapper { body }
end

#to_sObject



135
# File 'lib/cafe_car/component.rb', line 135

def to_s = to_html

#wrapperObject



125
126
127
# File 'lib/cafe_car/component.rb', line 125

def wrapper(&)
  @template.(tag, **attributes, &)
end

#~@Object



129
# File 'lib/cafe_car/component.rb', line 129

def ~@    = @template.concat(self)