Sha256: 93c870addff6a307be14892f8fc5ea5a399fa7b44c4e97f5842590749366c11e

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 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 pattr_initialize(*names)
      attr_initialize *names
      attr_private *names
    end

    def attr_id_query(*names)
      names.each do |name|
        name = name.to_s

        raise "#{__method__} wants `#{name}?`, not `#{name}`." unless name.end_with?("?")

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

Object.extend AttrExtras::ClassMethods

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attr_extras-1.1.0 lib/attr_extras.rb