PROTON-799: Created a wrapper helper module for Ruby bindings. This module provides methods to make it easier to write wrapper methods for the underlying Proton C libraries, reducing a lot of boilerplate coding and removing the potential for bugs due to typos.
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/0530d1a1 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/0530d1a1 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/0530d1a1 Branch: refs/heads/master Commit: 0530d1a194c1e94e347a9fb04fa8e5380bf2db1a Parents: 57deee4 Author: Darryl L. Pierce <mcpie...@gmail.com> Authored: Tue Feb 10 15:22:50 2015 -0500 Committer: Darryl L. Pierce <mcpie...@gmail.com> Committed: Wed Jun 3 16:29:21 2015 -0400 ---------------------------------------------------------------------- proton-c/bindings/ruby/lib/messenger/filters.rb | 2 +- .../bindings/ruby/lib/messenger/selectable.rb | 2 +- proton-c/bindings/ruby/lib/qpid_proton.rb | 1 + proton-c/bindings/ruby/lib/util/swig_helper.rb | 114 +++++++++++++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0530d1a1/proton-c/bindings/ruby/lib/messenger/filters.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/messenger/filters.rb b/proton-c/bindings/ruby/lib/messenger/filters.rb index e2b50bc..0ab3407 100644 --- a/proton-c/bindings/ruby/lib/messenger/filters.rb +++ b/proton-c/bindings/ruby/lib/messenger/filters.rb @@ -17,7 +17,7 @@ # under the License. #++ -module Qpid::Proton +module Qpid::Proton::Messenger # @private module Filters http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0530d1a1/proton-c/bindings/ruby/lib/messenger/selectable.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/messenger/selectable.rb b/proton-c/bindings/ruby/lib/messenger/selectable.rb index 36b5761..9a61317 100644 --- a/proton-c/bindings/ruby/lib/messenger/selectable.rb +++ b/proton-c/bindings/ruby/lib/messenger/selectable.rb @@ -25,7 +25,7 @@ module Qpid::Proton::Messenger # @private class Selectable - include Qpid::Proton::Filters + include Filters call_before :check_is_initialized, :fileno, :capacity, :pending, :deadline, http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0530d1a1/proton-c/bindings/ruby/lib/qpid_proton.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/qpid_proton.rb b/proton-c/bindings/ruby/lib/qpid_proton.rb index e90df84..da9983c 100644 --- a/proton-c/bindings/ruby/lib/qpid_proton.rb +++ b/proton-c/bindings/ruby/lib/qpid_proton.rb @@ -31,6 +31,7 @@ require "core/exceptions" require "util/version" require "util/error_handler" require "util/constants" +require "util/swig_helper" # Types require "types/strings" http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0530d1a1/proton-c/bindings/ruby/lib/util/swig_helper.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/util/swig_helper.rb b/proton-c/bindings/ruby/lib/util/swig_helper.rb new file mode 100644 index 0000000..d60e9e4 --- /dev/null +++ b/proton-c/bindings/ruby/lib/util/swig_helper.rb @@ -0,0 +1,114 @@ +#-- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +#++ + +module Qpid::Proton::Util + + # Provides helper functions for writing wrapper functions for the + # underlying C APIs. + # + # Before defining any mutators the class must define the name of the + # prefix for methods with the constant PROTON_METOD_PREFIX. + # + # == Mutators, Setters And Getters + # + # There are three types of wrappers that are supported: + # + # [proton_writer] Defines a set-only method for the named attribute. + # [proton_reader] Defines a get-only method for the named attribute. + # [proton_accessor] Defines both a set- and a get-method for the named + # attribute. + # [proton_caller] A simple wrapper for calling an underlying method, + # avoids repetitive boiler plate coding. + # + # == Arguments + # + # [:is_or_get => {:is, :get}] For both the getter and the mutator types + # you can also declare that the method uses "is" instead of "get" in the + # underlying API. Such methods are then defined with "?" + # + # @example + # class Terminus + # + # include WrapperHelper + # + # PROTON_METHOD_PREFIX = "pn_terminus" + # + # # add methods "type" and "type=" that call "pn_terminus_{get,set}_type" + # proton_accessor :type + # + # # adds the method "dynamic?" that calls "pn_terminus_is_dynamic" + # proton_accessor :dynamic, :is_or_get => :is + # + # # adds a method named "foo" that calls "pn_terminus_foo" + # proton_caller :foo + # + # end + # + # @private + module SwigHelper + + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods # :nodoc: + + def create_wrapper_method(name, proton_method, with_arg = false) + if with_arg + define_method "#{name}" do |arg| + Cproton.__send__(proton_method.to_sym, @impl, arg) + end + else + define_method "#{name}" do + Cproton.__send__(proton_method.to_sym, @impl) + end + end + end + + # Defines a method that calls an underlying C library function. + def proton_caller(name, options = {}) + proton_method = "#{self::PROTON_METHOD_PREFIX}_#{name}" + # drop the trailing '?' if this is a property method + proton_method = proton_method[0..-2] if proton_method.end_with? "?" + create_wrapper_method(name, proton_method) + end + + def proton_writer(name, options = {}) + proton_method = "#{self::PROTON_METHOD_PREFIX}_set_#{name}" + create_wrapper_method("#{name}=", proton_method, true) + end + + def proton_reader(name, options = {}) + an_is_method = options[:is_or_get] == :is + prefix = (an_is_method) ? "is" : "get" + proton_method = "#{self::PROTON_METHOD_PREFIX}_#{prefix}_#{name}" + name = "#{name}?" if an_is_method + create_wrapper_method(name, proton_method) + end + + def proton_accessor(name, options = {}) + proton_writer(name, options) + proton_reader(name, options) + end + + end + + end + +end --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org