underscore 0.0.0

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.
@@ -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
+ = underscore
2
+
3
+ underscore 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 = "underscore"
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/underscore"
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 = "underscore #{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,16 @@
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 'underscore/css'
11
+ require 'underscore/html'
12
+ require 'underscore/http'
13
+
14
+ module Underscore
15
+ end
16
+ US = Underscore
@@ -0,0 +1,44 @@
1
+ module Underscore
2
+
3
+ def self.css(&block)
4
+ css = Underscore::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, value in @styles[selector]
33
+ css << "#{key}:#{value};"
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 Underscore
2
+
3
+ def self.html(&block)
4
+ html = Underscore::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,52 @@
1
+ module Underscore
2
+
3
+ def self.http(&block)
4
+ Underscore::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
+ for route in @routes[env['REQUEST_METHOD']]
38
+ if match = route[0].match(env['PATH_INFO'])
39
+ response = Rack::Response.new(route[1].call(*match.captures))
40
+ break
41
+ end
42
+ end
43
+ unless response
44
+ response = Rack::Response.new('Page Not Found')
45
+ response.status = 404
46
+ end
47
+ response.finish
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), 'lib/underscore')
2
+
3
+ def greeter(greeting, source)
4
+ US.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 = US.http do
16
+
17
+ _('/') do
18
+
19
+ _('styles.css') do
20
+ US.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,24 @@
1
+ require File.join(File.dirname(__FILE__), 'tests_helper')
2
+
3
+ Shindo.tests('css') do
4
+
5
+ test('empty') do
6
+ css = US.css
7
+ css == ''
8
+ end
9
+
10
+ test('selector without properties') do
11
+ css = US.css do
12
+ _('div')
13
+ end
14
+ css == ''
15
+ end
16
+
17
+ test('selector with properties') do
18
+ css = US.css do
19
+ _('div', 'display' => 'none')
20
+ end
21
+ css == 'div{display:none}'
22
+ end
23
+
24
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), 'tests_helper')
2
+
3
+ Shindo.tests('html') do
4
+
5
+ test('empty') do
6
+ US.html == '<html/>'
7
+ end
8
+
9
+ test('nested elements') do
10
+ html = US.html do
11
+ _('head') do
12
+ _('title') { 'nested' }
13
+ end
14
+ end
15
+ html == '<html><head><title>nested</title></head></html>'
16
+ end
17
+
18
+ test('element with property') do
19
+ html = US.html do
20
+ _('div', 'class' => 'foo') { 'content' }
21
+ end
22
+ html == '<html><div class="foo">content</div></html>'
23
+ end
24
+
25
+ test('element with properties') do
26
+ html = US.html do
27
+ _('div', 'class' => 'foo', 'id' => 'bar') { 'content' }
28
+ end
29
+ html == '<html><div class="foo" id="bar">content</div></html>'
30
+ end
31
+
32
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'underscore'
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: underscore
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-10-31 00:00:00 -07: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/underscore.rb
31
+ - lib/underscore/css.rb
32
+ - lib/underscore/html.rb
33
+ - lib/underscore/http.rb
34
+ - naive.rb
35
+ - tests/css.rb
36
+ - tests/html.rb
37
+ - tests/tests_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/geemus/underscore
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
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.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: does the right thing
66
+ test_files: []
67
+