Sha256: 2ee5eec6ee8f107d1f5d960e9119183f3e29ed16451fcb122fd6240b7da1b09b

Contents?: true

Size: 1.05 KB

Versions: 15

Compression:

Stored size: 1.05 KB

Contents

# This is pretty much just a macro for creating a class that allows
# using a block to initialize stuff and to define getters and setters
# really quickly.
module Dreamy
  module EasyClassMaker
    
    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods
      # creates the attributes class variable and creates each attribute's accessor methods
      def attributes(*attrs)
        @@attributes = attrs
        @@attributes.each { |a| attr_accessor a }
      end
      
      # read method for attributes class variable
      def self.attributes; @@attributes end
    end
    
    # allows for any class that includes this to use a block to initialize
    # variables instead of assigning each one seperately
    # 
    # Example: 
    # 
    # instead of...
    # 
    # s = Status.new
    # s.foo = 'thing'
    # s.bar = 'another thing'
    #
    # you can ...
    # 
    # Status.new do |s|
    #   s.foo = 'thing'
    #   s.bar = 'another thing'
    # end
    def initialize
      yield self if block_given?
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
jordan-brough-dreamy-0.5.1 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.1.1 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.1.2 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.2.0 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.2.1 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.2.2 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.2.3 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.2.4 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.3.0 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.4.1 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.5.0 lib/dreamy/easy_class_maker.rb
sant0sk1-dreamy-0.5.1 lib/dreamy/easy_class_maker.rb
dreamy-0.5.3 lib/dreamy/easy_class_maker.rb
dreamy-0.5.2 lib/dreamy/easy_class_maker.rb
dreamy-0.5.1 lib/dreamy/easy_class_maker.rb