e2mmap-0.1.0/0000755000175000017500000000000013617277615014176 5ustar balasankarcbalasankarce2mmap-0.1.0/README.md0000644000175000017500000000346013617277615015460 0ustar balasankarcbalasankarc# Exception2MessageMapper Helper module for easily defining exceptions with predefined messages. ## Installation Add this line to your application's Gemfile: ```ruby gem 'e2mmap' ``` And then execute: $ bundle Or install it yourself as: $ gem install e2mmap ## Usage 1. ``` class Foo extend Exception2MessageMapper def_e2message ExistingExceptionClass, "message..." def_exception :NewExceptionClass, "message..."[, superclass] ... end ``` 2. ``` module Error extend Exception2MessageMapper def_e2message ExistingExceptionClass, "message..." def_exception :NewExceptionClass, "message..."[, superclass] ... end class Foo include Error ... end foo = Foo.new foo.Fail .... ``` 3. ``` module Error extend Exception2MessageMapper def_e2message ExistingExceptionClass, "message..." def_exception :NewExceptionClass, "message..."[, superclass] ... end class Foo extend Exception2MessageMapper include Error ... end Foo.Fail NewExceptionClass, arg... Foo.Fail ExistingExceptionClass, arg... ``` ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/e2mmap. ## License The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause). e2mmap-0.1.0/Rakefile0000644000175000017500000000003413617277615015640 0ustar balasankarcbalasankarcrequire "bundler/gem_tasks" e2mmap-0.1.0/Gemfile0000644000175000017500000000024113617277615015466 0ustar balasankarcbalasankarcsource "https://rubygems.org" git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in e2mmap.gemspec gemspec e2mmap-0.1.0/lib/0000755000175000017500000000000013617277615014744 5ustar balasankarcbalasankarce2mmap-0.1.0/lib/e2mmap.rb0000644000175000017500000000770613617277615016464 0ustar balasankarcbalasankarc# frozen_string_literal: true # #-- # e2mmap.rb - for Ruby 1.1 # $Release Version: 2.0$ # $Revision: 1.10 $ # by Keiju ISHITSUKA # #++ # # Helper module for easily defining exceptions with predefined messages. # # == Usage # # 1. # class Foo # extend Exception2MessageMapper # def_e2message ExistingExceptionClass, "message..." # def_exception :NewExceptionClass, "message..."[, superclass] # ... # end # # 2. # module Error # extend Exception2MessageMapper # def_e2message ExistingExceptionClass, "message..." # def_exception :NewExceptionClass, "message..."[, superclass] # ... # end # class Foo # include Error # ... # end # # foo = Foo.new # foo.Fail .... # # 3. # module Error # extend Exception2MessageMapper # def_e2message ExistingExceptionClass, "message..." # def_exception :NewExceptionClass, "message..."[, superclass] # ... # end # class Foo # extend Exception2MessageMapper # include Error # ... # end # # Foo.Fail NewExceptionClass, arg... # Foo.Fail ExistingExceptionClass, arg... # # module Exception2MessageMapper E2MM = Exception2MessageMapper # :nodoc: def E2MM.extend_object(cl) super cl.bind(self) unless cl < E2MM end def bind(cl) self.module_eval "#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1 begin; def Raise(err = nil, *rest) Exception2MessageMapper.Raise(self.class, err, *rest) end alias Fail Raise class << self undef included end def self.included(mod) mod.extend Exception2MessageMapper end end; end # Fail(err, *rest) # err: exception # rest: message arguments # def Raise(err = nil, *rest) E2MM.Raise(self, err, *rest) end alias Fail Raise alias fail Raise # def_e2message(c, m) # c: exception # m: message_form # define exception c with message m. # def def_e2message(c, m) E2MM.def_e2message(self, c, m) end # def_exception(n, m, s) # n: exception_name # m: message_form # s: superclass(default: StandardError) # define exception named ``c'' with message m. # def def_exception(n, m, s = StandardError) E2MM.def_exception(self, n, m, s) end # # Private definitions. # # {[class, exp] => message, ...} @MessageMap = {} # E2MM.def_e2message(k, e, m) # k: class to define exception under. # e: exception # m: message_form # define exception c with message m. # def E2MM.def_e2message(k, c, m) E2MM.instance_eval{@MessageMap[[k, c]] = m} c end # E2MM.def_exception(k, n, m, s) # k: class to define exception under. # n: exception_name # m: message_form # s: superclass(default: StandardError) # define exception named ``c'' with message m. # def E2MM.def_exception(k, n, m, s = StandardError) e = Class.new(s) E2MM.instance_eval{@MessageMap[[k, e]] = m} k.module_eval {remove_const(n)} if k.const_defined?(n, false) k.const_set(n, e) end # Fail(klass, err, *rest) # klass: class to define exception under. # err: exception # rest: message arguments # def E2MM.Raise(klass = E2MM, err = nil, *rest) if form = e2mm_message(klass, err) b = $@.nil? ? caller(1) : $@ b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/ raise err, sprintf(form, *rest), b else E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect end end class << E2MM alias Fail Raise end def E2MM.e2mm_message(klass, exp) for c in klass.ancestors if mes = @MessageMap[[c,exp]] m = klass.instance_eval('"' + mes + '"') return m end end nil end class << self alias message e2mm_message end E2MM.def_exception(E2MM, :ErrNotRegisteredException, "not registered exception(%s)") end e2mmap-0.1.0/lib/e2mmap/0000755000175000017500000000000013617277615016125 5ustar balasankarcbalasankarce2mmap-0.1.0/lib/e2mmap/version.rb0000644000175000017500000000006713617277615020142 0ustar balasankarcbalasankarcmodule Exception2MessageMapper VERSION = "0.1.0" end e2mmap-0.1.0/e2mmap.gemspec0000644000175000017500000000202213617277615016720 0ustar balasankarcbalasankarcbegin require_relative "lib/e2mmap/version" rescue LoadError # for Ruby core repository require_relative "e2mmap/version" end Gem::Specification.new do |spec| spec.name = "e2mmap" spec.version = Exception2MessageMapper::VERSION spec.authors = ["Keiju ISHITSUKA"] spec.email = ["keiju@ruby-lang.org"] spec.summary = %q{Module for defining custom exceptions with specific messages.} spec.description = %q{Module for defining custom exceptions with specific messages.} spec.homepage = "https://github.com/ruby/e2mmap" spec.license = "BSD-2-Clause" spec.files = [".gitignore", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "e2mmap.gemspec", "lib/e2mmap.rb", "lib/e2mmap/version.rb"] spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency "rake", "~> 10.0" end e2mmap-0.1.0/bin/0000755000175000017500000000000013617277615014746 5ustar balasankarcbalasankarce2mmap-0.1.0/bin/console0000755000175000017500000000052513617277615016340 0ustar balasankarcbalasankarc#!/usr/bin/env ruby require "bundler/setup" require "e2mmap" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. # (If you use this, don't forget to add pry to your Gemfile!) # require "pry" # Pry.start require "irb" IRB.start(__FILE__) e2mmap-0.1.0/bin/setup0000755000175000017500000000020313617277615016027 0ustar balasankarcbalasankarc#!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' set -vx bundle install # Do any other automated setup that you need to do here e2mmap-0.1.0/LICENSE.txt0000644000175000017500000000240213617277615016017 0ustar balasankarcbalasankarcCopyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. e2mmap-0.1.0/.gitignore0000644000175000017500000000011113617277615016157 0ustar balasankarcbalasankarc/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/