Module: Stimul8::Component::Core

Extended by:
ActiveSupport::Concern
Defined in:
lib/stimul8/component/core.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



135
136
137
# File 'lib/stimul8/component/core.rb', line 135

def == other
  other.component_id == component_id
end

#component(component_class, properties = {}, &contents) ⇒ Object Also known as: c

standard:disable Style/ArgumentsForwarding



97
98
99
# File 'lib/stimul8/component/core.rb', line 97

def component(component_class, properties = {}, &contents) # standard:disable Style/ArgumentsForwarding
  Stimul8::Component.component(component_class, properties, &contents) # standard:disable Style/ArgumentsForwarding
end

#component_classObject



77
78
79
# File 'lib/stimul8/component/core.rb', line 77

def component_class
  @component_class ||= self.class.name.underscore.dasherize.gsub("/", "--")
end

#component_idObject



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

def component_id
  @component_id ||= SecureRandom.uuid
end

#contentsObject



89
90
91
# File 'lib/stimul8/component/core.rb', line 89

def contents
  @contents&.call.to_s
end

#contextObject



93
94
95
# File 'lib/stimul8/component/core.rb', line 93

def context
  @context.freeze
end

#css_classObject



81
82
83
# File 'lib/stimul8/component/core.rb', line 81

def css_class
  [component_class, @attributes[:class]].compact.join(" ")
end

#expanded_attributesObject



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stimul8/component/core.rb', line 118

def expanded_attributes
  return @expanded_attributes unless @expanded_attributes.nil?

  @expanded_attributes = @attributes.except(:class)
  @expanded_attributes[:data] ||= {}
  controller = ["stimul8", @expanded_attributes[:data][:controller]].compact.join(" ")
  @expanded_attributes[:data][:controller] = controller
  @expanded_attributes[:data][:stimul8_class_name_value] = self.class.name
  @expanded_attributes = @expanded_attributes.transform_keys do |key|
    key.to_s.dasherize.to_sym
  end
end

#render?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/stimul8/component/core.rb', line 131

def render?
  self.class.render_if_block.nil? || instance_eval(&self.class.render_if_block)
end

#stylesheetObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/stimul8/component/core.rb', line 106

def stylesheet
  return @stylesheet unless @stylesheet.blank?
  @stylesheet = self.class.styles.map do |selector, rules|
    selection_rule = ".#{component_class} #{selector} {\n"
    rules.each do |rule|
      selection_rule += "  #{rule}\n"
    end
    selection_rule += "}"
    selection_rule
  end.join("\n")
end

#tag_nameObject



102
103
104
# File 'lib/stimul8/component/core.rb', line 102

def tag_name
  @tag_name ||= self.class.tag_name || :div
end

#templateObject



85
86
87
# File 'lib/stimul8/component/core.rb', line 85

def template
  self.class.template_block
end

#to_htmlObject



64
65
66
67
68
69
70
71
# File 'lib/stimul8/component/core.rb', line 64

def to_html
  return "" unless render?
  Markaby::Builder.new({}, self) do
    tag! tag_name, id: component_id, class: css_class, **expanded_attributes do
      instance_eval(&template)
    end
  end.to_s
end