mwmitchell-bluebird 0.0.1

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 (6) hide show
  1. data/LICENSE +3 -0
  2. data/README.rdoc +30 -0
  3. data/example.rb +56 -0
  4. data/lib/bluebird.rb +60 -0
  5. data/lib/core_ext.rb +23 -0
  6. metadata +67 -0
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ Copyright 2009 Matt Mitchell (goodieboy@gmail.com)
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
@@ -0,0 +1,30 @@
1
+ =Bluebird
2
+
3
+ ==Description
4
+ An XSL-like DSL for transforming XML, using Ruby
5
+
6
+ ==Install
7
+ gem sources -a http://gems.github.com
8
+ sudo gem install mwmitchell-bluebird
9
+
10
+ ==Example
11
+ bb = Bluebird.xml(XML)
12
+
13
+ bb.match '/' do |n|
14
+ build do
15
+ div bb.call_template('//PLANT')
16
+ end
17
+ end
18
+
19
+ bb.match '//PLANT' do |n|
20
+ build do
21
+ div do
22
+ dt 'price'
23
+ dd n.at('PRICE').inner_text
24
+ dt 'name'
25
+ dd n.at('COMMON').inner_text
26
+ end
27
+ end
28
+ end
29
+
30
+ bb.transform
@@ -0,0 +1,56 @@
1
+ XML = %(<CATALOG>
2
+ <PLANT>
3
+ <COMMON>Bloodroot</COMMON>
4
+ <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
5
+ <ZONE>4</ZONE>
6
+ <LIGHT>Mostly Shady</LIGHT>
7
+ <PRICE>$2.44</PRICE>
8
+ <AVAILABILITY>031599</AVAILABILITY>
9
+ </PLANT>
10
+ <PLANT>
11
+ <COMMON>Columbine</COMMON>
12
+ <BOTANICAL>Aquilegia canadensis</BOTANICAL>
13
+ <ZONE>3</ZONE>
14
+ <LIGHT>Mostly Shady</LIGHT>
15
+ <PRICE>$9.37</PRICE>
16
+ <AVAILABILITY>030699</AVAILABILITY>
17
+ </PLANT>
18
+ </CATALOG>)
19
+
20
+ EXPECTED = %(
21
+ <div>
22
+ <div>
23
+ <dt>price</dt>
24
+ <dd>$2.44</dd>
25
+ <dt>name</dt>
26
+ <dd>Bloodroot</dd>
27
+ </div>
28
+ <div>
29
+ <dt>price</dt>
30
+ <dd>$9.37</dd>
31
+ <dt>name</dt>
32
+ <dd>Columbine</dd>
33
+ </div>
34
+ </div>
35
+ ).gsub(/\n|\t/, '').gsub(/ +/, '').to_s # remove the spaces, tabs and line breaks
36
+
37
+ bb = Bluebird.xml(XML)
38
+
39
+ bb.match '/' do |n|
40
+ build do
41
+ div bb.call_template('//PLANT')
42
+ end
43
+ end
44
+
45
+ bb.match '//PLANT' do |n|
46
+ build do
47
+ div do
48
+ dt 'price'
49
+ dd n.at('PRICE').inner_text
50
+ dt 'name'
51
+ dd n.at('COMMON').inner_text
52
+ end
53
+ end
54
+ end
55
+
56
+ EXPECTED == bb.transform.to_s == true
@@ -0,0 +1,60 @@
1
+ proc {|base, files|
2
+ $: << base unless $:.include?(base) || $:.include?(File.expand_path(base))
3
+ files.each {|f| require f}
4
+ }.call(File.dirname(__FILE__), ['core_ext'])
5
+
6
+ require 'rubygems'
7
+ require 'hpricot'
8
+
9
+ module Bluebird
10
+
11
+ def self.xml(xml)
12
+ DSL.new(Hpricot::XML(xml))
13
+ end
14
+
15
+ class DSL
16
+
17
+ attr_reader :doc, :templates
18
+
19
+ def initialize(hpricot)
20
+ @doc = hpricot
21
+ @templates = []
22
+ end
23
+
24
+ def build(*a, &b)
25
+ Hpricot.build(*a, &b)
26
+ end
27
+
28
+ def match(pattern, &block)
29
+ @templates << {:pattern=>pattern, :block=>block}
30
+ end
31
+
32
+ def transform
33
+ tpl = @templates.first
34
+ new_doc=nil
35
+ @doc.search(tpl[:pattern]).each do |n|
36
+ result = instance_exec(n, &tpl[:block])
37
+ if new_doc.nil?
38
+ new_doc = Hpricot::XML(result.to_s)
39
+ else
40
+ new_doc << result
41
+ end
42
+ end
43
+ new_doc
44
+ end
45
+
46
+ def call_template(pattern)
47
+ matches = @doc.search(pattern)
48
+ tpl_matches=[]
49
+ tpl = @templates.detect do |t|
50
+ tpl_matches = @doc.search(t[:pattern])
51
+ tpl_matches.any?{|n| matches.include?(n) }
52
+ end
53
+ @doc.search(tpl[:pattern]).collect do |n|
54
+ instance_exec n, &tpl[:block]
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,23 @@
1
+
2
+ #http://eigenclass.org/hiki/bounded+space+instance_exec
3
+
4
+ class Object
5
+ module InstanceExecHelper; end
6
+ include InstanceExecHelper
7
+ def instance_exec(*args, &block)
8
+ begin
9
+ old_critical, Thread.critical = Thread.critical, true
10
+ n = 0
11
+ n += 1 while respond_to?(mname="__instance_exec#{n}")
12
+ InstanceExecHelper.module_eval{ define_method(mname, &block) }
13
+ ensure
14
+ Thread.critical = old_critical
15
+ end
16
+ begin
17
+ ret = send(mname, *args)
18
+ ensure
19
+ InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
20
+ end
21
+ ret
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mwmitchell-bluebird
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Mitchell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-09 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: An XSL-like DSL for transforming XML, written in Ruby
25
+ email: goodieboy@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - LICENSE
32
+ - README.rdoc
33
+ - example.rb
34
+ files:
35
+ - example.rb
36
+ - lib/core_ext.rb
37
+ - lib/bluebird.rb
38
+ - LICENSE
39
+ - README.rdoc
40
+ has_rdoc: true
41
+ homepage: http://github.com/mwmitchell/bluebird
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: An XSL-like DSL for transforming XML, written in Ruby
66
+ test_files: []
67
+