PROTON-799: Added a constants value mixin to the Ruby bindings.

The purpose of this mixin is to make it easier to create classes that
represent constant values from the Proton C code.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/57deee4d
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/57deee4d
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/57deee4d

Branch: refs/heads/master
Commit: 57deee4d6e5336b783b9ec14c8abb3107f707d39
Parents: 8359e41
Author: Darryl L. Pierce <mcpie...@gmail.com>
Authored: Tue Feb 10 15:22:05 2015 -0500
Committer: Darryl L. Pierce <mcpie...@gmail.com>
Committed: Wed Jun 3 16:29:21 2015 -0400

----------------------------------------------------------------------
 proton-c/bindings/ruby/lib/qpid_proton.rb    |  1 +
 proton-c/bindings/ruby/lib/util/constants.rb | 85 +++++++++++++++++++++++
 2 files changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/57deee4d/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 e8fab77..e90df84 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton.rb
@@ -30,6 +30,7 @@ require "core/exceptions"
 # Utility classes
 require "util/version"
 require "util/error_handler"
+require "util/constants"
 
 # Types
 require "types/strings"

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/57deee4d/proton-c/bindings/ruby/lib/util/constants.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/util/constants.rb 
b/proton-c/bindings/ruby/lib/util/constants.rb
new file mode 100644
index 0000000..50225e6
--- /dev/null
+++ b/proton-c/bindings/ruby/lib/util/constants.rb
@@ -0,0 +1,85 @@
+#--
+# 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 a means for defining constant values within the namespace
+  # of a class.
+  #
+  # If the class has defined the class method, :post_add_constant, then that
+  # method will be invoked after each new item is added. It must be defined
+  # *before* any constants are defined.
+  #
+  # ==== Example
+  #
+  #   class GrammarComponent
+  #
+  #     include Qpid::Proton::Constants
+  #
+  #     def self.post_add_constant(key, value)
+  #       @terminal << value if value.terminal?
+  #       @nonterminal << value if !value.terminal? && !value.rule
+  #       @rule << value if value.rule
+  #     end
+  #
+  #     self.add_constant :LEFT_PARENTHESIS, new GrammarComponent("(", 
:terminal)
+  #     self.add_constant :RIGHT_PARENTHESIS, new GrammarComponent(")", 
:terminal)
+  #     self.add_constant :ELEMENT, new GrammarComponent("E", :rule)
+  #
+  #     def initialize(component, type)
+  #       @component = component
+  #       @type = type
+  #     end
+  #
+  #     def terminal?; @type == :terminal; end
+  #
+  #     def rule?; @type == :rule; end
+  #
+  #   end
+  #
+  # @private
+  #
+  module Constants
+
+    def self.included(base)
+      base.extend ClassMethods
+    end
+
+    module ClassMethods
+
+      def add_constant(key, value)
+        self.const_set(key, value)
+
+        @pn_by_value ||= {}
+        @pn_by_value[value] = key
+
+        if self.respond_to? :post_add_constant
+          self.post_add_constant(key, value)
+        end
+      end
+
+      def by_value(value)
+        (@pn_by_value || {})[value]
+      end
+
+    end
+
+  end
+
+end


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to