unify 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
@@ -0,0 +1,28 @@
1
+ = unify
2
+
3
+ unify does the right thing.
4
+
5
+ == Copyright
6
+
7
+ (The MIT License)
8
+
9
+ Copyright (c) 2009 {geemus (Wesley Beary)}[http://github.com/geemus]
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining
12
+ a copy of this software and associated documentation files (the
13
+ "Software"), to deal in the Software without restriction, including
14
+ without limitation the rights to use, copy, modify, merge, publish,
15
+ distribute, sublicense, and/or sell copies of the Software, and to
16
+ permit persons to whom the Software is furnished to do so, subject to
17
+ the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be
20
+ included in all copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "unify"
8
+ gem.summary = %Q{does the right thing}
9
+ gem.description = %Q{simple web frameworkings}
10
+ gem.email = "wbeary@engineyard.com"
11
+ gem.homepage = "http://github.com/geemus/unify"
12
+ gem.authors = ["Wesley Beary"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+ task :test => :check_dependencies
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION')
46
+ version = File.read('VERSION')
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "unify #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,15 @@
1
+ __DIR__ = File.dirname(__FILE__)
2
+
3
+ $LOAD_PATH.unshift __DIR__ unless
4
+ $LOAD_PATH.include?(__DIR__) ||
5
+ $LOAD_PATH.include?(File.expand_path(__DIR__))
6
+
7
+ require 'rubygems'
8
+ require 'rack'
9
+
10
+ require 'unify/css'
11
+ require 'unify/html'
12
+ require 'unify/http'
13
+
14
+ module Unify
15
+ end
@@ -0,0 +1,44 @@
1
+ module Unify
2
+
3
+ def self.css(&block)
4
+ css = Unify::CSS.new(&block)
5
+ if block_given?
6
+ css.instance_eval(&block)
7
+ end
8
+ css.to_css
9
+ end
10
+
11
+ class CSS
12
+
13
+ def initialize(&block)
14
+ @selector_stack = []
15
+ @styles = {}
16
+ end
17
+
18
+ def _(name, properties = {}, &block)
19
+ @selector_stack.push(name.to_s)
20
+ (@styles[@selector_stack.join(' ')] ||= {}).merge!(properties)
21
+ if block_given?
22
+ instance_eval(&block)
23
+ end
24
+ @selector_stack.pop
25
+ end
26
+
27
+ def to_css
28
+ css = ''
29
+ for selector in @styles.keys.sort
30
+ unless @styles[selector].empty?
31
+ css << "#{selector}{"
32
+ for key in @styles[selector].keys.sort
33
+ css << "#{key}:#{@styles[selector][key]};"
34
+ end
35
+ css.chop!
36
+ css << "}"
37
+ end
38
+ end
39
+ css
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,49 @@
1
+ module Unify
2
+
3
+ def self.html(&block)
4
+ html = Unify::HTML.new(&block)
5
+ if block_given?
6
+ html.instance_eval(&block)
7
+ end
8
+ html.to_html
9
+ end
10
+
11
+ class HTML
12
+
13
+ def initialize(&block)
14
+ @dom_stack = [{ :name => 'html', :properties => {}, :children => [] }]
15
+ end
16
+
17
+ def _(name, properties = {}, &block)
18
+ element = { :name => name.to_s, :properties => properties, :children => [] }
19
+ @dom_stack.last[:children] << element
20
+ @dom_stack.push(element)
21
+ if block_given?
22
+ val = instance_eval(&block)
23
+ if val.is_a?(String)
24
+ @dom_stack.last[:inner_html] = val
25
+ end
26
+ end
27
+ @dom_stack.pop
28
+ end
29
+
30
+ def to_html(element=@dom_stack.first)
31
+ html = "<#{element[:name]}"
32
+ for key in element[:properties].keys.sort
33
+ html << %Q{ #{key}="#{element[:properties][key]}"}
34
+ end
35
+ if !element[:children].empty? || element[:inner_html]
36
+ html << '>'
37
+ for child in element[:children]
38
+ html << to_html(child)
39
+ end
40
+ html << "#{element[:inner_html]}</#{element[:name]}>"
41
+ else
42
+ html << '/>'
43
+ end
44
+ html
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,57 @@
1
+ module Unify
2
+
3
+ def self.http(&block)
4
+ Unify::HTTP.new(&block)
5
+ end
6
+
7
+ class HTTP
8
+
9
+ def initialize(&block)
10
+ @path_stack = []
11
+ @routes = {}
12
+ if block_given?
13
+ instance_eval(&block)
14
+ end
15
+ @finished = true
16
+ self
17
+ end
18
+
19
+ def _(path, options = {}, &block)
20
+ return if @finished
21
+ options = { :method => 'GET' }.merge!(options)
22
+ case path
23
+ when Regexp
24
+ @path_stack.push(path.source)
25
+ when String
26
+ @path_stack.push(Regexp.escape(path))
27
+ end
28
+ @routes[options[:method]] ||= []
29
+ @routes[options[:method]] << [/^#{@path_stack.join('/').squeeze('/')}$/ix, block]
30
+ if block_given?
31
+ instance_eval(&block)
32
+ end
33
+ @path_stack.pop
34
+ end
35
+
36
+ def call(env)
37
+ if @routes[env['REQUEST_METHOD']]
38
+ for route in @routes[env['REQUEST_METHOD']]
39
+ if match = route[0].match(env['PATH_INFO'])
40
+ @response = Rack::Response.new
41
+ unless @response.write(route[1].call(*match.captures))
42
+ @response = nil
43
+ end
44
+ break
45
+ end
46
+ end
47
+ end
48
+ unless @response
49
+ @response = Rack::Response.new('Page Not Found')
50
+ @response.status = 404
51
+ end
52
+ @response.finish
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), 'lib/unify')
2
+
3
+ def greeter(greeting, source)
4
+ Unify.html do
5
+ _('head') do
6
+ _('title') { 'underscore does the right thing' }
7
+ _('link', 'charset' => 'utf-8', 'href' => 'styles.css', 'media' => 'all', 'rel' => 'stylesheet', 'type' => 'text/css')
8
+ end
9
+ _('body') do
10
+ _('h1') { "#{greeting} from #{source}" }
11
+ end
12
+ end
13
+ end
14
+
15
+ router = Unify.http do
16
+
17
+ _('/') do
18
+
19
+ _('styles.css') do
20
+ Unify.css do
21
+ _('body', 'background-color' => '#EEE', 'text-align' => 'center')
22
+ _('h1', 'color' => '#666', 'font-size' => '1.5em')
23
+ end
24
+ end
25
+
26
+ _(/(.*)_from_(.*)/) do |greeting, name|
27
+ greeter(greeting, name)
28
+ end
29
+
30
+ greeter('hello', 'underscore')
31
+
32
+ end
33
+
34
+ end
35
+
36
+ Rack::Handler::WEBrick.run(router, :Port => 9292)
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), 'tests_helper')
2
+
3
+ Shindo.tests('css') do
4
+
5
+ test('empty') do
6
+ @css = Unify.css
7
+ @css == ''
8
+ end
9
+
10
+ test('selector with no properties') do
11
+ @css = Unify.css do
12
+ _('div')
13
+ end
14
+ @css == ''
15
+ end
16
+
17
+ test('selector with one property') do
18
+ @css = Unify.css do
19
+ _('div', 'display' => 'none')
20
+ end
21
+ @css == 'div{display:none}'
22
+ end
23
+
24
+
25
+ test('selector with many property') do
26
+ @css = Unify.css do
27
+ _('div', 'background-color' => '#EEE', 'color' => '#666', 'display' => 'none')
28
+ end
29
+ @css == 'div{background-color:#EEE;color:#666;display:none}'
30
+ end
31
+
32
+ test('nested selectors') do
33
+ @css = Unify.css do
34
+ _('ul', 'margin' => 0, 'padding' => 0) do
35
+ _('li', 'list-style' => 'none')
36
+ end
37
+ end
38
+ @css == 'ul{margin:0;padding:0}ul li{list-style:none}'
39
+ end
40
+
41
+ end
@@ -0,0 +1,39 @@
1
+ require File.join(File.dirname(__FILE__), 'tests_helper')
2
+
3
+ Shindo.tests('html') do
4
+
5
+ test('empty') do
6
+ Unify.html == '<html/>'
7
+ end
8
+
9
+ test('element with no properties') do
10
+ @html = Unify.html do
11
+ _('div') { 'inner html' }
12
+ end
13
+ @html == '<html><div>inner html</div></html>'
14
+ end
15
+
16
+ test('element with one property') do
17
+ @html = Unify.html do
18
+ _('div', 'class' => 'foo') { 'inner html' }
19
+ end
20
+ @html == '<html><div class="foo">inner html</div></html>'
21
+ end
22
+
23
+ test('element with many properties') do
24
+ @html = Unify.html do
25
+ _('div', 'class' => 'foo', 'id' => 'bar', 'style' => 'display:none') { 'inner html' }
26
+ end
27
+ @html == '<html><div class="foo" id="bar" style="display:none">inner html</div></html>'
28
+ end
29
+
30
+ test('nested elements') do
31
+ @html = Unify.html do
32
+ _('head') do
33
+ _('title') { 'inner html' }
34
+ end
35
+ end
36
+ @html == '<html><head><title>inner html</title></head></html>'
37
+ end
38
+
39
+ end
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__), 'tests_helper')
2
+
3
+ def request(http, method, path)
4
+ http.call('REQUEST_METHOD' => method, 'PATH_INFO' => path)[2]
5
+ end
6
+
7
+ Shindo.tests('http') do
8
+
9
+ tests('empty') do
10
+
11
+ test('get') do
12
+ @http = Unify.http
13
+ @response = request(@http, 'GET', '/')
14
+ @response.status == 404
15
+ end
16
+
17
+ test('post') do
18
+ @http = Unify.http
19
+ @response = request(@http, 'POST', '/')
20
+ @response.status == 404
21
+ end
22
+
23
+ end
24
+
25
+ tests('basic routes') do
26
+
27
+ test('string route') do
28
+ @http = Unify.http do
29
+ _('/') { 'body' }
30
+ end
31
+ @response = request(@http, 'GET', '/')
32
+ @response.body == ['body']
33
+ end
34
+
35
+ test('regex capture route') do
36
+ @http = Unify.http do
37
+ _(/\/(.*)/) { |param| param }
38
+ end
39
+ @response = request(@http, 'GET', '/body')
40
+ @response.body == ['body']
41
+ end
42
+
43
+ end
44
+
45
+ tests('nested routes') do
46
+
47
+ test('nested route concatenation') do
48
+ @http = Unify.http do
49
+ _('/') do
50
+ _('things') { 'things' }
51
+ end
52
+ end
53
+ @response = request(@http, 'GET', '/things')
54
+ @response.body == ['things']
55
+ end
56
+
57
+ test('nested route which returns nil') do
58
+ @http = Unify.http do
59
+ _('/') do
60
+ _('things') { 'things' }
61
+ end
62
+ end
63
+ @response = request(@http, 'GET', '/')
64
+ @response.status == 404
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'unify'
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{unify}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Wesley Beary"]
12
+ s.date = %q{2009-11-03}
13
+ s.description = %q{simple web frameworkings}
14
+ s.email = %q{wbeary@engineyard.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/unify.rb",
25
+ "lib/unify/css.rb",
26
+ "lib/unify/html.rb",
27
+ "lib/unify/http.rb",
28
+ "naive.rb",
29
+ "tests/css_tests.rb",
30
+ "tests/html_tests.rb",
31
+ "tests/http_tests.rb",
32
+ "tests/tests_helper.rb",
33
+ "unify.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/geemus/unify}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{does the right thing}
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Wesley Beary
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-03 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: simple web frameworkings
17
+ email: wbeary@engineyard.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - .document
26
+ - .gitignore
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION
30
+ - lib/unify.rb
31
+ - lib/unify/css.rb
32
+ - lib/unify/html.rb
33
+ - lib/unify/http.rb
34
+ - naive.rb
35
+ - tests/css_tests.rb
36
+ - tests/html_tests.rb
37
+ - tests/http_tests.rb
38
+ - tests/tests_helper.rb
39
+ - unify.gemspec
40
+ has_rdoc: true
41
+ homepage: http://github.com/geemus/unify
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: does the right thing
68
+ test_files: []
69
+