Sha256: 5d1e47cd9d193cee267625cdce0e358428d61ba555b69cd48c8b2e0fe3e6c569

Contents?: true

Size: 926 Bytes

Versions: 2

Compression:

Stored size: 926 Bytes

Contents

require "attr_extras/version"

module AttrExtras
  module ClassMethods
    def attr_initialize(*names)
      define_method(:initialize) do |*values|
        unless values.length == names.length
          raise ArgumentError, "wrong number of arguments (#{values.length} for #{names.length})"
        end

        names.zip(values).each do |name, value|
          instance_variable_set("@#{name}", value)
        end
      end
    end

    def attr_private(*names)
      attr_reader *names
      private *names
    end

    def attr_id_query(*names)
      names.each do |name|
        unless name.to_s.end_with?("?")
          raise "#{__method__} wants `#{name}?`, not `#{name}`."
        end

        define_method(name) do            # def foo?
          !!send("#{name.to_s.chop}_id")  #   !!send("foo_id")
        end                               # end
      end
    end
  end
end

Object.extend AttrExtras::ClassMethods

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attr_extras-0.0.6 lib/attr_extras.rb
attr_extras-0.0.5 lib/attr_extras.rb