hobofields 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,215 @@
1
+ # HoboFields -- Rich Types
2
+
3
+ This doctest describes the rich types bundles with HoboFields, and the process by which you can create and register your own types.
4
+
5
+ >> require 'rubygems'
6
+ >> require 'activesupport'
7
+ >> Dependencies.load_paths << '.'
8
+ >> Dependencies.mechanism = :require
9
+ >> require 'activerecord'
10
+ >> require 'hobofields'
11
+
12
+
13
+ ## `to_html` method
14
+
15
+ The rich types provide a `to_html` method. If you are using the full Hobo stack you don't need to be aware of this unless you're defining your own rich types -- the `<view>` tag uses `to_html` to render a rich type. If you are not using DRYML and Rapid, you can simply call `to_html` in your views, e.g.
16
+
17
+ <div class="post-body"><%= @post.body.to_html %></div>
18
+
19
+ If you ever decide to change from, say, plain text to markdown formatted, your view won't need to change.
20
+
21
+ ## Defining your own Rich Type
22
+
23
+ Defining a rich type is very simple. We'll show an example here before we go through the bundled types so that you start with the idea that there's really nothing sophisticated going on here.
24
+
25
+ This class defines the methods `to_html` to customize the way the type is rendered, and `validate` to provide a custom validation. It also defined the `COLUMN_TYPE` constant to tell the migration generator what underlying type should represent these values in the database.
26
+
27
+ # Loud text always renderd in caps.
28
+ # It's rude to shout too much so it's not allowed to be
29
+ # longer than 100 characters
30
+ class LoudText < String
31
+
32
+ COLUMN_TYPE = :string
33
+
34
+ HoboFields.register_type(:loud, self)
35
+
36
+ def validate
37
+ "is too long (you shouldn't shout that much)" if length > 100
38
+ end
39
+
40
+ def to_html
41
+ upcase
42
+ end
43
+
44
+ end
45
+
46
+ That's all there is to it. Defining `to_html` and `validate` are optional, defining `COLUMN_TYPE` and calling `HoboFields.register_type` are not.
47
+
48
+
49
+ ## Bundled types
50
+
51
+ Here we'll give a quick overview of the bundles types. Remember that, along with the specific features the type provides (e.g. validation), the simple fact that the type exists is also useful in the other layers of Hobo. For example `HoboFields::PasswordString` doesn't add any features to `String`, but the fact that a specific type exists for passwords means that the view layer can automatically render an `<input type="password">`.
52
+
53
+
54
+ ### `HoboFields::EmailAddress`
55
+
56
+ Provides validation of correct email address format.
57
+
58
+ >> good = HoboFields::EmailAddress.new("foo@baa.com")
59
+ >> bad = HoboFields::EmailAddress.new("foo.baa.com")
60
+ >> !!good.valid?
61
+ => true
62
+ >> good.validate
63
+ => nil
64
+ >> !!bad.valid?
65
+ => false
66
+ >> bad.validate
67
+ => "is not valid"
68
+
69
+ ### `HoboFields::HtmlString`
70
+
71
+ `HtmlString` provides no special behavior. The main reason for using this type is that the `to_html` method does not do any html-escaping. Use this for columns that store raw HTML in the database.
72
+
73
+ ### `HoboFields::MarkdownString`
74
+
75
+ `HoboFields::MarkdownString` provides a `to_html` that renders markdown syntax into html. It requires the bluecloth gem.
76
+
77
+ >> require 'bluecloth'
78
+ >> markdown = HoboFields::MarkdownString.new %(
79
+ # This is a heading
80
+
81
+ And text can be *emphasised*
82
+ )
83
+ >> markdown.to_html
84
+ =>""
85
+ <h1>This is a heading</h1>
86
+
87
+ <p>And text can be <em>emphasised</em></p>
88
+ >>
89
+
90
+ ### `HoboFields::TextileString`
91
+
92
+ `HoboFields::TextileString` provides a `to_html` that renders markdown syntax into html. It requires the redcloth gem.
93
+
94
+ >> require 'redcloth'
95
+ >> textile = HoboFields::TextileString.new %(
96
+ Text can be _emphasised_
97
+ )
98
+ >> textile.to_html
99
+ =>""
100
+ <p>Text can be <em>emphasised</em></p>
101
+ >>
102
+
103
+ ### `HoboFields::Text`
104
+
105
+ `HoboFields::Text` provides a `to_html` method with HTML escaping and conversion of newlines to `<br />` tags.
106
+
107
+ >> text = HoboFields::Text.new %(Tom & Jerry
108
+
109
+ Cat & Mouse)
110
+ >> text.to_html
111
+ =>""
112
+ Tom &amp; Jerry<br />
113
+ <br />
114
+ Cat &amp; Mouse
115
+ >>
116
+
117
+ ### `HoboFields::PasswordString`
118
+
119
+ `HoboFields::PasswordString` provides a simple `to_html` to prevent accidental display of a password. It simply returns "`[password hidden]`". The type is also used to indicate the need for an `<input type='password'>`
120
+
121
+
122
+ ## Enum Strings
123
+
124
+ `HoboFields::EnumString` is not a rich type that you use directly. It's a "type generator", rather like Ruby's `Struct`. It's used for the common situation in database driven apps that you want an enumerated type, but it's not worth going to the extra bother of a separate table enumerating the values. For example you could create a type to represent the status of an article:
125
+
126
+ >> ArticleStatus = HoboFields::EnumString.for(:draft, :approved, :published)
127
+ => ArticleStatus
128
+
129
+ Note that, like all dynamically created classes in Ruby, the class is anonymous until assigned to a constant:
130
+
131
+ >> klass = HoboFields::EnumString.for(:draft, :approved, :published)
132
+ => #<EnumString draft approved published>
133
+ >> AritcleStatus = klass
134
+ >> ArticleStatus
135
+ => ArticleStatus
136
+
137
+ The values in the enum are available as class constants:
138
+
139
+ >> ArticleStatus::DRAFT
140
+ => "draft"
141
+ >> ArticleStatus::DRAFT.class
142
+ => ArticleStatus
143
+
144
+ There are also instance methods to check for each of the values:
145
+
146
+ >> a = ArticleStatus::APPROVED
147
+ >> a.is_draft?
148
+ => false
149
+ >> a.is_approved?
150
+ => true
151
+
152
+ They can be constructed from strings:
153
+
154
+ >> a = ArticleStatus.new("approved")
155
+ >> a.is_approved?
156
+ => true
157
+
158
+ Equality is string equality, with symbols first converted to strings:
159
+
160
+ >> a == "approved"
161
+ => true
162
+ >> a == :approved
163
+ => true
164
+
165
+
166
+ Note that every enum you create is a subclass of HoboFields::EnumString:
167
+
168
+ >> a.is_a?(HoboFields::EnumString)
169
+ => true
170
+
171
+
172
+ ### Using EnumString in your models
173
+
174
+ `HoboFields::EnumString` extends the field declaration DSL with a shorthand for creating enum types:
175
+
176
+ >>
177
+ class Article < ActiveRecord::Base
178
+ fields do
179
+ status enum_string(:draft, :approved, :published)
180
+ end
181
+ end
182
+ >> Article.attr_type :status
183
+ #<EnumString draft approved published>
184
+
185
+ Sometimes it's nice to have a proper type name. Here's one way you might go about it:
186
+
187
+ >>
188
+ class Article < ActiveRecord::Base
189
+ Status = HoboFields::EnumString.for(:draft, :approved, :published)
190
+ fields do
191
+ status Status
192
+ end
193
+ end
194
+ >> Article.attr_type :status
195
+ => Article::Status
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hobofields
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.5
5
+ platform: ruby
6
+ authors:
7
+ - Tom Locke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-18 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rich field types and migration generator for Rails
17
+ email: tom@tomlocke.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/hobo_fields/email_address.rb
24
+ - lib/hobo_fields/enum_string.rb
25
+ - lib/hobo_fields/field_declaration_dsl.rb
26
+ - lib/hobo_fields/field_spec.rb
27
+ - lib/hobo_fields/fields_declaration.rb
28
+ - lib/hobo_fields/html_string.rb
29
+ - lib/hobo_fields/markdown_string.rb
30
+ - lib/hobo_fields/migration_generator.rb
31
+ - lib/hobo_fields/model_extensions.rb
32
+ - lib/hobo_fields/password_string.rb
33
+ - lib/hobo_fields/text.rb
34
+ - lib/hobo_fields/textile_string.rb
35
+ - lib/hobo_fields.rb
36
+ - lib/hobofields.rb
37
+ - LICENSE.txt
38
+ - README.txt
39
+ files:
40
+ - CHANGES.txt
41
+ - generators/hobo_migration/hobo_migration_generator.rb
42
+ - generators/hobo_migration/templates/migration.rb
43
+ - init.rb
44
+ - lib/hobo_fields/email_address.rb
45
+ - lib/hobo_fields/enum_string.rb
46
+ - lib/hobo_fields/field_declaration_dsl.rb
47
+ - lib/hobo_fields/field_spec.rb
48
+ - lib/hobo_fields/fields_declaration.rb
49
+ - lib/hobo_fields/html_string.rb
50
+ - lib/hobo_fields/markdown_string.rb
51
+ - lib/hobo_fields/migration_generator.rb
52
+ - lib/hobo_fields/model_extensions.rb
53
+ - lib/hobo_fields/password_string.rb
54
+ - lib/hobo_fields/text.rb
55
+ - lib/hobo_fields/textile_string.rb
56
+ - lib/hobo_fields.rb
57
+ - lib/hobofields.rb
58
+ - LICENSE.txt
59
+ - README.txt
60
+ - test/hobofields.rdoctest
61
+ - test/hobofields_api.rdoctest
62
+ - test/migration_generator.rdoctest
63
+ - test/rich_types.rdoctest
64
+ - Manifest
65
+ - hobofields.gemspec
66
+ has_rdoc: true
67
+ homepage: http://hobocentral.net/hobofields
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --line-numbers
71
+ - --inline-source
72
+ - --title
73
+ - Hobofields
74
+ - --main
75
+ - README.txt
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project: hobo
93
+ rubygems_version: 1.0.1
94
+ signing_key:
95
+ specification_version: 2
96
+ summary: Rich field types and migration generator for Rails
97
+ test_files: []
98
+