richerb 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/richerb.rb +101 -5
  2. metadata +18 -2
data/lib/richerb.rb CHANGED
@@ -1,7 +1,103 @@
1
- class RichERB
2
- def self.tag(tag)
3
- puts "<#{tag}>"
4
- yield
5
- puts "</#{tag}>"
1
+ require 'nokogiri'
2
+
3
+ module RichERB
4
+
5
+ class CustomTag
6
+ attr_accessor :doc
7
+ attr_accessor :parent
8
+ attr_accessor :context
9
+ attr_accessor :arity # :nodoc:
10
+
11
+ def self.with root, &block
12
+ new({}, root, &block)
13
+ end
14
+
15
+ def initialize options = {}, tag = 'div', root = nil, &block
16
+ if root
17
+ @doc = root.doc
18
+ @parent = @doc
19
+ else
20
+ @doc = Node.new(tag, options)
21
+ @parent = @doc
22
+ end
23
+
24
+ @context = nil
25
+ @arity = nil
26
+ @ns = nil
27
+
28
+ return unless block_given?
29
+
30
+
31
+ @arity = block.arity
32
+ if @arity <= 0
33
+ @context = eval('self', block.binding)
34
+ instance_eval(&block)
35
+ else
36
+ yield self
37
+ end
38
+
39
+ @parent = @doc
40
+ end
41
+
42
+ def method_missing method, *args, &block
43
+ if @context && @context.respond_to?(method)
44
+ @context.send(method, *args, &block)
45
+ else
46
+ options = args[0]
47
+ node = Node.new(method.to_s, options)
48
+
49
+
50
+ insert(node, &block)
51
+ end
52
+ end
53
+
54
+ def insert(node, &block)
55
+ node.parent = @parent
56
+
57
+ if block_given?
58
+ old_parent = @parent
59
+ @parent = node
60
+ @arity ||= block.arity
61
+ if @arity <= 0
62
+ instance_eval(&block)
63
+ else
64
+
65
+ block.call(@doc)
66
+ end
67
+ @parent = old_parent
68
+ end
69
+ @parent.children << node
70
+ end
71
+ end
72
+
73
+ class TagNode
74
+ attr_accessor :children
75
+ attr_accessor :parent
76
+ attr_accessor :name
77
+ attr_accessor :attributes
78
+
79
+ def initialize name, attributes = {}
80
+ @name = name
81
+ @children = []
82
+ @parent = nil
83
+ @attributes = {}
84
+ attributes.each {|k,v| @attributes[k.to_sym] = v } unless attributes.nil?
85
+ end
86
+
87
+ def to_xml_node(xml)
88
+ xml.send(@name.to_sym, @attributes) {
89
+ @children.each {|node| node.to_xml_node(xml) }
90
+ }
91
+ end
92
+
93
+ def to_xml
94
+ builder = Nokogiri::XML::Builder.new do |xml|
95
+ xml.send(@name.to_sym, @attributes) {
96
+ @children.each {|node| node.to_node(xml) }
97
+ }
98
+ end
99
+
100
+ builder.doc.root.to_xml
101
+ end
6
102
  end
7
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: richerb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-06-13 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description:
15
31
  email: ericparshall@gmail.com
16
32
  executables: []