Sha256: c01e58f4e946560cbb232dba152383b6d986269ec4bec775e92819504e839063

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

# http://snippets.dzone.com/posts/show/4382

module AttrWithDefault
  
  module ClassMethods
    
    def attr_accessor(*args)
      attrs, attrs_with_defaults = split_for_last_hash(args)
      attrs_with_defaults.each do |name, default|
        attr_reader_with_default name, default
        attr_writer              name
      end
      
      super(*attrs)
    end
    
    def attr_reader(*args)
      attrs, attrs_with_defaults = split_for_last_hash(args)
      attrs_with_defaults.each do |name, default|
        attr_reader_with_default name, default
      end
      
      super(*attrs)
    end
    
  private
    
    def attr_reader_with_default(name, default)
      define_method(name) do
        unless instance_variable_defined?("@#{name}")
          instance_variable_set "@#{name}", default
        end
        instance_variable_get "@#{name}"
      end
    end
    
    def split_for_last_hash(args)
      if args.last.is_a?(Hash)
        [args[0...-1], args.last]
      else
        [args, {}]
      end
    end
    
  end
  
  def self.included(other_module)
    other_module.extend ClassMethods
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nextbus-0.0.1 lib/attr_with_default.rb
nextbus-0.0.0 lib/attr_with_default.rb