Class: Scriptorium::Widget
- Inherits:
-
Object
- Object
- Scriptorium::Widget
- Defined in:
- lib/scriptorium/widgets/widget.rb
Direct Known Subclasses
Defined Under Namespace
Classes: FeaturedPosts, Links, ListWidget, Pages
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
Instance Method Summary collapse
-
#define_invariants ⇒ Object
Invariants.
- #generate ⇒ Object
-
#html_body(css = nil) ⇒ Object
Common HTML body wrapper for widgets.
-
#html_card(card_title = "Widget Card", tag = "widget", content) ⇒ Object
Common HTML card structure for widgets like Links.
-
#html_container(content) ⇒ Object
A generic container for widget content (for those that don’t use cards).
-
#initialize(repo, view) ⇒ Widget
constructor
A new instance of Widget.
- #load_config ⇒ Object
Methods included from Contract
#assume, #check_invariants, enabled?, #invariant, #verify
Methods included from Helpers
#cf_time, #change_config, #clean_slugify, #copy_gem_asset_to_user, #copy_to_clipboard, #d4, #escape_html, #generate_missing_asset_svg, #get_asset_path, #get_from_clipboard, #getvars, #list_gem_assets, #make_dir, #make_tree, #need, #read_commented_file, #read_file, #see, #see_file, #slugify, #substitute, #system!, #view_dir, #write_file, #write_file!, #ymdhms
Methods included from Exceptions
Constructor Details
#initialize(repo, view) ⇒ Widget
Returns a new instance of Widget.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/scriptorium/widgets/widget.rb', line 14 def initialize(repo, view) assume { repo.is_a?(Scriptorium::Repo) } assume { view.is_a?(Scriptorium::View) } @repo = repo @view = view @config = load_config @name = self.class.to_s.split("::").last.downcase @path = "#{@view.dir}/widgets/#@name" define_invariants verify { @repo == repo } verify { @view == view } check_invariants end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/scriptorium/widgets/widget.rb', line 4 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/scriptorium/widgets/widget.rb', line 4 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/scriptorium/widgets/widget.rb', line 4 def path @path end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
4 5 6 |
# File 'lib/scriptorium/widgets/widget.rb', line 4 def repo @repo end |
#view ⇒ Object (readonly)
Returns the value of attribute view.
4 5 6 |
# File 'lib/scriptorium/widgets/widget.rb', line 4 def view @view end |
Instance Method Details
#define_invariants ⇒ Object
Invariants
7 8 9 10 11 12 |
# File 'lib/scriptorium/widgets/widget.rb', line 7 def define_invariants invariant { @repo.is_a?(Scriptorium::Repo) } invariant { @view.is_a?(Scriptorium::View) } invariant { @name.is_a?(String) && !@name.empty? } invariant { @path.is_a?(String) && !@path.empty? } end |
#generate ⇒ Object
30 31 32 |
# File 'lib/scriptorium/widgets/widget.rb', line 30 def generate raise NotImplementedError, "Subclasses must implement the 'generate' method" end |
#html_body(css = nil) ⇒ Object
Common HTML body wrapper for widgets
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/scriptorium/widgets/widget.rb', line 39 def html_body(css = nil) check_invariants assume { css.nil? || css.is_a?(String) } result = "<html>" + (css ? "<head><style>#{css}</style></head>" : "") + "<body>" + yield + "</body></html>" verify { result.is_a?(String) && result.include?("<html>") } check_invariants result end |
#html_card(card_title = "Widget Card", tag = "widget", content) ⇒ Object
Common HTML card structure for widgets like Links
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/scriptorium/widgets/widget.rb', line 53 def html_card(card_title = "Widget Card", tag = "widget", content) check_invariants assume { card_title.is_a?(String) } assume { tag.is_a?(String) } assume { content.is_a?(String) } result = <<~EOS <div class="card mb-3"> <div class="card-body"> <h5 class="card-title"> <button type="button" class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="##{tag}">+</button> <a href="javascript:void(0)" onclick="javascript:load_main('#{tag}-main.html')" style="text-decoration: none; color: black">#{card_title}</a> </h5> <div class="collapse" id="#{tag}"> #{content} </div> </div> </div> EOS verify { result.is_a?(String) && result.include?("card") } check_invariants result end |
#html_container(content) ⇒ Object
A generic container for widget content (for those that don’t use cards)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/scriptorium/widgets/widget.rb', line 79 def html_container(content) check_invariants assume { content.is_a?(String) } result = <<~HTML <div class="widget-container"> #{content} </div> HTML verify { result.is_a?(String) && result.include?("widget-container") } check_invariants result end |
#load_config ⇒ Object
34 35 36 |
# File 'lib/scriptorium/widgets/widget.rb', line 34 def load_config raise NotImplementedError, "Subclasses must implement 'load_config'" end |