Class: CafeCar::Component
- Inherits:
-
Object
- Object
- CafeCar::Component
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. { _1.is_a? Symbol }
@args = args.flatten.compact_blank
@attributes = attributes.with_defaults!(attribute_defaults)
@children = attributes. { _1 =~ /^[A-Z]\w*$/ }
@block = block
(@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
#attributes ⇒ Object
Returns the value of attribute attributes.
48
49
50
|
# File 'lib/cafe_car/component.rb', line 48
def attributes
@attributes
end
|
#flags ⇒ Object
Returns the value of attribute flags.
48
49
50
|
# File 'lib/cafe_car/component.rb', line 48
def flags
@flags
end
|
#options ⇒ Object
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
78
|
# File 'lib/cafe_car/component.rb', line 78
def ancestor_href? = options[:href]&.then { @template.ancestor_href?(_1) }
|
#blank? ⇒ 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
|
#body ⇒ Object
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
|
#children ⇒ Object
105
106
107
|
# File 'lib/cafe_car/component.rb', line 105
def children
@children.map { |name, content| send(name) { content } }
end
|
#class_name ⇒ Object
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_names ⇒ Object
93
|
# File 'lib/cafe_car/component.rb', line 93
def class_names = @names.map(&:camelize)
|
#content ⇒ Object
117
118
119
|
# File 'lib/cafe_car/component.rb', line 117
def content
@content ||= safe_join [ *children, *@args, *(capture(self, &@block) if @block) ]
end
|
#context ⇒ Object
121
122
123
|
# File 'lib/cafe_car/component.rb', line 121
def context(&)
href? ? @template.context(:a, &) : capture(&)
end
|
#current_href? ⇒ Boolean
77
|
# File 'lib/cafe_car/component.rb', line 77
def current_href? = options[:href]&.then { @template.current_href?(_1, check_parameters: true) }
|
#data ⇒ Object
80
81
82
|
# File 'lib/cafe_car/component.rb', line 80
def data
super.merge({ tip: }.compact_blank)
end
|
#href ⇒ Object
73
74
75
|
# File 'lib/cafe_car/component.rb', line 73
def href
@template.href_for(super) if href?
end
|
#href? ⇒ Boolean
71
|
# File 'lib/cafe_car/component.rb', line 71
def href? = super && !context?(:a) && !current_href?
|
#html_safe? ⇒ Boolean
133
|
# File 'lib/cafe_car/component.rb', line 133
def html_safe? = true
|
#name ⇒ Object
69
|
# File 'lib/cafe_car/component.rb', line 69
def name = @names.last
|
#partial? ⇒ Boolean
84
|
# File 'lib/cafe_car/component.rb', line 84
def partial? = @template.partial?(partial_name)
|
#partial_name ⇒ Object
85
|
# File 'lib/cafe_car/component.rb', line 85
def partial_name = "ui/" + @names.join("_")
|
#render_partial ⇒ Object
87
88
89
|
# File 'lib/cafe_car/component.rb', line 87
def render_partial
render(partial_name, options:, flags:, name => self, **options) { content }
end
|
#selector ⇒ Object
91
|
# File 'lib/cafe_car/component.rb', line 91
def selector = class_names.join(?_).then { ?. + _1 }
|
#tag ⇒ Object
70
|
# File 'lib/cafe_car/component.rb', line 70
def tag = href? ? :a : super
|
#to_html ⇒ Object
137
138
139
140
|
# File 'lib/cafe_car/component.rb', line 137
def to_html
return "" if blank?
wrapper { body }
end
|
#to_s ⇒ Object
135
|
# File 'lib/cafe_car/component.rb', line 135
def to_s = to_html
|
#wrapper ⇒ Object
125
126
127
|
# File 'lib/cafe_car/component.rb', line 125
def wrapper(&)
@template.content_tag(tag, **attributes, &)
end
|
#~@ ⇒ Object
129
|
# File 'lib/cafe_car/component.rb', line 129
def ~@ = @template.concat(self)
|