view 1.0.0.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +158 -0
- data/init.rb +1 -0
- data/lib/view/formatter.rb +134 -0
- data/lib/view/formatters/auto.rb +49 -0
- data/lib/view/formatters/boolean.rb +19 -0
- data/lib/view/formatters/currency.rb +13 -0
- data/lib/view/formatters/date.rb +11 -0
- data/lib/view/formatters/datetime.rb +11 -0
- data/lib/view/formatters/delimited.rb +13 -0
- data/lib/view/formatters/file_link.rb +33 -0
- data/lib/view/formatters/html_safe.rb +11 -0
- data/lib/view/formatters/human.rb +16 -0
- data/lib/view/formatters/image.rb +37 -0
- data/lib/view/formatters/link.rb +25 -0
- data/lib/view/formatters/nil.rb +11 -0
- data/lib/view/formatters/percentage.rb +13 -0
- data/lib/view/formatters/phone.rb +13 -0
- data/lib/view/formatters/precision.rb +13 -0
- data/lib/view/formatters/self.rb +11 -0
- data/lib/view/formatters/sentence.rb +33 -0
- data/lib/view/formatters/size.rb +13 -0
- data/lib/view/helper.rb +11 -0
- data/lib/view/railtie.rb +13 -0
- data/lib/view/version.rb +3 -0
- data/lib/view.rb +29 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/view/formatters/auto_spec.rb +25 -0
- data/spec/view/formatters/boolean_spec.rb +28 -0
- data/spec/view/formatters/date_spec.rb +22 -0
- data/spec/view/formatters/datetime_spec.rb +17 -0
- data/spec/view/formatters/nil_spec.rb +15 -0
- data/spec/view/formatters/self_spec.rb +10 -0
- data/spec/view/formatters/sentence_spec.rb +10 -0
- data/spec/view/helper_spec.rb +11 -0
- data/spec/view/view_spec.rb +13 -0
- metadata +112 -0
data/README.rdoc
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
= View
|
2
|
+
|
3
|
+
Automatic viewer. Very configurable. Very flexible. Very alpha (for now).
|
4
|
+
|
5
|
+
It is made for displaying stuff in views. This can be very handy for DSLs that create views.
|
6
|
+
Some of those DSLs are coming soon to a rubygem near you!
|
7
|
+
|
8
|
+
But first, the view!
|
9
|
+
|
10
|
+
== Usage
|
11
|
+
|
12
|
+
Use it from your views:
|
13
|
+
|
14
|
+
= view @post
|
15
|
+
|
16
|
+
View will try to figure out what the most logical way is to present the object you passed in.
|
17
|
+
If the @post object has a title for instance, it will use that.
|
18
|
+
|
19
|
+
See the auto formatter for more information.
|
20
|
+
|
21
|
+
You can use view anywhere you like:
|
22
|
+
|
23
|
+
puts View.to_s(@post)
|
24
|
+
|
25
|
+
But beware that for many formatters, a reference to the template is required.
|
26
|
+
It is a helper for the view part of your application after all.
|
27
|
+
|
28
|
+
== Options
|
29
|
+
|
30
|
+
View will automatically try to choose the proper formatter. You can specify a formatter yourself:
|
31
|
+
|
32
|
+
= view @post.created_at, :as => :date
|
33
|
+
|
34
|
+
You can add a block to specify your own behavior. The parsed value is passed in as a block-variable.
|
35
|
+
|
36
|
+
= view @post.author do |author|
|
37
|
+
= link_to author, @post.author
|
38
|
+
|
39
|
+
Most formatters pass their options down to the method they use, such as the image formatter:
|
40
|
+
|
41
|
+
= view @user.avatar, :as => :image, :size => "40x40", :class => "avatar"
|
42
|
+
|
43
|
+
== Examples
|
44
|
+
|
45
|
+
There are many formatters included (18 and counting), but here are some interesting use cases of some of them.
|
46
|
+
|
47
|
+
# Options are passed to the helper methods:
|
48
|
+
= view @post, :as => :link, :method => :delete, :confirm => "are you sure?"
|
49
|
+
|
50
|
+
# Renders a link to the edit_page of the post:
|
51
|
+
= view @post, :as => :link, :path => [ :edit ]
|
52
|
+
|
53
|
+
# Renders a sentence of links:
|
54
|
+
= view Post.all, :each => { :as => :link }
|
55
|
+
|
56
|
+
# When using paperclip, renders the image:
|
57
|
+
= view @project.logo, :as => :image
|
58
|
+
|
59
|
+
# Renders "yes" or "no" (with I18n support!)
|
60
|
+
= view @user.admin?
|
61
|
+
|
62
|
+
See the formatters in lib/views/formatters to see their full documentation.
|
63
|
+
|
64
|
+
== Adding formatters
|
65
|
+
|
66
|
+
You can add a formatter by inheriting from View::Formatter.
|
67
|
+
The only method you need to implement is the to_s method.
|
68
|
+
|
69
|
+
In the class you have access to the following methods:
|
70
|
+
|
71
|
+
* value: the object passed in
|
72
|
+
* options: a filtered hash of options
|
73
|
+
* all_options: the unfiltered hash
|
74
|
+
* template: call methods like link_to on this
|
75
|
+
|
76
|
+
If you wanted a uppercase formatter for example, you could do this:
|
77
|
+
|
78
|
+
class Uppercase < View::Formatter
|
79
|
+
|
80
|
+
def to_s
|
81
|
+
value.to_s.upcase
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
The name of the formatter is automatically infered from the name of the class.
|
87
|
+
|
88
|
+
You can use the .as method to specify a different name.
|
89
|
+
|
90
|
+
class Foo < View::Formatter
|
91
|
+
as :bar
|
92
|
+
# etc ...
|
93
|
+
end
|
94
|
+
|
95
|
+
You can control which options are allowed, by adding reserved options:
|
96
|
+
|
97
|
+
class Foo < View::Formatter
|
98
|
+
self.reserved_options = [ :foo, :bar ]
|
99
|
+
end
|
100
|
+
|
101
|
+
Now, the options method will return the options passed by the user, minus foo and bar.
|
102
|
+
To use them, in your code, use the all_options method.
|
103
|
+
This is done to easily pass the options to another method, without cluttering:
|
104
|
+
|
105
|
+
class Paragraph < View::Formatter
|
106
|
+
def to_s
|
107
|
+
template.content_tag(:p, value.to_s, options)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
To more tightly control which options are allowed, specify the allowed_options.
|
112
|
+
|
113
|
+
class Size < View::Formatter
|
114
|
+
self.allowed_options = [ :separator, :delimiter, :precision ]
|
115
|
+
def to_s
|
116
|
+
template.number_to_human_size(value, options)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
You can use the existing formatters as examples.
|
121
|
+
|
122
|
+
== Configuration:
|
123
|
+
|
124
|
+
See lib/view.rb for information on configuration.
|
125
|
+
|
126
|
+
== Installation
|
127
|
+
|
128
|
+
=== Rails 3
|
129
|
+
|
130
|
+
In Rails 3, just add it to your Gemfile:
|
131
|
+
|
132
|
+
gem 'view'
|
133
|
+
|
134
|
+
Run <tt>bundle install</tt> and you're ready to go!
|
135
|
+
|
136
|
+
=== Rails 2
|
137
|
+
|
138
|
+
In Rails 2, add it to app/environment.rb:
|
139
|
+
|
140
|
+
config.gem 'view'
|
141
|
+
|
142
|
+
And run <tt>rake gems:install</tt> to get it.
|
143
|
+
|
144
|
+
Afterwards, include the helper mehtod manually, by editing app/helpers/application_helper.rb:
|
145
|
+
|
146
|
+
module ApplicationHelper
|
147
|
+
include View::Helper
|
148
|
+
end
|
149
|
+
|
150
|
+
Caution: some helpers may use new helper methods from Rails 3 and might not work in Rails 2.
|
151
|
+
|
152
|
+
== Contributing
|
153
|
+
|
154
|
+
Yes please! You know the drill: fork, commit, pull request, profit!
|
155
|
+
|
156
|
+
== License
|
157
|
+
|
158
|
+
Copyright 2010 Iain Hecker (iain@iain.nl), released under the MIT License.
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'view'
|
@@ -0,0 +1,134 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
class Formatter
|
4
|
+
|
5
|
+
attr_reader :value, :template, :block
|
6
|
+
|
7
|
+
class_inheritable_accessor :default_options
|
8
|
+
self.default_options = {}
|
9
|
+
|
10
|
+
class_inheritable_array :reserved_options
|
11
|
+
self.reserved_options = [ :as, :block_arguments ]
|
12
|
+
|
13
|
+
class_inheritable_array :allowed_options
|
14
|
+
self.allowed_options = []
|
15
|
+
|
16
|
+
def initialize(value, options = {}, template = nil, &block)
|
17
|
+
@value = value
|
18
|
+
@options = options
|
19
|
+
@template = template
|
20
|
+
@block = block
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.inherited(formatter)
|
24
|
+
super
|
25
|
+
formatters.unshift(formatter)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.formatters
|
29
|
+
View.formatters ||= []
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.as(type)
|
33
|
+
@type = type
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.type
|
37
|
+
@type || name.split('::').last.underscore
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.to_s(*args, &block)
|
41
|
+
new(*args, &block).format
|
42
|
+
end
|
43
|
+
|
44
|
+
def format
|
45
|
+
if block
|
46
|
+
captured_value
|
47
|
+
else
|
48
|
+
formatted_value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_s
|
53
|
+
msg <<-MSG.squeeze(' ')
|
54
|
+
The only thing a formatter needs to do is implement the #to_s method.
|
55
|
+
If you see this error, you forgot to do that for the #{self.class.type} formatter.
|
56
|
+
MSG
|
57
|
+
raise NotImplementedError.new(msg)
|
58
|
+
end
|
59
|
+
|
60
|
+
def options
|
61
|
+
default_options.merge(all_options).delete_if do |key, value|
|
62
|
+
option_not_allowed?(key)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def template_can_capture?
|
69
|
+
template && template.respond_to?(:capture)
|
70
|
+
end
|
71
|
+
|
72
|
+
def captured_value
|
73
|
+
if template_can_capture?
|
74
|
+
captured_value_by_template
|
75
|
+
else
|
76
|
+
captured_return_value
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def captured_value_by_template
|
81
|
+
template.capture(formatted_value, *block_arguments, &block)
|
82
|
+
end
|
83
|
+
|
84
|
+
def captured_return_value
|
85
|
+
block.call(formatted_value, *block_arguments)
|
86
|
+
end
|
87
|
+
|
88
|
+
def formatted_value
|
89
|
+
formatter.new(value, all_options, template, &block).to_s
|
90
|
+
end
|
91
|
+
|
92
|
+
def formatter
|
93
|
+
find_formatter || formatter_not_found
|
94
|
+
end
|
95
|
+
|
96
|
+
def formatter_not_found
|
97
|
+
raise "Couldn't find the #{as} formatter. Got: #{formatter_names.join(', ')}"
|
98
|
+
end
|
99
|
+
|
100
|
+
def formatter_names
|
101
|
+
self.class.formatters.map { |formatter| formatter.type.to_s }
|
102
|
+
end
|
103
|
+
|
104
|
+
def find_formatter
|
105
|
+
self.class.formatters.find { |formatter| formatter.type.to_s == as.to_s }
|
106
|
+
end
|
107
|
+
|
108
|
+
def option_not_allowed?(key)
|
109
|
+
if allowed_options.empty?
|
110
|
+
reserved_options.map(&:to_s).include?(key.to_s)
|
111
|
+
else
|
112
|
+
!allowed_options.map(&:to_s).include?(key.to_s)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def block_arguments
|
117
|
+
all_options[:block_arguments] || []
|
118
|
+
end
|
119
|
+
|
120
|
+
def as
|
121
|
+
all_options[:as] || View.default_formatter
|
122
|
+
end
|
123
|
+
|
124
|
+
def all_options
|
125
|
+
@options
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
Dir[File.expand_path('../formatters/*.rb', __FILE__)].each do |formatter_file|
|
133
|
+
require formatter_file
|
134
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
class Auto < Formatter
|
4
|
+
|
5
|
+
def to_s
|
6
|
+
if as
|
7
|
+
format
|
8
|
+
else
|
9
|
+
guess
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def datetime_format?
|
14
|
+
value.respond_to?(:strftime)
|
15
|
+
end
|
16
|
+
|
17
|
+
def file_link_format?
|
18
|
+
View.file_methods.any? { |method| value.respond_to?(method) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def boolean_format?
|
22
|
+
value == true || value == false
|
23
|
+
end
|
24
|
+
|
25
|
+
def nil_format?
|
26
|
+
value.nil?
|
27
|
+
end
|
28
|
+
|
29
|
+
def sentence_format?
|
30
|
+
value.respond_to?(:to_sentence)
|
31
|
+
end
|
32
|
+
|
33
|
+
def link_format?
|
34
|
+
all_options[:to]
|
35
|
+
end
|
36
|
+
|
37
|
+
def as
|
38
|
+
%w|nil boolean file_link datetime sentence link|.find { |type| send("#{type}_format?") }
|
39
|
+
end
|
40
|
+
|
41
|
+
def guess
|
42
|
+
View.guessing_methods.each do |method|
|
43
|
+
return value.send(method) if value.respond_to?(method)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
class Boolean < Formatter
|
4
|
+
|
5
|
+
def to_s
|
6
|
+
::I18n.t(boolean_value.to_s, :scope => [:view, :booleans], :default => default)
|
7
|
+
end
|
8
|
+
|
9
|
+
def default
|
10
|
+
boolean_value ? "Yes" : "No"
|
11
|
+
end
|
12
|
+
|
13
|
+
def boolean_value
|
14
|
+
!!value
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
class FileLink < Formatter
|
4
|
+
|
5
|
+
self.reserved_options = [ :text, :text_method ]
|
6
|
+
self.default_options = { :target => "_blank" }
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
template.link_to(text, path, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def path
|
13
|
+
View.path_methods.each do |method|
|
14
|
+
return value.send(method) if value.respond_to?(method)
|
15
|
+
end
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def text
|
20
|
+
all_options[:text] || text_via_method || value.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def text_via_method
|
24
|
+
send(text_method) if text_method
|
25
|
+
end
|
26
|
+
|
27
|
+
def text_method
|
28
|
+
all_options[:text_method]
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
# The number_to_human helper was introduced in Rails 3.
|
4
|
+
# It will give results like "10 thousand"
|
5
|
+
class Human < Formatter
|
6
|
+
|
7
|
+
self.allowed_options = [ :locale, :precision, :significant, :separator, :delimiter,
|
8
|
+
:strip_insignificant_zeros, :units, :format ]
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
template.number_to_human(value, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
class Image < Formatter
|
4
|
+
|
5
|
+
self.reserved_options = [ :with ]
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
template.image_tag(path, options) if file?
|
9
|
+
end
|
10
|
+
|
11
|
+
def path
|
12
|
+
value.send(path_method, *path_arguments)
|
13
|
+
end
|
14
|
+
|
15
|
+
# TODO I'm only guessing here, I don't actually know how other upload gems
|
16
|
+
# work, besides paperclip.
|
17
|
+
def path_method
|
18
|
+
View.path_methods.find { |method| value.respond_to?(method) }
|
19
|
+
end
|
20
|
+
|
21
|
+
# TODO with seems like a stupid name, but style is probably used for
|
22
|
+
# image_tag
|
23
|
+
def path_arguments
|
24
|
+
all_options[:with] || View.path_arguments
|
25
|
+
end
|
26
|
+
|
27
|
+
def file?
|
28
|
+
value.send(file_method)
|
29
|
+
end
|
30
|
+
|
31
|
+
def file_method
|
32
|
+
View.file_methods.find { |method| value.respond_to?(method) }
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
class Link < Formatter
|
4
|
+
|
5
|
+
self.reserved_options = [ :to, :path, :text ]
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
template.link_to(format, to, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def to
|
12
|
+
all_options[:to] || template.polymorphic_path(automatic_link)
|
13
|
+
end
|
14
|
+
|
15
|
+
def automatic_link
|
16
|
+
(all_options[:path] || []) + [ value ]
|
17
|
+
end
|
18
|
+
|
19
|
+
def as
|
20
|
+
all_options[:text] || :auto
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
class Sentence < Formatter
|
4
|
+
|
5
|
+
self.allowed_options = [ :words_connector, :two_words_connector, :last_word_connector ]
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
if all_safe?
|
9
|
+
sentence.html_safe
|
10
|
+
else
|
11
|
+
sentence
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def all_safe?
|
16
|
+
formatted_values.all? { |element| element.html_safe? }
|
17
|
+
end
|
18
|
+
|
19
|
+
def formatted_values
|
20
|
+
value.map { |element| View.to_s(element, each, template, &block) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def sentence
|
24
|
+
formatted_values.to_sentence if value.present?
|
25
|
+
end
|
26
|
+
|
27
|
+
def each
|
28
|
+
all_options[:each] || { :as => View.default_formatter }
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/lib/view/helper.rb
ADDED
data/lib/view/railtie.rb
ADDED
data/lib/view/version.rb
ADDED
data/lib/view.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module View
|
2
|
+
|
3
|
+
autoload :Formatter, 'view/formatter'
|
4
|
+
autoload :Helper, 'view/helper'
|
5
|
+
autoload :VERSION, 'view/version'
|
6
|
+
|
7
|
+
mattr_accessor :guessing_methods
|
8
|
+
self.guessing_methods = %w|to_label display_name full_name name title username login value to_s|
|
9
|
+
|
10
|
+
mattr_accessor :file_methods
|
11
|
+
self.file_methods = %w|mounted_as file? public_filename|
|
12
|
+
|
13
|
+
mattr_accessor :path_methods
|
14
|
+
self.path_methods = %w|mounted_as url public_filename|
|
15
|
+
|
16
|
+
mattr_accessor :path_arguments
|
17
|
+
self.path_arguments = []
|
18
|
+
|
19
|
+
mattr_accessor :default_formatter
|
20
|
+
self.default_formatter = :auto
|
21
|
+
|
22
|
+
mattr_accessor :formatters
|
23
|
+
|
24
|
+
|
25
|
+
def self.to_s(value, options = {}, template = nil, &block)
|
26
|
+
Formatter.to_s(value, options, template, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
Bundler.setup :default
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/core_ext'
|
6
|
+
require 'rspec'
|
7
|
+
require 'view'
|
8
|
+
|
9
|
+
module WithTranslation
|
10
|
+
def with_translation(keys)
|
11
|
+
locale = I18n.locale
|
12
|
+
I18n.backend.store_translations(:test, keys)
|
13
|
+
I18n.locale = :test
|
14
|
+
yield
|
15
|
+
ensure
|
16
|
+
I18n.locale = locale
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.include(WithTranslation)
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Auto formatter" do
|
4
|
+
|
5
|
+
it "guesses the to_label" do
|
6
|
+
object = Struct.new(:to_label).new("some label")
|
7
|
+
View.to_s(object).should == "some label"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "guesses to_s" do
|
11
|
+
object = Struct.new(:to_s).new("string")
|
12
|
+
View.to_s(object).should == "string"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "guesses a name" do
|
16
|
+
object = Struct.new(:name).new("my name")
|
17
|
+
View.to_s(object).should == "my name"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "guesses a login" do
|
21
|
+
object = Struct.new(:login).new("loginname")
|
22
|
+
View.to_s(object).should == "loginname"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Boolean formatter" do
|
4
|
+
|
5
|
+
it "formats true" do
|
6
|
+
View.to_s(true).should == "Yes"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "formats false" do
|
10
|
+
View.to_s(false).should == "No"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "formats boolean values" do
|
14
|
+
View.to_s("h", :as => :boolean).should == "Yes"
|
15
|
+
View.to_s(nil, :as => :boolean).should == "No"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "localizes booleans" do
|
19
|
+
with_translation :view => { :booleans => { :true => "yup" } } do
|
20
|
+
View.to_s(true).should == "yup"
|
21
|
+
end
|
22
|
+
with_translation :view => { :booleans => { :false => "nope" } } do
|
23
|
+
View.to_s(false).should == "nope"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Date formatter" do
|
4
|
+
|
5
|
+
it "localizes dates" do
|
6
|
+
date = Date.new(2010, 10, 11)
|
7
|
+
with_translation :date => { :formats => { :foo => "%A" }, :day_names => [ "zondag", "maandag" ] } do
|
8
|
+
View.to_s(date, :format => :foo).should == "maandag"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "formats dates" do
|
13
|
+
date = Date.new(2010, 10, 10)
|
14
|
+
View.to_s(date).should == "2010-10-10"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "forces datetimes to date" do
|
18
|
+
time = Time.new(2010, 10, 10, 8, 45, 30, '+01:00')
|
19
|
+
View.to_s(time, :as => :date).should == "2010-10-10"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Datetime formatter" do
|
4
|
+
|
5
|
+
it "formats" do
|
6
|
+
time = Time.new(2010, 10, 10, 8, 45, 30, '+01:00')
|
7
|
+
View.to_s(time).should == "Sun, 10 Oct 2010 08:45:30 +0100"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "localizes" do
|
11
|
+
time = Time.new(2010, 10, 10, 8, 45, 30, '+01:00')
|
12
|
+
with_translation :time => { :formats => { :short => "%d-%m-%Y %H:%M" } } do
|
13
|
+
View.to_s(time, :format => :short).should == "10-10-2010 08:45"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Nil formatter" do
|
4
|
+
|
5
|
+
it "formats nil" do
|
6
|
+
View.to_s(nil).should == ""
|
7
|
+
end
|
8
|
+
|
9
|
+
it "uses i18n for nil" do
|
10
|
+
with_translation :view => { :nil => "nothing" } do
|
11
|
+
View.to_s(nil).should == "nothing"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: view
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: true
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- alpha
|
10
|
+
- 1
|
11
|
+
version: 1.0.0.alpha.1
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Iain Hecker
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-10-03 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: A very extensible way of viewing objects, easily integrated with other gems
|
24
|
+
email:
|
25
|
+
- iain@iain.nl
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README.rdoc
|
32
|
+
files:
|
33
|
+
- lib/view/formatter.rb
|
34
|
+
- lib/view/formatters/auto.rb
|
35
|
+
- lib/view/formatters/boolean.rb
|
36
|
+
- lib/view/formatters/currency.rb
|
37
|
+
- lib/view/formatters/date.rb
|
38
|
+
- lib/view/formatters/datetime.rb
|
39
|
+
- lib/view/formatters/delimited.rb
|
40
|
+
- lib/view/formatters/file_link.rb
|
41
|
+
- lib/view/formatters/html_safe.rb
|
42
|
+
- lib/view/formatters/human.rb
|
43
|
+
- lib/view/formatters/image.rb
|
44
|
+
- lib/view/formatters/link.rb
|
45
|
+
- lib/view/formatters/nil.rb
|
46
|
+
- lib/view/formatters/percentage.rb
|
47
|
+
- lib/view/formatters/phone.rb
|
48
|
+
- lib/view/formatters/precision.rb
|
49
|
+
- lib/view/formatters/self.rb
|
50
|
+
- lib/view/formatters/sentence.rb
|
51
|
+
- lib/view/formatters/size.rb
|
52
|
+
- lib/view/helper.rb
|
53
|
+
- lib/view/railtie.rb
|
54
|
+
- lib/view/version.rb
|
55
|
+
- lib/view.rb
|
56
|
+
- init.rb
|
57
|
+
- README.rdoc
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
- spec/view/formatters/auto_spec.rb
|
60
|
+
- spec/view/formatters/boolean_spec.rb
|
61
|
+
- spec/view/formatters/date_spec.rb
|
62
|
+
- spec/view/formatters/datetime_spec.rb
|
63
|
+
- spec/view/formatters/nil_spec.rb
|
64
|
+
- spec/view/formatters/self_spec.rb
|
65
|
+
- spec/view/formatters/sentence_spec.rb
|
66
|
+
- spec/view/helper_spec.rb
|
67
|
+
- spec/view/view_spec.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: http://github.com/iain/view
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 1
|
92
|
+
- 3
|
93
|
+
- 6
|
94
|
+
version: 1.3.6
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project: view
|
98
|
+
rubygems_version: 1.3.7
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Displaying objects automatically
|
102
|
+
test_files:
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/view/formatters/auto_spec.rb
|
105
|
+
- spec/view/formatters/boolean_spec.rb
|
106
|
+
- spec/view/formatters/date_spec.rb
|
107
|
+
- spec/view/formatters/datetime_spec.rb
|
108
|
+
- spec/view/formatters/nil_spec.rb
|
109
|
+
- spec/view/formatters/self_spec.rb
|
110
|
+
- spec/view/formatters/sentence_spec.rb
|
111
|
+
- spec/view/helper_spec.rb
|
112
|
+
- spec/view/view_spec.rb
|