qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcpie...@apache.org
Subject [13/35] qpid-proton git commit: PROTON-799: Added a constants value mixin to the Ruby bindings.
Date Tue, 19 May 2015 17:22:33 GMT
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/a16a7c9e
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/a16a7c9e
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/a16a7c9e

Branch: refs/heads/ruby-engine-apis
Commit: a16a7c9ef6d963183407592b725d46112134dc69
Parents: d341d07
Author: Darryl L. Pierce <mcpierce@gmail.com>
Authored: Tue Feb 10 15:22:05 2015 -0500
Committer: Darryl L. Pierce <mcpierce@gmail.com>
Committed: Tue May 19 10:16:56 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/a16a7c9e/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/a16a7c9e/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-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message