dohweb 0.1.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Makani Mason, Kem Mason
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,107 @@
1
+ require 'doh/web/html_tags'
2
+
3
+ module DohWeb
4
+
5
+ class FormBuilder
6
+ include HtmlTags
7
+
8
+ def initialize(model, prefix, error_fields = [])
9
+ @model, @prefix, @error_fields = model, prefix, error_fields
10
+ end
11
+
12
+ def input(field, type, options = {})
13
+ errorified_html(field, input_tag(groupified_name(field), type, merge_value(field, options)))
14
+ end
15
+
16
+ def textbox(field, options = {})
17
+ errorified_html(field, input_tag(groupified_name(field), :text, merge_value(field, options)))
18
+ end
19
+
20
+ def password(field, options = {})
21
+ errorified_html(field, input_tag(groupified_name(field), :password, merge_value(field, options)))
22
+ end
23
+
24
+ def checkbox(field, options = {})
25
+ options[:checked] = 'checked' if model_value(field)
26
+ errorified_html(field, input_tag(groupified_name(field), :checkbox, options))
27
+ end
28
+
29
+ def radio(field, value, options = {})
30
+ options[:value] = value
31
+ options[:checked] = 'checked' if model_value(field) == value
32
+ errorified_html(field, input_tag(groupified_name(field), :radio, options))
33
+ end
34
+
35
+ def select(field, items, options = {})
36
+ errorified_html(field, select_tag(groupified_name(field), items, merge_value(field, options)))
37
+ end
38
+
39
+ def select_date(field, start_year, end_year, options = {})
40
+ name = groupified_name(field)
41
+ value = options[:value] || model_value(field)
42
+ controls = []
43
+ controls << select_month("#{name}__month", dtvalmap(value, :month))
44
+ controls << select_day("#{name}__day", dtvalmap(value, :day))
45
+ controls << select_year("#{name}__year", start_year, end_year, dtvalmap(value, :year))
46
+ errorified_html(field, controls.join("\n"))
47
+ end
48
+
49
+ def select_datetime(field, start_year, end_year, options = {})
50
+ name = groupified_name(field)
51
+ value = options[:value] || model_value(field)
52
+ controls = []
53
+ controls << select_month("#{name}__month", dtvalmap(value, :month))
54
+ controls << select_day("#{name}__day", dtvalmap(value, :day))
55
+ controls << select_year("#{name}__year", start_year, end_year, dtvalmap(value, :year))
56
+ controls << select_hour("#{name}__hour", dtvalmap(value, :hour))
57
+ controls << select_minute("#{name}__minute", dtvalmap(value, :minute))
58
+ errorified_html(field, controls.join("\n"))
59
+ end
60
+
61
+ def textarea(field, cols, rows, options = {})
62
+ options[:name] = groupified_name(field)
63
+ options[:cols] = cols
64
+ options[:rows] = rows
65
+ options[:value] ||= model_value(field)
66
+ errorified_html(field, full_tag(:textarea, '', options))
67
+ end
68
+
69
+ private
70
+ def dtvalmap(value, part)
71
+ if value
72
+ {:value => value.send(part) }
73
+ else
74
+ {}
75
+ end
76
+ end
77
+
78
+ def groupified_name(field)
79
+ "#{@prefix}_#{field}"
80
+ end
81
+
82
+ def errorified_html(field, html)
83
+ if @error_fields.include?(field.to_s)
84
+ full_tag('div', html, :class => 'hasError')
85
+ else
86
+ html
87
+ end
88
+ end
89
+
90
+ def merge_value(field, options)
91
+ options[:value] ||= model_value(field)
92
+ options
93
+ end
94
+
95
+ def model_value(field)
96
+ return nil unless @model
97
+ if @model.respond_to?(field)
98
+ @model.send(field)
99
+ elsif @model.respond_to?('[]')
100
+ @model[field] || @model[field.to_s]
101
+ else
102
+ raise "unable to access value for model #{@model}, field #{field}"
103
+ end
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,44 @@
1
+ require 'date'
2
+
3
+ module DohWeb
4
+
5
+ def flatten_multi_param(map)
6
+ if map.key?('hour') || map.key?('minute') || map.key?('second')
7
+ DateTime.new(map['year'].to_i, map['month'].to_i, map['day'].to_i, map['hour'].to_i, map['minute'].to_i, map['second'].to_i) rescue nil
8
+ elsif map.key?('year') || map.key?('month') || map.key?('day')
9
+ Date.new(map['year'].to_i, map['month'].to_i, map['day'].to_i) rescue nil
10
+ else
11
+ map.to_s
12
+ end
13
+ end
14
+
15
+ def group_params(group, _params = nil)
16
+ prefix = "#{group}_"
17
+ prefix_size = prefix.size
18
+ retval = {}
19
+
20
+ # first, go through all params to get the group ones, and combine the multi params ones into hashes
21
+ (_params || params).each_pair do |key, value|
22
+ next unless key.start_with?(prefix)
23
+ key = key.slice(prefix_size..-1)
24
+
25
+ unless key.index('__')
26
+ retval[key] = value
27
+ next
28
+ end
29
+
30
+ key, delim, subkey = key.partition('__')
31
+ raise "missing subkey for multi part param #{key}" if subkey.empty?
32
+ parts = (retval[key] ||= {})
33
+ parts[subkey] = value
34
+ end
35
+
36
+ # now, go back through and flatten all the multi part params into a single value object
37
+ retval.each_pair do |key, value|
38
+ retval[key] = flatten_multi_param(value) if value.is_a?(Hash)
39
+ end
40
+
41
+ retval
42
+ end
43
+
44
+ end
@@ -0,0 +1,67 @@
1
+ module DohWeb
2
+
3
+ module HtmlTags
4
+ SELECT_MONTH_ITEMS = [['January','1'],['February','2'],['March','3'],['April','4'],['May','5'],['June','6'],['July','7'],['August','8'],['September','9'],['October','10'],['November','11'],['December','12']].freeze
5
+
6
+ def start_tag(tag, open, options = {})
7
+ options[:id] ||= options[:name] if options[:name]
8
+ options_str = options.collect {|key, value| %(#{key}="#{value}") }.join(' ')
9
+ open_str = open ? '' : ' /'
10
+ "<#{tag} #{options_str}#{open_str}>"
11
+ end
12
+
13
+ def full_tag(tag, body, options = {})
14
+ "#{start_tag(tag, true, options)}#{body}</#{tag}>"
15
+ end
16
+
17
+ def input_tag(name, type, options = {})
18
+ options[:name] = name
19
+ options[:type] = type
20
+ start_tag(:input, false, options)
21
+ end
22
+
23
+ def select_tag(name, items, options = {})
24
+ selected_value = options.delete(:value).to_s
25
+ options[:name] = name
26
+ lines = [start_tag('select', true, options)]
27
+ items.each do |elem|
28
+ if elem.is_a?(Array)
29
+ text, value = elem
30
+ else
31
+ text = value = elem
32
+ end
33
+ subopts = {:value => value}
34
+ subopts[:selected] = 'selected' if value.to_s == selected_value
35
+ lines.push("#{start_tag(:option, true, subopts)}#{text}</option>")
36
+ end
37
+ lines.push('</select>')
38
+ lines.join("\n")
39
+ end
40
+
41
+ def select_day(name, options = {})
42
+ options[:value] ||= 1
43
+ select_tag(name, 1..31, options)
44
+ end
45
+
46
+ def select_month(name, options = {})
47
+ options[:value] ||= 1
48
+ select_tag(name, SELECT_MONTH_ITEMS, options)
49
+ end
50
+
51
+ def select_year(name, start_year, end_year, options = {})
52
+ options[:value] ||= start_year
53
+ select_tag(name, start_year..end_year, options)
54
+ end
55
+
56
+ def select_hour(name, options = {})
57
+ options[:value] ||= 0
58
+ select_tag(name, 0..23, options)
59
+ end
60
+
61
+ def select_minute(name, options = {})
62
+ options[:value] ||= 0
63
+ select_tag(name, 0..59, options)
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,24 @@
1
+ module DohWeb
2
+
3
+ # this is a simplified version of Rails ActiveSupport::Inflector.humanize
4
+ def humanize(lower_case_and_underscored_word)
5
+ retval = lower_case_and_underscored_word.to_s.dup
6
+ retval.gsub!(/_id$/, "")
7
+ retval.gsub!(/_/, ' ')
8
+ retval.gsub(/([a-z\d]*)/i) { |match| "#{match.downcase}" }.gsub(/^\w/) { $&.upcase }
9
+ end
10
+
11
+ # this is a simplified version of Rails ActionView::Helpers::TextHelper.cycle
12
+ # it only supports a single active cycle at a name, and no options
13
+ def cycle(*values)
14
+ if values != @cycle_values
15
+ @cycle_values = values
16
+ @cycle_index = 0
17
+ else
18
+ @cycle_index += 1
19
+ @cycle_index = 0 if @cycle_index >= @cycle_values.size
20
+ end
21
+ @cycle_values[@cycle_index]
22
+ end
23
+
24
+ end
@@ -0,0 +1,58 @@
1
+ require 'doh/web/group_params'
2
+
3
+ module DohWeb
4
+
5
+ class Test_group_params < DohTest::TestGroup
6
+ include DohWeb
7
+
8
+ def before_each
9
+ @exd = {}
10
+ @inp = {}
11
+ end
12
+
13
+ def verify
14
+ assert_equal(@exd, group_params(:some, @inp))
15
+ end
16
+
17
+ def test_stuff
18
+ @exd['blah'] = @inp['some_blah'] = :yes
19
+ verify
20
+
21
+ @exd['blee'] = @inp['some_blee'] = 'bloo'
22
+ verify
23
+
24
+ @inp['other'] = :diff
25
+ verify
26
+
27
+ @exd['thing'] = @inp['some_thing'] = 1
28
+ @inp['another'] = 1
29
+ verify
30
+
31
+ @exd['multi'] = {'hey' => :yes, 'hi' => :no}.to_s
32
+ @inp['some_multi__hey'] = :yes
33
+ @inp['some_multi__hi'] = :no
34
+ verify
35
+
36
+ @exd['long'] = {'howdy' => 'b', 'there' => 'e', 'george' => 'goo', 'bob' => 'i'}.to_s
37
+ @inp['some_long__howdy'] = 'b'
38
+ @inp['some_long__there'] = 'e'
39
+ @inp['some_long__george'] = 'goo'
40
+ @inp['some_long__bob'] = 'i'
41
+ verify
42
+
43
+ @exd['bday'] = Date.new(2012, 3, 21)
44
+ @inp['some_bday__year'] = '2012'
45
+ @inp['some_bday__month'] = '3'
46
+ @inp['some_bday__day'] = '21'
47
+ verify
48
+
49
+ @exd['when'] = DateTime.new(2012, 3, 21, 5, 0, 0)
50
+ @inp['some_when__year'] = '2012'
51
+ @inp['some_when__month'] = '3'
52
+ @inp['some_when__day'] = '21'
53
+ @inp['some_when__hour'] = '5'
54
+ verify
55
+ end
56
+ end
57
+
58
+ end
data/test/util.dt.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'doh/web/util'
2
+
3
+ module DohWeb
4
+
5
+ class Test_cycle < DohTest::TestGroup
6
+ include DohWeb
7
+
8
+ def first_set
9
+ cycle(1, 2)
10
+ end
11
+
12
+ def second_set
13
+ cycle(3, 4)
14
+ end
15
+
16
+ def third_set
17
+ cycle(5, 6, 7)
18
+ end
19
+
20
+ def test_stuff
21
+ assert_equal(1, first_set)
22
+ assert_equal(2, first_set)
23
+ assert_equal(1, first_set)
24
+ assert_equal(2, first_set)
25
+ assert_equal(1, first_set)
26
+
27
+ assert_equal(3, second_set)
28
+ assert_equal(4, second_set)
29
+ assert_equal(3, second_set)
30
+ assert_equal(4, second_set)
31
+ assert_equal(3, second_set)
32
+
33
+ assert_equal(5, third_set)
34
+ assert_equal(6, third_set)
35
+ assert_equal(7, third_set)
36
+ assert_equal(5, third_set)
37
+ assert_equal(6, third_set)
38
+ assert_equal(7, third_set)
39
+ assert_equal(5, third_set)
40
+ assert_equal(6, third_set)
41
+ assert_equal(7, third_set)
42
+ assert_equal(5, third_set)
43
+
44
+ assert_equal(1, first_set)
45
+ assert_equal(5, third_set)
46
+ assert_equal(3, second_set)
47
+ assert_equal(4, second_set)
48
+ assert_equal(5, third_set)
49
+ assert_equal(6, third_set)
50
+ assert_equal(1, first_set)
51
+ assert_equal(3, second_set)
52
+ assert_equal(5, third_set)
53
+ assert_equal(1, first_set)
54
+ assert_equal(2, first_set)
55
+ assert_equal(3, second_set)
56
+ assert_equal(5, third_set)
57
+ end
58
+ end
59
+
60
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dohweb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Makani Mason
9
+ - Kem Mason
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-27 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dohtest
17
+ requirement: &70306292257880 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.7
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70306292257880
26
+ description: utilities for building non-Rails web apps
27
+ email:
28
+ - devinfo@atpsoft.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - MIT-LICENSE
33
+ files:
34
+ - lib/doh/web/form_builder.rb
35
+ - lib/doh/web/group_params.rb
36
+ - lib/doh/web/html_tags.rb
37
+ - lib/doh/web/util.rb
38
+ - test/group_params.dt.rb
39
+ - test/util.dt.rb
40
+ - MIT-LICENSE
41
+ homepage: https://github.com/atpsoft/dohweb
42
+ licenses:
43
+ - MIT
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.9.2
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.15
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: utilities for building web apps
66
+ test_files:
67
+ - test/group_params.dt.rb
68
+ - test/util.dt.rb
69
+ has_rdoc: