Class: FreeDom
- Inherits:
-
Domle
- Object
- Domle
- FreeDom
- Defined in:
- lib/free_dom.rb
Instance Method Summary collapse
-
#doc ⇒ Object
used within the scope of the script tags.
-
#initialize(xml, debug: false) ⇒ FreeDom
constructor
A new instance of FreeDom.
- #script ⇒ Object
Constructor Details
#initialize(xml, debug: false) ⇒ FreeDom
Returns a new instance of FreeDom.
10 11 12 13 14 15 16 17 18 19 20 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/free_dom.rb', line 10 def initialize(xml, debug: false) @doc, @debug = Rexle.new(xml), debug h = {} @doc.root.each_recursive do |e| h[e.name.to_sym] ||= {} # if there's a custom attribute, add a default trigger called trigger_change a = e.attributes.keys.reject {|x| i(id name class).include? x } e.attributes.merge!({trigger_change: a.first}) if a.any? # add a trigger attribute for each *on* event attribute events = e.attributes.keys.select {|x| x =~ /^on/}\ .map {|x| 'trigger_' + x.to_s[/(?<=^on)\w+/]} e.attributes.merge! events.zip(['']*events.length).to_h h[e.name.to_sym].merge!(e.attributes) end @defined_elements = {} h.each do |name, attributelist| klass = Class.new(Domle::Element) do a = attributelist.keys triggers = a.select {|x| x =~ /^trigger_/ } attr2_accessor *((a - triggers) + i(onchange)).uniq triggers.each do |x| trigger = x.to_s[/(?<=^trigger_).*/].to_sym puts 'trigger: ' + trigger.inspect if @debug define_method(trigger) do statement = method(('on' + trigger.to_s).to_sym).call eval statement, $env if statement end if trigger == :change then attribute = attributelist[x].to_sym define_method((attribute.to_s + '=').to_sym) do |val| oldval = attributes[attribute] attributes[attribute] = val @rexle.refresh if @rexle change() unless val == oldval val end end end end custom_class = FreeDom.const_set name.to_s.capitalize, klass @defined_elements.merge!({name => custom_class}) end # remove the trigger declaration attributes @doc.root.each_recursive do |e| e.attributes.keys.select {|x| x =~ /^trigger_/ }\ .each {|x| e.attributes.delete x } end super(@doc.root.xml) script() end |
Instance Method Details
#doc ⇒ Object
used within the scope of the script tags
93 94 95 |
# File 'lib/free_dom.rb', line 93 def doc() self end |
#script ⇒ Object
97 98 99 100 101 |
# File 'lib/free_dom.rb', line 97 def script() s = @doc.root.xpath('//script').map {|x| x.text.unescape }.join eval s $env = binding end |