class Object
Public Instance Methods
SPACES(indent)
click to toggle source
# File lib/www_app.rb, line 74 def SPACES indent SPACE * indent end
ancestor?(*args)
click to toggle source
# File lib/www_app.rb, line 199 def ancestor? *args !!(find_ancestor *args) end
args_to_traversal_args(*args)
click to toggle source
# File lib/www_app.rb, line 78 def args_to_traversal_args *args tags = nil tag = nil syms = [] args.each { |a| case a when ::Symbol syms << a when ::Array tags = a when ::Hash tag = a tags = [a] else fail ::ArgumentError, "#{args.inspect}" end } tags ||= @tags.dup tag ||= @tag return [tags, tag, syms] end
close() { || ... }
click to toggle source
# File lib/www_app.rb, line 399 def close group = find_nearest(:group) if group stay_or_go_up_to :group final_parent = parent # We set :groups=>true because # we want tags created in the block to create their own # group as a child element. @tag[:groups] = true @tag[:closed] = true yield @tag = final_parent return self end @tag[:closed] = true final_parent = parent orig = @tag if block_given? results = yield # The :yield may have left some opened tags, :input, :br/ # So we make sure we are in the original tag/element # when we want to make some final changes. in_tag(orig) { case when tag?(:form) input(:hidden, :auth_token, :auth_token.to_mustache(:html)) when results.is_a?(::Symbol) && ancestor?(:script) create :text, :skip_escape=>true, :value => results.to_mustache(:mustache, :html) when results.is_a?(::Symbol) create :text, :skip_escape=>true, :value => results.to_mustache(:html) when results.is_a?(::String) text results end } end @tag = final_parent self end
create(name, opts = nil) { || ... }
click to toggle source
# File lib/www_app.rb, line 330 def create name, opts = nil if @tag && @tag[:closed!] go_up end if @tag && @tag[:tag_name] == :_ # Ex: # _.id(:the_body).^(:loading) # div { # } go_up if !@tag[:closed] # Ex: # _.^(:happy) { # a { } # } fail "New tags not allowed here." if (@tag && @tag[:closed]) && !ancestor?(:group) end # === If: # we are creating an HTML element # within a group, then we either start # a new group or stay here. if name != :group && tag_or_ancestor?(:groups) if tag?(:groups) create :group else stay_or_go_up_to_if_exists(:group) end end old = @tag new = {:tag_name=>name} # === Add to parent's children array: if old old[:children] ||= [] old[:children] << new new[:parent] = old else # === Example: :head, :body, etc. @tags << new end @tag = new @tag.merge!(opts) if opts.is_a?(::Hash) if block_given? close { yield } elsif opts.is_a?(::String) close { text opts } end self end
de_ref(tag)
click to toggle source
# File lib/www_app.rb, line 106 def de_ref tag t_name = tag[:tag_name] return tag unless t_name == :_ # === Remove unneeded values from tag: dup = tag.dup dup.delete :tag_name dup.delete :parent # === Find the true parent: parent = tag while parent && [:_, :style, :group].freeze.include?(parent[:tag_name]) parent = parent[:parent] end if !parent parent = {:tag_name=>:body} end # === Merge, Freeze, and Return: t = {} t.merge! parent t.merge! dup t.freeze t end
detect(*raw_args)
click to toggle source
Ex:
detect :body, :style, :span detect [], :body, :a detect {tag}, :body, :a
# File lib/www_app.rb, line 163 def detect *raw_args tags, tag, syms = args_to_traversal_args(*raw_args) found = nil while !found && !tags.empty? found = tags.shift if !syms.include?(found[:tag_name]) if found[:children] tags = found[:children].dup.concat tags end found = nil end end found end
dom_id?()
click to toggle source
# File lib/www_app.rb, line 257 def dom_id? tag && !!tag[:id] end
find_all(*raw_args)
click to toggle source
Ex:
find_all :body, :style, :span find_all [], :body, :a find_all {tag}, :body, :a
# File lib/www_app.rb, line 138 def find_all *raw_args tags, tag, syms = args_to_traversal_args(*raw_args) fail ::ArgumentError, "tag names to find empty: #{syms.inspect}" if syms.empty? found = [] while !tags.empty? t = tags.shift if t[:children] tags = t[:children].dup.concat tags end (found << t) if syms.include?(t[:tag_name]) end found end
find_ancestor(*raw_args)
click to toggle source
# File lib/www_app.rb, line 209 def find_ancestor *raw_args tags, tag, syms = args_to_traversal_args(*raw_args) name = syms.first return nil unless tag ancestor = tag[:parent] while ancestor && !tag?(ancestor, name) ancestor = ancestor[:parent] end return ancestor if tag?(ancestor, name) end
find_nearest(*raw_args)
click to toggle source
# File lib/www_app.rb, line 203 def find_nearest *raw_args tags, tag, syms = args_to_traversal_args(*raw_args) return tag if tag?(tag, syms.first) find_ancestor *raw_args end
first_class()
click to toggle source
# File lib/www_app.rb, line 253 def first_class tag[:class] && tag[:class].first end
go_up()
click to toggle source
# File lib/www_app.rb, line 248 def go_up @tag = @tag[:parent] self end
go_up_to(name)
click to toggle source
# File lib/www_app.rb, line 228 def go_up_to name go_up_to_if_exists name fail "No parent found: #{name.inspect}" unless tag?(name) self end
go_up_to_if_exists(name)
click to toggle source
# File lib/www_app.rb, line 222 def go_up_to_if_exists name target = find_ancestor name (@tag = target) if target self end
in_tag(t) { || ... }
click to toggle source
# File lib/www_app.rb, line 313 def in_tag t if t.is_a?(::Symbol) in_tag(detect(t)) { yield } return self else # ============================= orig = @tag @tag = t yield @tag = orig nil end # === if t == :head end
parent(*args)
click to toggle source
Ex:
parent parent tag
# File lib/www_app.rb, line 299 def parent *args case when args.length == 0 tag = @tag when args.length == 1 && args.first.is_a?(::Hash) tag = args.first else fail ::ArgumentError, "Unknown args: #{args.inspect}" end # === case tag && tag[:parent] end
parent?(*args)
click to toggle source
Ex:
parent? parent? :body parent? tag, :div
# File lib/www_app.rb, line 268 def parent? *args case when args.length == 0 tag = @tag[:parent] name = nil when args.length == 1 && args.first.is_a?(::Symbol) tag = @tag name = args.first when args.length == 2 tag = args.first name = args.last else fail ::ArgumentError, "Unknown args: #{args.inspect}" end # === case p = parent(tag) return true if p && !name return true if p && p[:tag_name] == name return true if !p && name == :body false end
raw_text(str)
click to toggle source
# File lib/www_app.rb, line 393 def raw_text str fail "No block allowed." if block_given? create(:text, :skip_escape=>true, :value=>str, :closed=>true) go_up end
stay_or_go_up_to(name)
click to toggle source
# File lib/www_app.rb, line 242 def stay_or_go_up_to name stay_or_go_up_to_if_exists name fail "No parent found: #{name.inspect}" unless tag?(name) self end
stay_or_go_up_to_if_exists(name)
click to toggle source
# File lib/www_app.rb, line 234 def stay_or_go_up_to_if_exists name return self if tag?(name) target = find_ancestor(name) (@tag = target) if target self end
tag?(*args)
click to toggle source
# File lib/www_app.rb, line 180 def tag? *args case args.size when 1 tag = @tag name = args.first when 2 tag = args.first name = args.last else fail "Unknown args: #{args.inspect}" end tag && (tag[:tag_name] == name || !!tag[name]) end
tag_or_ancestor?(*args)
click to toggle source
# File lib/www_app.rb, line 195 def tag_or_ancestor? *args !!find_nearest(*args) end
text(str)
click to toggle source
# File lib/www_app.rb, line 387 def text str fail "No block allowed." if block_given? create(:text, :value=>str, :closed=>true) go_up end
use(www_app)
click to toggle source
# File lib/www_app.rb, line 102 def use www_app instance_eval &(www_app.blok) end