From 7b0fd82c7c8c3fedc842274205015e0ddb0f3f24 Mon Sep 17 00:00:00 2001 From: Dag Odenhall Date: Sat, 31 May 2008 04:03:50 +0200 Subject: [PATCH] Framework for lazy data [#5 state:resolved] --- lib/amazing.rb | 1 + lib/amazing/helpers/lazy_data.rb | 11 +++++++++++ lib/{amazing.rb => amazing/lazy.rb} | 27 +++++++++++++++++---------- lib/amazing/widget.rb | 2 ++ 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 lib/amazing/helpers/lazy_data.rb copy lib/{amazing.rb => amazing/lazy.rb} (69%) diff --git a/lib/amazing.rb b/lib/amazing.rb index c6241e7..efbb55d 100644 --- a/lib/amazing.rb +++ b/lib/amazing.rb @@ -15,6 +15,7 @@ require 'amazing/awesome' require 'amazing/cli' require 'amazing/config' +require 'amazing/lazy' require 'amazing/numeric' require 'amazing/options' require 'amazing/proc_file' diff --git a/lib/amazing/helpers/lazy_data.rb b/lib/amazing/helpers/lazy_data.rb new file mode 100644 index 0000000..5c3178a --- /dev/null +++ b/lib/amazing/helpers/lazy_data.rb @@ -0,0 +1,11 @@ +require 'amazing/lazy' + +module Amazing + module Helpers + module LazyData + def lazy(&block) + Lazy.new(&block) + end + end + end +end diff --git a/lib/amazing.rb b/lib/amazing/lazy.rb similarity index 69% copy from lib/amazing.rb copy to lib/amazing/lazy.rb index c6241e7..551ec86 100644 --- a/lib/amazing.rb +++ b/lib/amazing/lazy.rb @@ -12,13 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -require 'amazing/awesome' -require 'amazing/cli' -require 'amazing/config' -require 'amazing/numeric' -require 'amazing/options' -require 'amazing/proc_file' -require 'amazing/string' -require 'amazing/widget' -require 'amazing/widgets' -require 'amazing/x11' +module Amazing + class Lazy + def initialize(&block) + @block = block + end + + def to_s + @value ||= @block.call.to_s + end + + alias inspect to_s + + def method_missing(name, *args, &block) + to_s.__send__(name, *args, &block) + end + end +end diff --git a/lib/amazing/widget.rb b/lib/amazing/widget.rb index d8259c9..95335e4 100644 --- a/lib/amazing/widget.rb +++ b/lib/amazing/widget.rb @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +require 'amazing/helpers/lazy_data' require 'amazing/helpers/pango_markup' require 'erb' @@ -36,6 +37,7 @@ module Amazing # end # end class Widget + include Helpers::LazyData include Helpers::PangoMarkup include ERB::Util -- 2.11.4.GIT