lib/glimmer/swt/custom_widget.rb in glimmer-0.4.7 vs lib/glimmer/swt/custom_widget.rb in glimmer-0.4.8
- old
+ new
@@ -1,7 +1,5 @@
-require 'set'
-
require_relative 'proc_tracker'
require_relative 'observable_model'
module Glimmer
module SWT
@@ -32,21 +30,29 @@
# where they return the instance values `options[:color1]` and `options[:color2]`
# respectively.
# Can be called multiple times to set more options additively.
# When passed no arguments, it returns list of all option names captured so far
def options(*new_options)
- new_options = new_options.compact.map(&:to_s)
+ new_options = new_options.compact.map(&:to_s).map(&:to_sym)
if new_options.empty?
- @options ||= Set.new # class options array
+ @options ||= {} # maps options to defaults
else
- options |= Set[*new_options]
+ new_options = new_options.reduce({}) {|new_options, new_option| new_options.merge(new_option => nil)}
+ @options = options.merge(new_options)
def_option_attr_readers(new_options)
end
end
+ def option(new_option, new_option_default = nil)
+ new_option = new_option.to_s.to_sym
+ new_options = {new_option => new_option_default}
+ @options = options.merge(new_options)
+ def_option_attr_readers(new_options)
+ end
+
def def_option_attr_readers(new_options)
- new_options.each do |option|
+ new_options.each do |option, default|
class_eval <<-end_eval, __FILE__, __LINE__
def #{option}
options[:#{option}]
end
end_eval
@@ -57,11 +63,12 @@
attr_reader :body_root, :widget, :parent, :swt_style, :options, :content
def initialize(parent, *swt_constants, options, &content)
@parent = parent
@swt_style = GSWT[*swt_constants]
- @options = options # instance options hash
+ options ||= {}
+ @options = self.class.options.merge(options)
@content = ProcTracker.new(content) if content
@body_root = body
@widget = @body_root.widget
end
@@ -73,15 +80,21 @@
def can_add_listener?(underscored_listener_name)
@body_root.can_add_listener?(underscored_listener_name)
end
+ # TODO clean up difference between add_listener and add_observer
+
def add_listener(underscored_listener_name, &block)
@body_root.add_listener(underscored_listener_name, &block)
end
def add_observer(observer, attribute_name)
- @body_root.add_observer(observer, attribute_name)
+ if respond_to?(attribute_name)
+ super
+ else
+ @body_root.add_observer(observer, attribute_name)
+ end
end
def has_attribute?(attribute_name, *args)
respond_to?(attribute_setter(attribute_name), args) ||
@body_root.has_attribute?(attribute_name, *args)