Skip to content

Instantly share code, notes, and snippets.

@bradgessler
Created August 18, 2022 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradgessler/8ef267e88c4c89f068f66b981f61db5a to your computer and use it in GitHub Desktop.
Save bradgessler/8ef267e88c4c89f068f66b981f61db5a to your computer and use it in GitHub Desktop.
A class-base DSL for Ruby asset components
class Komponent
class << self
attr_reader :css_block, :html_block, :script_block
def css(value=nil ,&block)
@css_block = Proc.new { value || block.call }
end
def html(value=nil ,&block)
@html_block = Proc.new { value || block.call }
end
def script(value=nil ,&block)
@script_block = Proc.new { value || block.call }
end
end
def html
eval("\"" + self.class.html_block.call + "\"")
end
def css
eval("\"" + self.class.css_block.call + "\"")
end
end
class Headline < Komponent
css %q{
h1 {
color: #{highlight};
}
}
html %q{
<h1>Hi #{@name}, its #{time}</h1>
}
def initialize(name:)
@name = name
end
def time
Time.now
end
def highlight
"green"
end
end
component = Headline.new(name: "Brad")
p component.html
p component.css
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment