Class: Shoes::Drawable
- Defined in:
- lacci/lib/shoes.rb,
lacci/lib/shoes/download.rb,
lacci/lib/shoes/drawable.rb,
lacci/lib/shoes/drawables/para.rb
Overview
Shoes::Drawable
This is the display-service portable Shoes Drawable interface. Visible Shoes drawables like buttons inherit from this. Compound drawables made of multiple different smaller Drawables inherit from it in their various apps or libraries. The Shoes Drawable helps build a Shoes-side drawable tree, with parents and children. Any API that applies to all drawables (e.g. remove) should be defined here.
Direct Known Subclasses
App, Arc, Arrow, Button, Check, EditBox, EditLine, Image, Line, ListBox, Para, Progress, Radio, Rect, Slot, Span, Star, SubscriptionItem, TextDrawable, Video
Defined Under Namespace
Classes: ResponseWrapper
Constant Summary
Constants included from Log
Log::DEFAULT_COMPONENT, Log::DEFAULT_DEBUG_LOG_CONFIG, Log::DEFAULT_LOG_CONFIG
Class Attribute Summary collapse
-
.drawable_classes ⇒ Object
Returns the value of attribute drawable_classes.
-
.drawable_default_styles ⇒ Object
Returns the value of attribute drawable_default_styles.
-
.widget_classes ⇒ Object
Returns the value of attribute widget_classes.
Instance Attribute Summary collapse
-
#debug_id ⇒ Object
readonly
Returns the value of attribute debug_id.
-
#destroyed ⇒ Object
readonly
Returns the value of attribute destroyed.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Attributes inherited from Linkable
Class Method Summary collapse
-
.allocate_drawable_id ⇒ Object
Assign a new Shoes Drawable ID number, starting from 1.
- .drawable_by_id(id, none_ok: false) ⇒ Object
- .drawable_class_by_name(name) ⇒ Object
- .dsl_name ⇒ Object
-
.get_shoes_events ⇒ Object
Return a list of Shoes events for this class.
- .is_widget_class?(name) ⇒ Boolean
- .register_drawable_id(id, drawable) ⇒ Object
-
.shoes_events(*args) ⇒ void
Set the list of Shoes event names that are allowed for this class.
-
.shoes_style(name, &validator) ⇒ Object
Shoes styles in Shoes Linkables are automatically sync'd with the display side objects.
- .shoes_style_hashes ⇒ Object
- .shoes_style_name?(name) ⇒ Boolean
- .shoes_style_names ⇒ Object
-
.shoes_styles(*names) ⇒ Object
Add these names as Shoes styles.
- .unregister_drawable_id(id) ⇒ Object
- .validate_as(prop_name, value) ⇒ Object
Instance Method Summary collapse
-
#app { ... } ⇒ Shoes::App
Calling stack.app or drawable.app will execute the block with the Shoes::App as self, and with that stack or flow as the current slot.
- #banner(*args, **kwargs) ⇒ Object
- #caption(*args, **kwargs) ⇒ Object
-
#destroy ⇒ Object
(also: #remove)
Removes the element from the Shoes::Drawable tree and removes all event subscriptions.
- #download(url, method: "GET", save: nil, styles: {}, &block) ⇒ Object
- #event(event_name, *args, **kwargs) ⇒ Object
-
#hide ⇒ Object
Hide the drawable.
-
#initialize(*args, **kwargs) ⇒ Drawable
constructor
A new instance of Drawable.
- #inscription(*args, **kwargs) ⇒ Object (also: #ins)
- #inspect ⇒ Object
-
#method_missing(name, *args, **kwargs, &block) ⇒ Object
We use method_missing to auto-create Shoes style getters and setters.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #set_parent(new_parent) ⇒ Object
- #shoes_style_values ⇒ Object
-
#show ⇒ Object
Show the drawable.
- #style(*args, **kwargs) ⇒ Object
- #subtitle(*args, **kwargs) ⇒ Object
- #tagline(*args, **kwargs) ⇒ Object
- #title(*args, **kwargs) ⇒ Object
-
#toggle ⇒ Object
Hide the drawable if it is currently shown.
Methods included from Colors
Methods included from Log
configure_logger, #log_init, logger
Methods inherited from Linkable
#bind_shoes_event, #send_self_event, #send_shoes_event, #unsub_all_shoes_events, #unsub_shoes_event
Constructor Details
#initialize(*args, **kwargs) ⇒ Drawable
Returns a new instance of Drawable.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lacci/lib/shoes/drawable.rb', line 165 def initialize(*args, **kwargs) log_init("Shoes::#{self.class.name}") default_styles = Shoes::Drawable.drawable_default_styles[self.class] self.class.shoes_style_names.each do |prop| prop_sym = prop.to_sym if kwargs.key?(prop_sym) val = self.class.validate_as(prop, kwargs[prop_sym]) instance_variable_set("@" + prop, val) elsif default_styles.key?(prop_sym) val = self.class.validate_as(prop, default_styles[prop_sym]) instance_variable_set("@" + prop, val) end end super(linkable_id: Shoes::Drawable.allocate_drawable_id) Shoes::Drawable.register_drawable_id(self.linkable_id, self) generate_debug_id end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs, &block) ⇒ Object
We use method_missing to auto-create Shoes style getters and setters.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lacci/lib/shoes/drawable.rb', line 340 def method_missing(name, *args, **kwargs, &block) name_s = name.to_s if name_s[-1] == "=" prop_name = name_s[0..-2] if self.class.shoes_style_name?(prop_name) self.class.define_method(name) do |new_value| raise(Shoes::Errors::NoLinkableIdError, "Trying to set Shoes styles in an object with no linkable ID! #{inspect}") unless linkable_id new_value = self.class.validate_as(prop_name, new_value) instance_variable_set("@" + prop_name, new_value) send_shoes_event({ prop_name => new_value }, event_name: "prop_change", target: linkable_id) end return self.send(name, *args, **kwargs, &block) end end if self.class.shoes_style_name?(name_s) self.class.define_method(name) do raise(Shoes::Errors::NoLinkableIdError, "Trying to get Shoes styles in an object with no linkable ID! #{inspect}") unless linkable_id instance_variable_get("@" + name_s) end return self.send(name, *args, **kwargs, &block) end super(name, *args, **kwargs, &block) end |
Class Attribute Details
.drawable_classes ⇒ Object
Returns the value of attribute drawable_classes.
21 22 23 |
# File 'lacci/lib/shoes/drawable.rb', line 21 def drawable_classes @drawable_classes end |
.drawable_default_styles ⇒ Object
Returns the value of attribute drawable_default_styles.
22 23 24 |
# File 'lacci/lib/shoes/drawable.rb', line 22 def drawable_default_styles @drawable_default_styles end |
.widget_classes ⇒ Object
Returns the value of attribute widget_classes.
23 24 25 |
# File 'lacci/lib/shoes/drawable.rb', line 23 def @widget_classes end |
Instance Attribute Details
#debug_id ⇒ Object (readonly)
Returns the value of attribute debug_id.
163 164 165 |
# File 'lacci/lib/shoes/drawable.rb', line 163 def debug_id @debug_id end |
#destroyed ⇒ Object (readonly)
Returns the value of attribute destroyed.
304 305 306 |
# File 'lacci/lib/shoes/drawable.rb', line 304 def destroyed @destroyed end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
303 304 305 |
# File 'lacci/lib/shoes/drawable.rb', line 303 def parent @parent end |
Class Method Details
.allocate_drawable_id ⇒ Object
Assign a new Shoes Drawable ID number, starting from 1. This allows non-overlapping small integer IDs for Shoes linkable IDs - the number part of making it clear what widget you're talking about.
88 89 90 91 92 |
# File 'lacci/lib/shoes/drawable.rb', line 88 def allocate_drawable_id @drawable_id_counter ||= 0 @drawable_id_counter += 1 @drawable_id_counter end |
.drawable_by_id(id, none_ok: false) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lacci/lib/shoes/drawable.rb', line 104 def drawable_by_id(id, none_ok: false) val = @drawables_by_id[id] unless val || none_ok raise "No Drawable Found! #{@drawables_by_id.inspect}" end val end |
.drawable_class_by_name(name) ⇒ Object
45 46 47 |
# File 'lacci/lib/shoes/drawable.rb', line 45 def drawable_class_by_name(name) drawable_classes.detect { |k| k.dsl_name == name.to_s } end |
.dsl_name ⇒ Object
40 41 42 43 |
# File 'lacci/lib/shoes/drawable.rb', line 40 def dsl_name n = name.split("::").last.chomp("Drawable") n.gsub(/(.)([A-Z])/, '\1_\2').downcase end |
.get_shoes_events ⇒ Object
Return a list of Shoes events for this class.
68 69 70 71 72 73 74 |
# File 'lacci/lib/shoes/drawable.rb', line 68 def get_shoes_events if @shoes_events.nil? raise UnknownEventsForClass, "Drawable type #{self.class} hasn't defined its list of Shoes events!" end @shoes_events end |
.is_widget_class?(name) ⇒ Boolean
49 50 51 |
# File 'lacci/lib/shoes/drawable.rb', line 49 def (name) !!Shoes::Drawable..intersect?([name.to_s]) end |
.register_drawable_id(id, drawable) ⇒ Object
94 95 96 97 |
# File 'lacci/lib/shoes/drawable.rb', line 94 def register_drawable_id(id, drawable) @drawables_by_id ||= {} @drawables_by_id[id] = drawable end |
.shoes_events(*args) ⇒ void
This method returns an undefined value.
Set the list of Shoes event names that are allowed for this class.
80 81 82 |
# File 'lacci/lib/shoes/drawable.rb', line 80 def shoes_events(*args) @shoes_events ||= args.map(&:to_s) + self.superclass.get_shoes_events end |
.shoes_style(name, &validator) ⇒ Object
Shoes styles in Shoes Linkables are automatically sync'd with the display side objects. If a block is passed to shoes_style, that's the validation for the property. It should convert a given value to a valid value for the property or throw an exception.
128 129 130 131 132 133 134 135 |
# File 'lacci/lib/shoes/drawable.rb', line 128 def shoes_style(name, &validator) name = name.to_s return if linkable_properties_hash[name] linkable_properties << { name: name, validator: } linkable_properties_hash[name] = true end |
.shoes_style_hashes ⇒ Object
148 149 150 151 152 |
# File 'lacci/lib/shoes/drawable.rb', line 148 def shoes_style_hashes parent_hashes = self != Shoes::Drawable ? self.superclass.shoes_style_hashes : [] parent_hashes + linkable_properties end |
.shoes_style_name?(name) ⇒ Boolean
154 155 156 157 |
# File 'lacci/lib/shoes/drawable.rb', line 154 def shoes_style_name?(name) linkable_properties_hash[name.to_s] || (self != Shoes::Drawable && superclass.shoes_style_name?(name)) end |
.shoes_style_names ⇒ Object
142 143 144 145 146 |
# File 'lacci/lib/shoes/drawable.rb', line 142 def shoes_style_names parent_prop_names = self != Shoes::Drawable ? self.superclass.shoes_style_names : [] parent_prop_names | linkable_properties.map { |prop| prop[:name] } end |
.shoes_styles(*names) ⇒ Object
Add these names as Shoes styles
138 139 140 |
# File 'lacci/lib/shoes/drawable.rb', line 138 def shoes_styles(*names) names.each { |n| shoes_style(n) } end |
.unregister_drawable_id(id) ⇒ Object
99 100 101 102 |
# File 'lacci/lib/shoes/drawable.rb', line 99 def unregister_drawable_id(id) @drawables_by_id ||= {} @drawables_by_id.delete(id) end |
.validate_as(prop_name, value) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lacci/lib/shoes/drawable.rb', line 53 def validate_as(prop_name, value) prop_name = prop_name.to_s hashes = shoes_style_hashes h = hashes.detect { |hash| hash[:name] == prop_name } raise(Shoes::Errors::NoSuchStyleError, "Can't find property #{prop_name.inspect} in #{self} property list: #{hashes.inspect}!") unless h return value if h[:validator].nil? h[:validator].call(value) end |
Instance Method Details
#app { ... } ⇒ Shoes::App
Calling stack.app or drawable.app will execute the block with the Shoes::App as self, and with that stack or flow as the current slot.
198 199 200 201 |
# File 'lacci/lib/shoes/drawable.rb', line 198 def app(&block) Shoes::App.instance.with_slot(self, &block) if block_given? Shoes::App.instance end |
#banner(*args, **kwargs) ⇒ Object
92 93 94 |
# File 'lacci/lib/shoes/drawables/para.rb', line 92 def (*args, **kwargs) para(*args, **{ size: :banner }.merge(kwargs)) end |
#caption(*args, **kwargs) ⇒ Object
108 109 110 |
# File 'lacci/lib/shoes/drawables/para.rb', line 108 def caption(*args, **kwargs) para(*args, **{ size: :caption }.merge(kwargs)) end |
#destroy ⇒ Object Also known as: remove
Removes the element from the Shoes::Drawable tree and removes all event subscriptions
314 315 316 317 318 319 320 321 |
# File 'lacci/lib/shoes/drawable.rb', line 314 def destroy @parent&.remove_child(self) @parent = nil @destroyed = true unsub_all_shoes_events send_shoes_event(event_name: "destroy", target: linkable_id) Shoes::Drawable.unregister_drawable_id(linkable_id) end |
#download(url, method: "GET", save: nil, styles: {}, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lacci/lib/shoes/download.rb', line 21 def download(url, method: "GET", save: nil, styles: {}, &block) require "net/http" require "openssl" require "nokogiri" @block = block Thread.new do logger = Shoes::Log.logger("Shoes::App#download") begin uri = URI(url) response = perform_request(uri, method, styles) if response.is_a?(Net::HTTPRedirection) new_location = response["location"] new_uri = URI(new_location) response = perform_request(new_uri, method, styles) end wrapped_response = ResponseWrapper.new(response) # Wrap the response handle_response(wrapped_response, save, styles) rescue Net::HTTPError, Net::OpenTimeout, Net::ReadTimeout => e handle_error(e, logger) rescue StandardError => e handle_error(e., logger) # Pass the error message as a string end end end |
#event(event_name, *args, **kwargs) ⇒ Object
247 248 249 250 251 |
# File 'lacci/lib/shoes/drawable.rb', line 247 def event(event_name, *args, **kwargs) validate_event_name(event_name) send_shoes_event(*args, **kwargs, event_name:, target: linkable_id) end |
#hide ⇒ Object
Hide the drawable.
325 326 327 |
# File 'lacci/lib/shoes/drawable.rb', line 325 def hide self.hidden = true end |
#inscription(*args, **kwargs) ⇒ Object Also known as: ins
112 113 114 |
# File 'lacci/lib/shoes/drawables/para.rb', line 112 def inscription(*args, **kwargs) para(*args, **{ size: :inscription }.merge(kwargs)) end |
#inspect ⇒ Object
217 218 219 220 221 |
# File 'lacci/lib/shoes/drawable.rb', line 217 def inspect "#<#{debug_id} " + " @parent=#{@parent ? @parent.debug_id : "(none)"} " + "@children=#{@children ? @children.map(&:debug_id) : "(none)"} properties=#{shoes_style_values.inspect}>" end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
371 372 373 374 375 376 377 378 |
# File 'lacci/lib/shoes/drawable.rb', line 371 def respond_to_missing?(name, include_private = false) name_s = name.to_s return true if self.class.shoes_style_name?(name_s) return true if self.class.shoes_style_name?(name_s[0..-2]) && name_s[-1] == "=" return true if Drawable.drawable_class_by_name(name_s) super end |
#set_parent(new_parent) ⇒ Object
306 307 308 309 310 311 |
# File 'lacci/lib/shoes/drawable.rb', line 306 def set_parent(new_parent) @parent&.remove_child(self) new_parent&.add_child(self) @parent = new_parent send_shoes_event(new_parent.linkable_id, event_name: "parent", target: linkable_id) end |
#shoes_style_values ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lacci/lib/shoes/drawable.rb', line 253 def shoes_style_values all_property_names = self.class.shoes_style_names properties = {} all_property_names.each do |prop| properties[prop] = instance_variable_get("@" + prop) end properties["shoes_linkable_id"] = self.linkable_id properties end |
#show ⇒ Object
Show the drawable.
330 331 332 |
# File 'lacci/lib/shoes/drawable.rb', line 330 def show self.hidden = false end |
#style(*args, **kwargs) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lacci/lib/shoes/drawable.rb', line 264 def style(*args, **kwargs) if args.empty? && kwargs.empty? # Just called as .style() shoes_style_values elsif args.empty? # This is called to set one or more Shoes styles prop_names = self.class.shoes_style_names unknown_styles = kwargs.keys.select { |k| !prop_names.include?(k.to_s) } unless unknown_styles.empty? raise Shoes::Errors::NoSuchStyleError, "Unknown styles for drawable type #{self.class.name}: #{unknown_styles.join(", ")}" end kwargs.each do |name, val| instance_variable_set("@#{name}", val) end elsif args.length == 1 && args[0] < Shoes::Drawable # Shoes supports calling .style with a Shoes class, e.g. .style(Shoes::Button, displace_left: 5) kwargs.each do |name, val| Shoes::Drawable.drawable_default_styles[args[0]][name.to_sym] = val end else raise Shoes::Errors::InvalidAttributeValueError, "Unexpected arguments to style! args: #{args.inspect}, keyword args: #{kwargs.inspect}" end end |
#subtitle(*args, **kwargs) ⇒ Object
100 101 102 |
# File 'lacci/lib/shoes/drawables/para.rb', line 100 def subtitle(*args, **kwargs) para(*args, **{ size: :subtitle }.merge(kwargs)) end |
#tagline(*args, **kwargs) ⇒ Object
104 105 106 |
# File 'lacci/lib/shoes/drawables/para.rb', line 104 def tagline(*args, **kwargs) para(*args, **{ size: :tagline }.merge(kwargs)) end |
#title(*args, **kwargs) ⇒ Object
96 97 98 |
# File 'lacci/lib/shoes/drawables/para.rb', line 96 def title(*args, **kwargs) para(*args, **{ size: :title }.merge(kwargs)) end |
#toggle ⇒ Object
Hide the drawable if it is currently shown. Show it if it is currently hidden.
335 336 337 |
# File 'lacci/lib/shoes/drawable.rb', line 335 def toggle self.hidden = !self.hidden end |