Class: MetaRuby::GUI::HTML::Button
- Inherits:
-
Object
- Object
- MetaRuby::GUI::HTML::Button
- Defined in:
- lib/metaruby/gui/html/button.rb
Overview
Representation of a button in Page
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The button ID.
-
#off_text ⇒ String
readonly
The text when the button is OFF.
-
#on_text ⇒ String
readonly
The text when the button is ON.
-
#state ⇒ Object
The current button state.
Instance Method Summary collapse
-
#base_url ⇒ String
The button base URL.
-
#html_id ⇒ String
private
The ID, quoted for HTML.
-
#initialize(id, text: nil, on_text: "#{id} (on)", off_text: "#{id} (off)", state: false) ⇒ Button
constructor
Create a button.
-
#render ⇒ String
Render the button as HTML.
-
#text ⇒ Object
The button text.
-
#toggle_url ⇒ Object
The URL that would toggle the button (i.e. turn it off if it is ON).
-
#url ⇒ Object
The URL that represents this button and its current state.
Constructor Details
#initialize(id, text: nil, on_text: "#{id} (on)", off_text: "#{id} (off)", state: false) ⇒ Button
Create a button
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/metaruby/gui/html/button.rb', line 35 def initialize(id, text: nil, on_text: "#{id} (on)", off_text: "#{id} (off)", state: false) if id[0, 1] != '/' id = "/#{id}" elsif id[-1, 1] == '/' id = id[0..-2] end @id = id if text @on_text = text @off_text = text @state = true else @on_text = on_text @off_text = off_text @state = state end end |
Instance Attribute Details
#id ⇒ String (readonly)
The button ID
It is used to generate the button’s #base_url
11 12 13 |
# File 'lib/metaruby/gui/html/button.rb', line 11 def id @id end |
#off_text ⇒ String (readonly)
The text when the button is OFF
21 22 23 |
# File 'lib/metaruby/gui/html/button.rb', line 21 def off_text @off_text end |
#on_text ⇒ String (readonly)
The text when the button is ON
16 17 18 |
# File 'lib/metaruby/gui/html/button.rb', line 16 def on_text @on_text end |
#state ⇒ Object
The current button state
24 25 26 |
# File 'lib/metaruby/gui/html/button.rb', line 24 def state @state end |
Instance Method Details
#base_url ⇒ String
The button base URL
63 |
# File 'lib/metaruby/gui/html/button.rb', line 63 def base_url; "btn://metaruby#{id}" end |
#html_id ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The ID, quoted for HTML
58 |
# File 'lib/metaruby/gui/html/button.rb', line 58 def html_id; id.gsub(/[^\w]/, '_') end |
#render ⇒ String
Render the button as HTML
90 91 92 |
# File 'lib/metaruby/gui/html/button.rb', line 90 def render "<a id=\"#{html_id}\" href=\"#{toggle_url}\">#{text}</a>" end |
#text ⇒ Object
The button text
81 82 83 84 85 |
# File 'lib/metaruby/gui/html/button.rb', line 81 def text if state then off_text else on_text end end |
#toggle_url ⇒ Object
The URL that would toggle the button (i.e. turn it off if it is ON)
67 68 69 70 71 |
# File 'lib/metaruby/gui/html/button.rb', line 67 def toggle_url if state then "#{base_url}#off" else "#{base_url}#on" end end |
#url ⇒ Object
The URL that represents this button and its current state
74 75 76 77 78 |
# File 'lib/metaruby/gui/html/button.rb', line 74 def url if state then "#{base_url}#on" else "#{base_url}#off" end end |