Class: UnderOs::Page::Builder
- Inherits:
-
Object
- Object
- UnderOs::Page::Builder
- Defined in:
- lib/under_os/page/builder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build_children_for(view, nodes) ⇒ Object
- #build_view_from(node) ⇒ Object
- #class_for(node) ⇒ Object
- #find_collection_items_for(collection, nodes) ⇒ Object
- #from_html(source) ⇒ Object
- #from_nodes(list) ⇒ Object
- #options_from_attrs(hash) ⇒ Object
- #select_optgroups_from(nodes) ⇒ Object
Class Method Details
.views_from(source) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/under_os/page/builder.rb', line 3 def self.views_from(source) @inst ||= new if source.is_a?(String) @inst.from_html(source) else @inst.from_nodes(source) end end |
Instance Method Details
#build_children_for(view, nodes) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/under_os/page/builder.rb', line 54 def build_children_for(view, nodes) if view.is_a?(UnderOs::UI::Select) view.optgroups = select_optgroups_from(nodes) elsif view.is_a?(UnderOs::UI::Collection) find_collection_items_for(view, nodes) else from_nodes(nodes).each do |child| view.insert child end end end |
#build_view_from(node) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/under_os/page/builder.rb', line 23 def build_view_from(node) klass = class_for(node) = (node[:attrs] || {}) if klass.instance_methods.include?(:text) && node[:text] [:text] = node[:text] end klass.new().tap do |view| if node[:children] && node[:children].any? build_children_for(view, node[:children]) end end end |
#class_for(node) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/under_os/page/builder.rb', line 38 def class_for(node) if node[:attrs] && node[:attrs][:wrapper].is_a?(String) node[:attrs].delete(:wrapper).constantize else UnderOs::UI::Wrap::WRAPS_TAGS_MAP[node[:tag]] || UnderOs::UI::View end end |
#find_collection_items_for(collection, nodes) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/under_os/page/builder.rb', line 83 def find_collection_items_for(collection, nodes) nodes.each do |node| klass = class_for(node) if klass <= UnderOs::UI::Collection::Item klass.build(collection) { from_nodes(node[:children]) } end # header # footer end end |
#from_html(source) ⇒ Object
12 13 14 15 |
# File 'lib/under_os/page/builder.rb', line 12 def from_html(source) @html ||= UnderOs::Parser::HTML.new from_nodes @html.parse(source) end |
#from_nodes(list) ⇒ Object
17 18 19 20 21 |
# File 'lib/under_os/page/builder.rb', line 17 def from_nodes(list) list.map do |node| build_view_from(node) end end |
#options_from_attrs(hash) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/under_os/page/builder.rb', line 46 def (hash) {}.tap do || hash.each do |key, value| [key] = value end end end |
#select_optgroups_from(nodes) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/under_os/page/builder.rb', line 66 def select_optgroups_from(nodes) nodes = [{tag: 'optgroup', children: nodes}] if nodes[0][:tag] == 'option' nodes.map do |group| {}.tap do || group[:children].each do |option| if option[:tag] == 'option' label = option[:text] value = option[:attrs][:value] || label [value] = label if value && label end end end end end |