Sha256: f6a6870384ec04d28b5933d7d401576638470624749528962d4fc567e059b8f2
Contents?: true
Size: 1.19 KB
Versions: 1
Compression:
Stored size: 1.19 KB
Contents
# DefinedBy > Advanced DSL and Dynamic class generation for Ruby ## When do I is this? - If you want to use a DSL to cut down on your code base - If you need that DSL to create classes - If you have a base class (e.g. `Widget`) and want to subclass it dozens of times but it's overkill to create separate classes for it manually. - Define template classes (default values for subclasses) ## All at once class Widget class << self def define(&block) DefinedBy::DSL(&block).map do |key, value, dsl_block| define_class(key, self).new(:name => key.to_s, :description => value) end end end attr_accessor :name, :description def initialize(attributes = {}) attributes.each do |key, value| self.send("#{key.to_s}=", value) end end end def Widget(&block) Widget.define(&block) end widgets = Widget do widget_a "This widget does X" widget_b "This does Y" end puts widgets.inspect #=> [ #<WidgetA:0x5c3348 @description="This widget does X", @name="widget_a">, #<WidgetB:0x5c2970 @description="This does Y", @name="widget_b"> ]
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
defined-by-0.0.5 | README.markdown |