You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2011/11/29 14:44:56 UTC

[PATCH core 04/10] Core: Added callback extension for Proc

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/core_ext.rb      |    1 +
 server/lib/deltacloud/core_ext/proc.rb |   27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)
 create mode 100644 server/lib/deltacloud/core_ext/proc.rb

diff --git a/server/lib/deltacloud/core_ext.rb b/server/lib/deltacloud/core_ext.rb
index 042bffc..fedbc18 100644
--- a/server/lib/deltacloud/core_ext.rb
+++ b/server/lib/deltacloud/core_ext.rb
@@ -18,3 +18,4 @@ require 'deltacloud/core_ext/string'
 require 'deltacloud/core_ext/integer'
 require 'deltacloud/core_ext/hash'
 require 'deltacloud/core_ext/array'
+require 'deltacloud/core_ext/proc'
diff --git a/server/lib/deltacloud/core_ext/proc.rb b/server/lib/deltacloud/core_ext/proc.rb
new file mode 100644
index 0000000..3c443ff
--- /dev/null
+++ b/server/lib/deltacloud/core_ext/proc.rb
@@ -0,0 +1,27 @@
+# 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.
+
+# Original code copied from: http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
+# Copyright 2011 Matt Sears.
+class Proc
+  def callback(callable, *args)
+    self === Class.new do
+      method_name = callable.to_sym
+      define_method(method_name) { |&block| block.nil? ? true : block.call(*args) }
+      define_method("#{method_name}?") { true }
+      def method_missing(method_name, *args, &block) false; end
+    end.new
+  end
+end
-- 
1.7.4.4