Sha256: ab941a912ff086af85ad6b9ab2249e73007bfc3ef3531bbc5b55200fdf37fdc3

Contents?: true

Size: 628 Bytes

Versions: 2

Compression:

Stored size: 628 Bytes

Contents

class Widget
  class << self
    def define(&block)
      DefinedBy::DSL(&block).map do |key, value, dsl_block|
        define_class(key.to_s.camelize, self).class_eval <<-EOF
          def initialize(attributes = {})
            attributes[:name] ||= #{key.to_s.inspect}
            attributes[:description] ||= #{value.to_s.inspect}
            super(attributes)
          end
        EOF
      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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
defined-by-0.0.6 test/lib/widget.rb
defined-by-0.0.5 test/lib/widget.rb