summaryrefslogtreecommitdiff
path: root/lib/chef/decorator
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2016-05-02 13:38:57 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2016-05-02 13:39:06 -0700
commit754889d5563a23ca55faa2534f951d8315b8b63e (patch)
treeed0557ecb311965b1c9cda036851f29d7fb62fe5 /lib/chef/decorator
parent34f0dfc8226860aa2d2bfcbf17dad6be20bea7e3 (diff)
downloadchef-754889d5563a23ca55faa2534f951d8315b8b63e.tar.gz
WIP
Diffstat (limited to 'lib/chef/decorator')
-rw-r--r--lib/chef/decorator/lazy.rb50
-rw-r--r--lib/chef/decorator/lazy_array.rb44
2 files changed, 94 insertions, 0 deletions
diff --git a/lib/chef/decorator/lazy.rb b/lib/chef/decorator/lazy.rb
new file mode 100644
index 0000000000..067d6bd7f8
--- /dev/null
+++ b/lib/chef/decorator/lazy.rb
@@ -0,0 +1,50 @@
+#--
+# Copyright:: Copyright 2016 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed 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.
+#
+
+require "chef/decorator"
+
+class Chef
+ class Decorator
+ # Lazy wrapper to delay construction of an object until a method is
+ # called against the object.
+ #
+ # @example
+ # a = Chef::Decorator::Lazy.new { puts "allocated" }
+ # puts "start"
+ # puts a.class
+ #
+ # outputs:
+ #
+ # start
+ # allocated
+ # String
+ #
+ # @since 12.10.x
+ class Lazy < Decorator
+ def initialize(&block)
+ super
+ @block = block
+ end
+
+ def __getobj__
+ __setobj__(@block.call) unless defined?(@delegate_sd_obj)
+ super
+ end
+
+ end
+ end
+end
diff --git a/lib/chef/decorator/lazy_array.rb b/lib/chef/decorator/lazy_array.rb
new file mode 100644
index 0000000000..3879aea5f6
--- /dev/null
+++ b/lib/chef/decorator/lazy_array.rb
@@ -0,0 +1,44 @@
+#--
+# Copyright:: Copyright 2016 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed 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.
+#
+
+require "chef/decorator"
+
+class Chef
+ class Decorator
+ # Lazy wrapper to delay construction of an object until a method is
+ # called against the object.
+ #
+ # @example
+ # a = Chef::Decorator::Lazy.new { puts "allocated" }
+ # puts "start"
+ # puts a.class
+ #
+ # outputs:
+ #
+ # start
+ # allocated
+ # String
+ #
+ # @since 12.10.x
+ class LazyArray < Lazy
+ def [](idx)
+ block = @block
+ Lazy.new { block.call[idx] }
+ end
+ end
+ end
+end