Created
August 18, 2022 16:46
-
-
Save bradgessler/8ef267e88c4c89f068f66b981f61db5a to your computer and use it in GitHub Desktop.
A class-base DSL for Ruby asset components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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