Permalink
Browse files

entering the enterprise

  • Loading branch information...
0 parents commit 2aa96dd7ee5418d462fbaeb883e0cf349d3b0c2b @tenderlove committed Jun 18, 2009
@@ -0,0 +1,23 @@
+# -*- ruby -*-
+
+require 'autotest/restart'
+
+# Autotest.add_hook :initialize do |at|
+# at.extra_files << "../some/external/dependency.rb"
+#
+# at.libs << ":../some/external"
+#
+# at.add_exception 'vendor'
+#
+# at.add_mapping(/dependency.rb/) do |f, _|
+# at.files_matching(/test_.*rb$/)
+# end
+#
+# %w(TestA TestB).each do |klass|
+# at.extra_class_map[klass] = "test/test_misc.rb"
+# end
+# end
+
+# Autotest.add_hook :run_command do |at|
+# system "rake build"
+# end
@@ -0,0 +1,6 @@
+=== 1.0.0 / 2009-06-17
+
+* 1 major enhancement
+
+ * Birthday!
+
@@ -0,0 +1,7 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+bin/enterprise
+lib/enterprise.rb
+test/test_enterprise.rb
@@ -0,0 +1,48 @@
+= enterprise
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2009 FIX
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+Hoe.spec 'enterprise' do
+ developer('Aaron Patterson', 'aaronp@rubyforge.org')
+ self.extra_deps = ['ruby2ruby', 'ruby_parser', 'nokogiri', 'polyglot']
+end
+
+# vim: syntax=ruby
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'enterprise'
+require 'find'
+
+if File.directory? ARGV[0]
+ Find.find ARGV[0] do |file|
+ next if File.directory? file
+ next unless File.extname(file) == '.rb'
+ end
+else
+end
@@ -0,0 +1,12 @@
+require 'nokogiri'
+require 'ruby_parser'
+require 'ruby2ruby'
+require 'polyglot'
+
+require 'enterprise/sexp_hacks'
+require 'enterprise/sexml'
+require 'enterprise/file_loader'
+
+module Enterprise
+ VERSION = '1.0.0'
+end
@@ -0,0 +1,10 @@
+module Enterprise
+ module FileLoader
+ def self.load filename, options = nil, &block
+ sexml = Nokogiri::XML(File.read(filename)) { |cmd| cmd.noblanks }
+ Kernel.eval sexml.to_ruby, TOPLEVEL_BINDING
+ end
+ end
+end
+
+Polyglot.register 'xml', Enterprise::FileLoader
@@ -0,0 +1,48 @@
+require 'nokogiri'
+require 'ruby2ruby'
+
+module Enterprise
+ def self.SEXML codes, filename = nil
+ sexp = RubyParser.new.parse codes, filename
+ sexp.to_xml
+ end
+
+ class XMLToSexp
+ def visit node
+ if ! node['value']
+ return s(*node.children.map { |child| child.accept self })
+ else
+ case node['type']
+ when 'Symbol'
+ node['value'].to_sym
+ when 'Regexp'
+ Marshal.load node['value'].unpack('m').first
+ when 'NilClass'
+ nil
+ when 'Fixnum', 'Bignum'
+ Integer(node['value'])
+ when 'Float'
+ Float(node['value'])
+ else
+ node['value']
+ end
+ end
+ end
+ end
+end
+
+class Nokogiri::XML::Node
+ def to_sexp
+ accept Enterprise::XMLToSexp.new
+ end
+
+ def to_ruby
+ Ruby2Ruby.new.process(to_sexp)
+ end
+end
+
+class Nokogiri::XML::Document
+ def to_ruby
+ Ruby2Ruby.new.process(root.to_sexp)
+ end
+end
@@ -0,0 +1,31 @@
+class Sexp
+ include Nokogiri::XML
+
+ def to_xml doc = Document.new, parent = doc.root = Node.new('s', doc)
+ each do |node|
+ case node
+ when Sexp
+ new_parent = Node.new('s', parent.document)
+ parent.add_child new_parent
+ node.to_xml doc, new_parent
+ else
+ name = node.to_s
+ if node.to_s.empty? || node.to_s !~ /^[A-Za-z]+$/
+ name = 'special'
+ end
+
+ n = Node.new(name, parent.document)
+ n['type'] = node.class.name
+
+ if Regexp === node
+ n['value'] = [Marshal.dump(node)].pack('m')
+ else
+ n['value'] = node.to_s
+ end
+
+ parent << n
+ end
+ end
+ doc
+ end
+end
@@ -0,0 +1,41 @@
+<?xml version="1.0"?>
+<s>
+ <class type="Symbol" value="class"/>
+ <HelloWorld type="Symbol" value="HelloWorld"/>
+ <special type="NilClass" value=""/>
+ <s>
+ <scope type="Symbol" value="scope"/>
+ <s>
+ <defn type="Symbol" value="defn"/>
+ <innernet type="Symbol" value="innernet"/>
+ <s>
+ <args type="Symbol" value="args"/>
+ </s>
+ <s>
+ <scope type="Symbol" value="scope"/>
+ <s>
+ <block type="Symbol" value="block"/>
+ <s>
+ <call type="Symbol" value="call"/>
+ <s>
+ <str type="Symbol" value="str"/>
+ <special type="String" value="the innernet is awesome"/>
+ </s>
+ <sub type="Symbol" value="sub"/>
+ <s>
+ <arglist type="Symbol" value="arglist"/>
+ <s>
+ <lit type="Symbol" value="lit"/>
+ <special type="Regexp" value="BAgvB25uAA==&#10;"/>
+ </s>
+ <s>
+ <str type="Symbol" value="str"/>
+ <bar type="String" value="bar"/>
+ </s>
+ </s>
+ </s>
+ </s>
+ </s>
+ </s>
+ </s>
+</s>
@@ -0,0 +1,5 @@
+class HelloWorld
+ def innernet
+ "the innernet is awesome".sub(/nn/, 'bar')
+ end
+end
@@ -0,0 +1,39 @@
+require 'test/unit'
+require 'rubygems'
+require 'enterprise'
+
+class TestEnterprise < Test::Unit::TestCase
+ def setup
+ @filename = File.join(File.dirname(__FILE__), 'assets', 'reference.rb')
+ @codes = File.read(@filename)
+ end
+
+ def teardown
+ Object.send(:remove_const, :HelloWorld) if defined? HelloWorld
+ end
+
+ def test_sexp_parse
+ sexml = Enterprise::SEXML @codes, @filename
+ assert sexml
+ assert_equal 11, sexml.css('s').length
+ end
+
+ def test_sexml_can_be_converted_to_sexp
+ sexml = Enterprise::SEXML @codes, @filename
+ ruby_codes = sexml.to_sexp
+ assert_instance_of Sexp, ruby_codes
+ end
+
+ def test_sexml_can_be_converted_to_ruby
+ sexml = Enterprise::SEXML @codes, @filename
+ ruby_codes = sexml.to_ruby
+ m = Module.new
+ m.module_eval ruby_codes
+ assert m::HelloWorld.new.innernet
+ end
+
+ def test_require_some_xml
+ require 'assets/hello_world'
+ assert_equal 'the ibarernet is awesome', ::HelloWorld.new.innernet
+ end
+end

0 comments on commit 2aa96dd

Please sign in to comment.