Sha256: 570e04745a59aad3541b109342a77e1238ca090c368501916dfdee6719307925

Contents?: true

Size: 920 Bytes

Versions: 1

Compression:

Stored size: 920 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|
        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-0.0.7 lib/attr_extras.rb