massive-nancy 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.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 0
4
+ :major: 0
data/lib/nancy.rb ADDED
@@ -0,0 +1,99 @@
1
+ require 'sinatra/base'
2
+ require 'core_ext/string'
3
+
4
+ module Sinatra
5
+ module Nancy
6
+ attr_accessor :resource, :resource_name, :resource_class
7
+
8
+ # add your helpers here
9
+ def form_for(resource, action, options = {}, &block)
10
+ @resource = resource
11
+ @resource_name = @resource.class.to_s.snake_case
12
+ @resource_class = Object.const_get(@resource.class.to_s)
13
+
14
+ options = {:method => :post, :name => @resource_name, :action => action}.merge(options)
15
+ @_out_buf ||= ''
16
+ @_out_buf << open_tag(:form, options)
17
+ yield if block_given?
18
+ @_out_buf << close_tag(:form)
19
+ end
20
+
21
+ def open_tag(name, options = {})
22
+ attrs = hash_to_attributes options
23
+ "<#{name.to_s}#{attrs}>"
24
+ end
25
+
26
+ def close_tag(name)
27
+ "</#{name.to_s}>"
28
+ end
29
+
30
+ def content_tag(name, content, options = {})
31
+ open_tag(name.to_s) + content + close_tag(name.to_s)
32
+ end
33
+
34
+ def empty_tag(name, options = {})
35
+ attrs = hash_to_attributes options
36
+ "<#{name.to_s}#{attrs}/>"
37
+ end
38
+
39
+ def hash_to_attributes(hash)
40
+ out = [' ']
41
+ hash.each_pair do |key, value|
42
+ out << %(#{key}="#{value}")
43
+ end
44
+ out << ' '
45
+ out.join(" ")
46
+ end
47
+
48
+ def input(name, type, options = {})
49
+ options[:name] = name
50
+ options[:type] = type
51
+ empty_tag(:input, options)
52
+ end
53
+
54
+ def field_for(field, options = {})
55
+ label = label_for(field, options)
56
+ field_type = @resource_class.properties[field].type.to_s.downcase
57
+ content = self.send("field_for_#{field_type}", field, options)
58
+ label + content
59
+ end
60
+
61
+ def label_for(field, options = {})
62
+ opts = {:for => field}.merge(options)
63
+ label = field.to_s || options[:label].humanize
64
+ content_tag(:label, label, opts)
65
+ end
66
+
67
+ def name_for(field)
68
+ [@resource_name, '[', field.to_s, ']'].join
69
+ end
70
+
71
+ def field_for_string(field, options = {})
72
+ input(name_for(field.to_s), :text, options)
73
+ end
74
+
75
+ alias_method :field_for_integer, :field_for_string
76
+
77
+ def field_for_trueclass(field, options = {})
78
+ select_tag(field, {"Yes" => true, "No" => false}, options)
79
+ end
80
+
81
+ def select_tag(name, choices, options = {})
82
+ start = open_tag(:select, options)
83
+ content = []
84
+ choices.each_pair do |key, value|
85
+ content << content_tag(:option, key, value)
86
+ end
87
+ content = content.join("\n")
88
+ stop = close_tag(:select)
89
+ start + content + stop
90
+ end
91
+
92
+ def submit_tag(name)
93
+ options = {:type => :submit, :name => :submit, :value => name}
94
+ empty_tag(:input, options)
95
+ end
96
+ end
97
+
98
+ helpers Sinatra::Nancy
99
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class NancyTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'nancy'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: massive-nancy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - massive
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-20 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: massivea@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/nancy.rb
27
+ - test/nancy_test.rb
28
+ - test/test_helper.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/massive/nancy
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --inline-source
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Fantastic Forms for Sinatra
56
+ test_files: []
57
+