trenni-formatters 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 541558e37b2508160c0f53b739a55fca59dc2bbb
4
- data.tar.gz: 57e9b3758e6ea0bfb835cc39358d441a6d3bc08c
3
+ metadata.gz: 3b94680567298f92a104b915eb9394dbcbba604a
4
+ data.tar.gz: e63108af3f7dd07993a4b299bad3bc596e5138a8
5
5
  SHA512:
6
- metadata.gz: 7f0c5bf2ecddaa4031ab0cc05fe75a91fea59671f12787d4f2ff0b37be6b5671f80346eac8e55d9502e24a135df7459195ca2818789673e7ef25e43fd19c800f
7
- data.tar.gz: a6d55bbf536c9b503b5f89b10d8e35c343ea15e3fdf74d9accf07a0e7244718881031d5771cb75be1b59d0fb854b6710560d77f80783eefa798317134fd73c51
6
+ metadata.gz: 70478b6fe86057a00dd1a497c78ebd037dc09966dd2ce29f7a61ac8e05c14cc7cb4ece5d2442378889b7f9ce4a33012a6b6bb6e8ab4aa3f719d62d5ac723b0fe
7
+ data.tar.gz: 10030cb89fa77e24efa96bec0aa21517076dd49bdfad29560cbec0c7fe3779d15116d90c7aa7ade3e3d8afad85753a13b4d36cada63b7216001445fa2dfc47af
data/.travis.yml CHANGED
@@ -1,9 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.8.7"
4
- - "1.9.2"
5
- - "1.9.3"
6
- - jruby-18mode # JRuby in 1.8 mode
7
- - jruby-19mode # JRuby in 1.9 mode
8
- - rbx-18mode
9
- - rbx-19mode
3
+ - "1.9"
4
+ - "2.0"
5
+ - "2.1"
data/Gemfile CHANGED
@@ -2,7 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in trenni-formatters.gemspec
4
4
  gemspec
5
-
6
- group :test do
7
- gem "rake"
8
- end
data/README.md CHANGED
@@ -7,7 +7,7 @@ adapter that turns model data into presentation text.
7
7
  Formatters are designed to be customised, typically per-project, for specific
8
8
  formatting needs.
9
9
 
10
- [![Build Status](https://secure.travis-ci.org/ioquatix/trenni-formatters.png)](http://travis-ci.org/ioquatix/trenni-formatters)
10
+ [![Build Status](https://travis-ci.org/ioquatix/trenni-formatters.svg?branch=master)](https://travis-ci.org/ioquatix/trenni-formatters)
11
11
 
12
12
  ## Installation
13
13
 
@@ -50,7 +50,7 @@ For more examples please see `test/`.
50
50
 
51
51
  Released under the MIT license.
52
52
 
53
- Copyright, 2012, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
53
+ Copyright, 2014, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
54
54
 
55
55
  Permission is hereby granted, free of charge, to any person obtaining a copy
56
56
  of this software and associated documentation files (the "Software"), to deal
data/Rakefile CHANGED
@@ -1,9 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
- require "rake/testtask"
2
+ require "rspec/core/rake_task"
3
3
 
4
- Rake::TestTask.new do |t|
5
- t.libs << 'test'
6
- end
4
+ RSpec::Core::RakeTask.new(:spec)
7
5
 
8
- desc "Run tests"
9
- task :default => :test
6
+ task :default => :spec
@@ -20,63 +20,14 @@
20
20
 
21
21
  require 'trenni/builder'
22
22
 
23
+ require_relative 'form_formatter'
24
+
23
25
  module Trenni
24
26
  module Formatters
25
27
  module HTML
26
28
  module DefinitionListForm
27
- # The target object of the form.
28
- def object
29
- @options[:object]
30
- end
31
-
32
- # The name of the field.
33
- def name_for(options)
34
- options[:field] || title_for(options).downcase.gsub(/\s+/, '_').to_sym
35
- end
36
-
37
- # The human presentable title for the field.
38
- def title_for(options)
39
- title = options[:title] || Strings::to_title(options[:field].to_s)
40
-
41
- Strings::to_html(title)
42
- end
43
-
44
- # The value of the field.
45
- def value_for(options)
46
- value = options[:value]
47
-
48
- if options[:object]
49
- value ||= options[:object].send(name_for(options))
50
- end
51
-
52
- self.format(value, options)
53
- end
54
-
55
- def pattern_for(options)
56
- options[:pattern]
57
- end
58
-
59
- def placeholder_for(options)
60
- options[:placeholder]
61
- end
62
-
63
- def input_attributes_for(options)
64
- attributes = {
65
- :type => options[:type],
66
- :name => name_for(options),
67
- :value => value_for(options),
68
- :required => options[:required] ? true : false,
69
- :pattern => pattern_for(options),
70
- :placeholder => placeholder_for(options)
71
- }
72
-
73
- if explicit_attributes = options[:attributes]
74
- attributes.update(explicit_attributes)
75
- end
76
-
77
- return attributes
78
- end
79
-
29
+ include FormFormatter
30
+
80
31
  # An input field (single line text).
81
32
  def input(options = {})
82
33
  options = @options.merge(options)
@@ -90,14 +41,6 @@ module Trenni
90
41
  end
91
42
  end
92
43
 
93
- def textarea_attributes_for(options)
94
- return {
95
- :name => name_for(options),
96
- :required => options[:required] ? true : false,
97
- :placeholder => placeholder_for(options),
98
- }
99
- end
100
-
101
44
  # A textarea field (multi-line text).
102
45
  def textarea(options = {})
103
46
  options = @options.merge(options)
@@ -113,16 +56,6 @@ module Trenni
113
56
  end
114
57
  end
115
58
 
116
- def checkbox_attributes_for(options)
117
- return {
118
- :type => options[:type] || 'checkbox',
119
- :checked => options[:object].send(name),
120
- :name => name_for(options),
121
- :required => options[:required] ? true : false,
122
- :value => 'true',
123
- }
124
- end
125
-
126
59
  # A checkbox field.
127
60
  def checkbox(options)
128
61
  options = @options.merge(options)
@@ -136,14 +69,6 @@ module Trenni
136
69
  end
137
70
  end
138
71
 
139
- def submit_attributes_for(options)
140
- return {
141
- :type => options[:type] || 'submit',
142
- :name => name_for(options),
143
- :value => title_for(options),
144
- }
145
- end
146
-
147
72
  # A submission button
148
73
  def submit(options = {})
149
74
  options = @options.merge(options)
@@ -157,23 +82,6 @@ module Trenni
157
82
  end
158
83
  end
159
84
 
160
- def hidden_attributes_for(options)
161
- return {
162
- :type => options[:type] || 'hidden',
163
- :name => name_for(options),
164
- :value => value_for(options),
165
- }
166
- end
167
-
168
- # A hidden field.
169
- def hidden(options = {})
170
- options = @options.merge(options)
171
-
172
- Builder.fragment do |builder|
173
- builder.tag :input, hidden_attributes_for(options)
174
- end
175
- end
176
-
177
85
  def element(klass, options = {}, &block)
178
86
  options = @options.merge(options)
179
87
  buffer = Trenni::Template.buffer(block.binding)
@@ -0,0 +1,128 @@
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'trenni/builder'
22
+
23
+ module Trenni
24
+ module Formatters
25
+ module HTML
26
+ module FormFormatter
27
+ # The target object of the form.
28
+ def object
29
+ @options[:object]
30
+ end
31
+
32
+ # The name of the field.
33
+ def name_for(options)
34
+ options[:field] || title_for(options).downcase.gsub(/\s+/, '_').to_sym
35
+ end
36
+
37
+ # The human presentable title for the field.
38
+ def title_for(options)
39
+ title = options[:title] || Strings::to_title(options[:field].to_s)
40
+
41
+ Strings::to_html(title)
42
+ end
43
+
44
+ # The value of the field.
45
+ def value_for(options)
46
+ value = options[:value]
47
+
48
+ if options[:object]
49
+ value ||= options[:object].send(name_for(options))
50
+ end
51
+
52
+ # Allow to specify a default value if the value given, usually from an object, is nil.
53
+ value ||= options[:default]
54
+
55
+ self.format(value, options)
56
+ end
57
+
58
+ def pattern_for(options)
59
+ options[:pattern]
60
+ end
61
+
62
+ def placeholder_for(options)
63
+ options[:placeholder]
64
+ end
65
+
66
+ def input_attributes_for(options)
67
+ attributes = {
68
+ :type => options[:type],
69
+ :name => name_for(options),
70
+ :value => value_for(options),
71
+ :required => options[:required] ? true : false,
72
+ :pattern => pattern_for(options),
73
+ :placeholder => placeholder_for(options)
74
+ }
75
+
76
+ if explicit_attributes = options[:attributes]
77
+ attributes.update(explicit_attributes)
78
+ end
79
+
80
+ return attributes
81
+ end
82
+
83
+ def textarea_attributes_for(options)
84
+ return {
85
+ :name => name_for(options),
86
+ :required => options[:required] ? true : false,
87
+ :placeholder => placeholder_for(options),
88
+ }
89
+ end
90
+
91
+ def checkbox_attributes_for(options)
92
+ return {
93
+ :type => options[:type] || 'checkbox',
94
+ :checked => options[:object].send(name),
95
+ :name => name_for(options),
96
+ :required => options[:required] ? true : false,
97
+ :value => 'true',
98
+ }
99
+ end
100
+
101
+ def submit_attributes_for(options)
102
+ return {
103
+ :type => options[:type] || 'submit',
104
+ :name => name_for(options),
105
+ :value => title_for(options),
106
+ }
107
+ end
108
+
109
+ def hidden_attributes_for(options)
110
+ return {
111
+ :type => options[:type] || 'hidden',
112
+ :name => name_for(options),
113
+ :value => value_for(options),
114
+ }
115
+ end
116
+
117
+ # A hidden field.
118
+ def hidden(options = {})
119
+ options = @options.merge(options)
120
+
121
+ Builder.fragment do |builder|
122
+ builder.tag :input, hidden_attributes_for(options)
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Trenni
22
22
  module Formatters
23
- VERSION = "0.3.1"
23
+ VERSION = "0.3.2"
24
24
  end
25
25
  end
@@ -20,27 +20,32 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'pathname'
24
- require 'test/unit'
25
- require 'stringio'
26
-
27
- require 'trenni'
28
23
  require 'trenni/formatters'
29
24
 
30
- class TestFormatters < Test::Unit::TestCase
31
- def test_basic_formatter
32
- count = 0
33
- formatter = Trenni::Formatters::Formatter.new
34
-
35
- formatter.for(String) do |value, options|
36
- count += 1
37
- "String: #{value}"
25
+ module Trenni
26
+ module Formatters
27
+ module FormattersSpec
28
+ describe Formatters do
29
+ before do
30
+ @count = 0
31
+ @formatter = Trenni::Formatters::Formatter.new
32
+
33
+ @formatter.for(String) do |value, options|
34
+ @count += 1
35
+ "String: #{value}"
36
+ end
37
+ end
38
+
39
+ it "should format string" do
40
+ expect(@formatter.format("foobar")).to be == "String: foobar"
41
+ expect(@count).to be == 1
42
+ end
43
+
44
+ it "should format numbers" do
45
+ expect(@formatter.format(10)).to be == "10"
46
+ expect(@count).to be == 0
47
+ end
48
+ end
38
49
  end
39
-
40
- assert_equal "String: foobar", formatter.format("foobar")
41
- assert_equal 1, count
42
-
43
- assert_equal "10", formatter.format(10)
44
- assert_equal 1, count
45
50
  end
46
51
  end
@@ -20,30 +20,34 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require 'pathname'
24
- require 'test/unit'
25
- require 'stringio'
26
-
27
23
  require 'trenni/formatters'
28
24
  require 'trenni/formatters/html/definition_list_form'
29
25
  require 'trenni/formatters/html/option_select'
30
26
  require 'trenni/formatters/html/table_select'
31
27
 
32
- class TestFormsFormatter < Test::Unit::TestCase
33
- class BasicFormatter < Trenni::Formatters::Formatter
34
- include Trenni::Formatters::HTML::DefinitionListForm
35
- end
36
-
37
- class Foo
38
- attr :bar, true
39
- end
40
-
41
- def test_basic_formatter
42
- object = Foo.new
43
- object.bar = 10
44
-
45
- formatter = BasicFormatter.new(:object => object)
46
-
47
- assert_equal %Q{<dt>Bar</dt>\n<dd><input name="bar" value="10"/></dd>}, formatter.input(:field => :bar)
28
+ module Trenni
29
+ module Formatters
30
+ module FormFormattersSpec
31
+ class FormFormatter < Trenni::Formatters::Formatter
32
+ include Trenni::Formatters::HTML::DefinitionListForm
33
+ end
34
+
35
+ describe Formatters do
36
+ it "should generate form" do
37
+ object = double(:bar => 10)
38
+
39
+ formatter = FormFormatter.new(:object => object)
40
+
41
+ expect(formatter.input(:field => :bar)).to be == %Q{<dt>Bar</dt>\n<dd><input name="bar" value="10"/></dd>}
42
+ end
43
+
44
+ it "should have default value" do
45
+ formatter = FormFormatter.new
46
+
47
+ expect(formatter.value_for(:value => 10)).to be == "10"
48
+ expect(formatter.value_for(:value => nil, :default => 10)).to be == "10"
49
+ end
50
+ end
51
+ end
48
52
  end
49
53
  end
@@ -3,12 +3,12 @@ lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'trenni/formatters/version'
5
5
 
6
- Gem::Specification.new do |gem|
7
- gem.name = "trenni-formatters"
8
- gem.version = Trenni::Formatters::VERSION
9
- gem.authors = ["Samuel Williams"]
10
- gem.email = ["samuel.williams@oriontransfer.co.nz"]
11
- gem.description = <<-EOF
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trenni-formatters"
8
+ spec.version = Trenni::Formatters::VERSION
9
+ spec.authors = ["Samuel Williams"]
10
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
11
+ spec.description = <<-EOF
12
12
  Trenni is a templating system, and these formatters assist with the development
13
13
  of typical view and form based web interface. A formatter is a high-level
14
14
  adapter that turns model data into presentation text.
@@ -16,13 +16,17 @@ Gem::Specification.new do |gem|
16
16
  Formatters are designed to be customised, typically per-project, for specific
17
17
  formatting needs.
18
18
  EOF
19
- gem.summary = %q{Formatters for Trenni, to assist with typical views and form based interfaces.}
20
- gem.homepage = "https://github.com/ioquatix/trenni-formatters"
19
+ spec.summary = %q{Formatters for Trenni, to assist with typical views and form based interfaces.}
20
+ spec.homepage = "https://github.com/ioquatix/trenni-formatters"
21
21
 
22
- gem.files = `git ls-files`.split($/)
23
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
24
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
25
- gem.require_paths = ["lib"]
22
+ spec.files = `git ls-files`.split($/)
23
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+ spec.require_paths = ["lib"]
26
26
 
27
- gem.add_dependency "trenni", "~> 1.2.0"
27
+ spec.add_dependency "trenni", "~> 1.2.0"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.3"
30
+ spec.add_development_dependency "rspec", "~> 3.0.0.rc1"
31
+ spec.add_development_dependency "rake"
28
32
  end
metadata CHANGED
@@ -1,29 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trenni-formatters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-12 00:00:00.000000000 Z
11
+ date: 2014-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trenni
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0.rc1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0.rc1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
27
69
  description: "\tTrenni is a templating system, and these formatters assist with the
28
70
  development\n\tof typical view and form based web interface. A formatter is a high-level\n\tadapter
29
71
  that turns model data into presentation text.\n\n\tFormatters are designed to be
@@ -34,19 +76,20 @@ executables: []
34
76
  extensions: []
35
77
  extra_rdoc_files: []
36
78
  files:
37
- - .gitignore
38
- - .travis.yml
79
+ - ".gitignore"
80
+ - ".travis.yml"
39
81
  - Gemfile
40
82
  - README.md
41
83
  - Rakefile
42
84
  - lib/trenni/formatters.rb
43
85
  - lib/trenni/formatters/formatter.rb
44
86
  - lib/trenni/formatters/html/definition_list_form.rb
87
+ - lib/trenni/formatters/html/form_formatter.rb
45
88
  - lib/trenni/formatters/html/option_select.rb
46
89
  - lib/trenni/formatters/html/table_select.rb
47
90
  - lib/trenni/formatters/version.rb
48
- - test/test_formatters.rb
49
- - test/test_forms_formatter.rb
91
+ - spec/trenni/formatters/formatters_spec.rb
92
+ - spec/trenni/formatters/test_forms_formatter.rb
50
93
  - trenni-formatters.gemspec
51
94
  homepage: https://github.com/ioquatix/trenni-formatters
52
95
  licenses: []
@@ -57,21 +100,20 @@ require_paths:
57
100
  - lib
58
101
  required_ruby_version: !ruby/object:Gem::Requirement
59
102
  requirements:
60
- - - '>='
103
+ - - ">="
61
104
  - !ruby/object:Gem::Version
62
105
  version: '0'
63
106
  required_rubygems_version: !ruby/object:Gem::Requirement
64
107
  requirements:
65
- - - '>='
108
+ - - ">="
66
109
  - !ruby/object:Gem::Version
67
110
  version: '0'
68
111
  requirements: []
69
112
  rubyforge_project:
70
- rubygems_version: 2.0.3
113
+ rubygems_version: 2.2.2
71
114
  signing_key:
72
115
  specification_version: 4
73
116
  summary: Formatters for Trenni, to assist with typical views and form based interfaces.
74
117
  test_files:
75
- - test/test_formatters.rb
76
- - test/test_forms_formatter.rb
77
- has_rdoc:
118
+ - spec/trenni/formatters/formatters_spec.rb
119
+ - spec/trenni/formatters/test_forms_formatter.rb