Module: AttrSetter
- Included in:
- Swing::Frame, Swing::Label, Swing::List, Swing::Menu, Swing::MenuBar, Swing::MenuItem, Swing::Panel, Swing::ScrollPane, Swing::SplitPane
- Defined in:
- lib/swing_support/extensions/attr_setter.rb
Overview
Module allows including classes to receive attribute values in an opts Hash and sets those attributes after object initialization
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#set_attributes(opts) {|opts| ... } ⇒ Object
Sets attributes after yielding to a given block (usually to call super).
Class Method Details
.dim_proc(default = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/swing_support/extensions/attr_setter.rb', line 8 def self.dim_proc default = nil proc do |*args| case args.size when 2 Dimension.new *args when 1 case args.first when Dimension args.first when String Dimension.new *args.first.split(/x|-|:/, 2) end when 0 Dimension.new *default unless default.nil? end end end |
.included(host) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/swing_support/extensions/attr_setter.rb', line 26 def self.included host host.send :extend, ClassMethods host.attr_setter :font, :tool_tip_text, :preferred_size => dim_proc, :minimum_size => dim_proc, :maximum_size => dim_proc end |
Instance Method Details
#set_attributes(opts) {|opts| ... } ⇒ Object
Sets attributes after yielding to a given block (usually to call super). Use this method to wrap super call to ORIGINAL J… object, exactly once per each derived view hierarchy. TODO: better way?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/swing_support/extensions/attr_setter.rb', line 56 def set_attributes opts # Extract known attributes given in opts, to be set after object initialization attributes = self.class.attributes.map do |name, default| value = opts.delete name result = if default.nil? # No default, return whatever value value elsif default.respond_to? :call # Default is callable, call it with whatever value default.call *value else # Return either non-nil value or default value.nil? ? default : value end [name, result] unless result.nil? end.compact yield opts if block_given? attributes.each { |(name, value)| send "#{name}=", *value } end |