Sha256: ce8d01fdcc46428815d3d5c83f2bd44acbb3389a016ef2e1af3a8117b9825dad
Contents?: true
Size: 1.44 KB
Versions: 2
Compression:
Stored size: 1.44 KB
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_or_names, value| if name_or_names.is_a?(Array) name_or_names.each do |key| instance_variable_set("@#{key}", value[key]) end else instance_variable_set("@#{name_or_names}", value) end end end end def attr_private(*names) attr_reader *names private *names end def pattr_initialize(*names) attr_initialize *names attr_private *names.flatten end def method_object(method_name, *names) metaclass = (class << self; self; end) metaclass.send(:define_method, method_name) do |*values| new(*values).send(method_name) end pattr_initialize *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 class Class include AttrExtras::ClassMethods end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
attr_extras-1.4.0 | lib/attr_extras.rb |
attr_extras-1.3.0 | lib/attr_extras.rb |