Sha256: 177f95339190cc5e0a3e86c345490b2c36e97e2900c4344a3e2076c69b26d493

Contents?: true

Size: 1.05 KB

Versions: 38

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 Twitter
  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

38 entries across 38 versions & 15 rubygems

Version Path
anthonycrumley-twitter-0.3.8 lib/twitter/easy_class_maker.rb
baron-twitter-0.4.2 lib/twitter/easy_class_maker.rb
billymeltdown-twitter-0.3.8.1 lib/twitter/easy_class_maker.rb
billymeltdown-twitter-0.4.2 lib/twitter/easy_class_maker.rb
billymeltdown-twitter-0.4.3 lib/twitter/easy_class_maker.rb
drnic-twitter-0.4.4.1 lib/twitter/easy_class_maker.rb
dschn-twitter-0.3.7.1 lib/twitter/easy_class_maker.rb
dschn-twitter-0.3.7.2 lib/twitter/easy_class_maker.rb
dschn-twitter-0.4.1.1 lib/twitter/easy_class_maker.rb
dschn-twitter-0.4.1.2 lib/twitter/easy_class_maker.rb
dschn-twitter-0.4.1.3 lib/twitter/easy_class_maker.rb
dschn-twitter-0.4.1 lib/twitter/easy_class_maker.rb
dustin-twitter-0.3.2.1 lib/twitter/easy_class_maker.rb
dustin-twitter-0.3.2.2 lib/twitter/easy_class_maker.rb
dustin-twitter-0.3.7 lib/twitter/easy_class_maker.rb
gilesbowkett-gilesbowkett-twitter-0.4.4 lib/twitter/easy_class_maker.rb
gilesbowkett-twitter-0.4.3 lib/twitter/easy_class_maker.rb
gilesbowkett-twitter-0.4.5 lib/twitter/easy_class_maker.rb
handcrafted-twitter-0.4.0 lib/twitter/easy_class_maker.rb
handcrafted-twitter-0.4.2 lib/twitter/easy_class_maker.rb