Skip to content

Instantly share code, notes, and snippets.

@wasnotrice
Created May 9, 2014 04:46
Show Gist options
  • Select an option

  • Save wasnotrice/568a64002b4207e1f881 to your computer and use it in GitHub Desktop.

Select an option

Save wasnotrice/568a64002b4207e1f881 to your computer and use it in GitHub Desktop.
Alternate ways of adding style_with
module Style
def style
# Special style handling
@style
end
end
module StyleWith
def style_with(*styles)
styles.map(&:to_sym).each do |style|
define_method style do
@style[style]
end
define_method "#{style}=" do |new_style|
@style[style] = new_style
end
end
end
end
class Arc
include Style
extend StyleWith
style_with :wedge, :stroke, :fill
def initialize(style = {})
@style = style
end
end
a = Arc.new
a.wedge = 100
a.wedge
#=> 100
module Style
def style
# Special style handling
@style
end
module StyleWith
def style_with(*styles)
styles.map(&:to_sym).each do |style|
define_method style do
@style[style]
end
define_method "#{style}=" do |new_style|
@style[style] = new_style
end
end
end
end
def self.included(klass)
klass.extend StyleWith
end
end
class Arc
include Style
style_with :wedge, :stroke, :fill
def initialize(style = {})
@style = style
end
end
a = Arc.new
a.wedge = 100
a.wedge
#=> 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment