Sha256: 9040f32b1cfdc4191a55e5aa3cc4f3e52fa5e1527d236852d80ec82f0344f3df
Contents?: true
Size: 903 Bytes
Versions: 3
Compression:
Stored size: 903 Bytes
Contents
class AttrExtras::AttrImplement def initialize(klass, names) @klass, @names = klass, names.dup end def apply arg_names = @names.last.is_a?(Array) ? @names.pop : [] expected_arity = arg_names.length # Make available within the block. names = @names mod = Module.new do define_method :method_missing do |name, *args| if names.include?(name) provided_arity = args.length if provided_arity != expected_arity raise ArgumentError, "wrong number of arguments (#{provided_arity} for #{expected_arity})" end raise AttrExtras::MethodNotImplementedError, "Implement a#{"n" if name[0].match?(/\A[aeiou]/i)} '#{name}(#{arg_names.join(", ")})' method" else super(name, *args) end end end # include is private in Ruby 2.0 and earlier. @klass.send(:include, mod) end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
attr_extras-6.2.5 | lib/attr_extras/attr_implement.rb |
attr_extras-6.2.4 | lib/attr_extras/attr_implement.rb |
attr_extras-6.2.3 | lib/attr_extras/attr_implement.rb |