Skip to content

Instantly share code, notes, and snippets.

@nathankleyn
Created February 6, 2012 11:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathankleyn/1751725 to your computer and use it in GitHub Desktop.
Save nathankleyn/1751725 to your computer and use it in GitHub Desktop.
Gist #11 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
attr_reader :selector, :properties
def initialize(selector, properties = {})
@selector = selector.dup.freeze
@properties = properties.dup.freeze
end
def set(key, value = nil)
new_properties = if key.is_a?(Hash)
key
elsif !value.nil?
{
key => value
}
else
raise "Either provide a Hash of values, or a key and value."
end
self.class.new(self.selector, self.properties.merge(new_properties))
end
def to_s
serialised_properties = self.properties.inject([]) do |acc, (k, v)|
acc + ["#{k}: #{v}"]
end
"#{self.selector} { #{serialised_properties.join("; ") } }"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment