Created
May 9, 2014 04:46
-
-
Save wasnotrice/568a64002b4207e1f881 to your computer and use it in GitHub Desktop.
Alternate ways of adding style_with
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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