myobie-turbine-core 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 4
4
- :patch: 0
3
+ :minor: 5
4
+ :patch: 1
@@ -1,32 +1,35 @@
1
1
  require 'rdiscount' # markdown
2
2
  require 'nokogiri'
3
3
  require 'uuidtools'
4
+ require 'haml'
4
5
 
5
6
  class PostType
6
7
  extend ClassLevelInheritableAttributes
7
8
  include Extlib::Hook
8
9
 
9
10
  DEFAULT_FIELDS = [:published_at, :status, :slug, :trackbacks, :type, :tags]
10
- NON_EDITABLE_FIELDS = [:trackbacks, :type, :published_at, :status] # don't show up in to_s
11
+ NON_EDITABLE_FIELDS = [:trackbacks, :type, :published_at]
12
+
11
13
 
12
14
  # vars to inherit down
13
15
  cattr_inheritable :fields_list, :allowed_fields_list, :required_fields_list, :primary_field, :heading_field,
14
- :specials_blocks, :defaults_blocks, :only_declared_fields, :always_use_uuid,
15
- :truncate_slugs, :markdown_fields
16
+ :specials_blocks, :defaults_blocks, :string_for_blocks, :only_declared_fields, :always_use_uuid,
17
+ :truncate_slugs, :markdown_fields, :html_block
16
18
 
17
19
  # defaults for class instance inheritable vars
18
20
  @fields_list = []
19
- @allowed_fields_list = []
21
+ @allowed_fields_list = DEFAULT_FIELDS
20
22
  @required_fields_list = []
21
23
  @primary_field = nil
22
24
  @heading_field = nil
23
25
  @specials_blocks = {}
24
26
  @defaults_blocks = {}
27
+ @string_for_blocks = {}
25
28
  @only_declared_fields = true
26
29
  @always_use_uuid = false
27
30
  @truncate_slugs = true
28
31
  @markdown_fields = []
29
-
32
+ @html_block = nil
30
33
 
31
34
  ### cattr_accessor
32
35
  @@preferred_order = []
@@ -39,10 +42,31 @@ class PostType
39
42
  end
40
43
  ### cattr_accessor
41
44
 
45
+ ### Defaults
46
+ after_class_method :inherited do |all_children, klass|
47
+ klass.string_for :tags do |tags_array|
48
+ tags_array.join(', ')
49
+ end
50
+
51
+ klass.special :tags do |tags_string|
52
+ if tags_string.is_a? String
53
+ tags_string.split(',').collect { |t| t.strip }
54
+ else
55
+ tags_string
56
+ end
57
+ end
58
+
59
+ klass.default :slug do
60
+ generate_slug
61
+ end
62
+
63
+ all_children #return original result
64
+ end
65
+
42
66
  attr_accessor :content # where everything is stored
43
67
 
44
68
  ### basic setup methods for types of posts
45
- def self.fields(*list) # NOTE: this is a replacing function, not addititve like the others
69
+ def self.fields(*list)
46
70
  self.fields_list = list.make_attrs
47
71
  self.allowed_fields_list = [DEFAULT_FIELDS, list.make_attrs].flatten.uniq
48
72
  end
@@ -74,12 +98,26 @@ class PostType
74
98
 
75
99
  def self.special(field, &block)
76
100
  field = field.make_attr
77
- self.specials_blocks[field.make_attr] = block if fields_list.include? field
101
+ self.specials_blocks[field] = block if allowed_fields_list.include? field
102
+ end
103
+
104
+ def self.string_for(field, &block)
105
+ field = field.make_attr
106
+ self.string_for_blocks[field] = block if allowed_fields_list.include? field
107
+ end
108
+
109
+ ### Defaults
110
+ string_for :tags do |content|
111
+ content.join(', ')
78
112
  end
79
113
 
80
114
  def self.default(field, &block)
81
115
  field = field.make_attr
82
- self.defaults_blocks[field.make_attr] = block if fields_list.include? field
116
+ self.defaults_blocks[field] = block if allowed_fields_list.include? field
117
+ end
118
+
119
+ def self.html(&block)
120
+ self.html_block = block
83
121
  end
84
122
 
85
123
  def self.dynamic(field, &block)
@@ -105,8 +143,14 @@ class PostType
105
143
  key = key.make_attr
106
144
 
107
145
  unless value.blank?
108
- @content[key] = value
146
+ # run specials if there is one, or just set the key
147
+ if self.class.specials_blocks[key]
148
+ @content[key] = self.class.specials_blocks[key].call(value)
149
+ else
150
+ @content[key] = value
151
+ end
109
152
 
153
+ # run markdowns if there is one
110
154
  if self.class.markdown_fields.include?(key)
111
155
  markdown = Markdown.new @content[key].strip
112
156
  @content[key.html] = markdown.to_html.strip
@@ -114,6 +158,7 @@ class PostType
114
158
  @content.delete(key.html)
115
159
  end
116
160
  else
161
+ # remove any blank valued keys
117
162
  @content.delete(key)
118
163
  @content.delete(key.html)
119
164
  end
@@ -257,11 +302,6 @@ class PostType
257
302
  when Hash
258
303
  commit_hash(result)
259
304
  end
260
-
261
- eval_specials # this won't be needed once the set_attr takes care of it
262
- eval_defaults # this won't be needed once the get_attr takes care of it
263
- parse_tags # TODO: parse_tags needs to be turned into a special block
264
- generate_slug # TODO: generate_slug should be turned into a default block
265
305
  end
266
306
 
267
307
  def commit_hash(pairs_hash)
@@ -283,19 +323,12 @@ class PostType
283
323
  end
284
324
  end
285
325
  end
286
-
287
- def eval_specials
288
- self.class.specials_blocks.each do |key, block|
289
- unless get_attr(key).blank?
290
- set_attr(key, block.call(get_attr(key)))
291
- end
292
- end
293
- end
294
-
295
- def parse_tags
296
- if get_attr?(:tags) && get_attr(:tags).class == String
297
- tags_array = get_attr(:tags).split(',').collect { |t| t.strip }
298
- set_attr(:tags, tags_array)
326
+
327
+ def get_string_for key
328
+ if self.class.string_for_blocks[key]
329
+ self.class.string_for_blocks[key].call(get_attr(key))
330
+ else
331
+ get_attr(key)
299
332
  end
300
333
  end
301
334
 
@@ -314,11 +347,10 @@ class PostType
314
347
  def fill_default_fields
315
348
  set_default(:published_at, Time.now.utc)
316
349
  set_default(:status, default_status)
350
+ eval_defaults
317
351
  end
318
352
 
319
353
  def generate_slug # OPTIMIZE: this slug generation is ugly
320
- return unless blank_attr?(:slug)
321
-
322
354
  result = ''
323
355
 
324
356
  unless self.class.always_use_uuid
@@ -344,7 +376,7 @@ class PostType
344
376
  result = uuid
345
377
  end
346
378
 
347
- set_attr(:slug, result)
379
+ result
348
380
  end
349
381
 
350
382
  def truncate_slug(letter_count = 50)
@@ -371,19 +403,27 @@ class PostType
371
403
 
372
404
  result = fields_to_parse.map do |field|
373
405
  unless blank_attr?(field)
374
-
375
- "#{field}: #{get_attr(field)}"
376
-
377
- # TODO: make a way to override this like string_for :content { "hello" }
378
-
406
+ "#{field}: #{get_string_for(field)}"
379
407
  end#unless
380
- end.compact.join("\n") << "\n\n"
408
+ end.compact.join("\n")
381
409
 
382
410
  unless self.class.primary_field.blank? || blank_attr?(self.class.primary_field)
383
- result << get_attr(self.class.primary_field, false)
411
+ result << "\n\n" + get_attr(self.class.primary_field, false)
384
412
  end
385
413
 
386
414
  result.strip
387
415
  end
388
416
 
417
+ def haml(data, options = {}, locals = {})
418
+ ::Haml::Engine.new(data, options).render(self, locals)
419
+ end
420
+
421
+ def to_html
422
+ unless self.class.html_block.blank?
423
+ self.instance_eval(&self.class.html_block)
424
+ else
425
+ Markdown.new(to_s).to_html
426
+ end
427
+ end
428
+
389
429
  end
@@ -4,6 +4,20 @@ class Article < PostType
4
4
  primary :body
5
5
  heading :title
6
6
 
7
+ html do
8
+ haml %Q{
9
+ - unless blank_attr?(:title)
10
+ %h2= get_attr(:title)
11
+
12
+ - unless blank_attr?(:category)
13
+ %p.category
14
+ Category:
15
+ = get_attr(:category)
16
+
17
+ ~ get_attr(:body)
18
+ }.indents(true)
19
+ end
20
+
7
21
  def self.detect?(text)
8
22
  true # it can always be an article, so make this last in the preferred order
9
23
  end
@@ -3,6 +3,18 @@ class Audio < PostType
3
3
  required :audio_url
4
4
  primary :description
5
5
 
6
+ html do
7
+ haml %Q{
8
+ - unless blank_attr?(:embed)
9
+ .embed= get_attr(:embed)
10
+
11
+ %p.url
12
+ %a{:href=>get_attr(:audio_url)}= get_attr(:audio_url)
13
+
14
+ ~ get_attr(:description)
15
+ }.indents(true)
16
+ end
17
+
6
18
  def self.detect?(text)
7
19
  has_required? text
8
20
  end
@@ -2,6 +2,16 @@ class Chat < PostType
2
2
  fields :transcript
3
3
  required :transcript
4
4
 
5
+ html do
6
+ haml %Q{
7
+ %ol
8
+ - get_attr(:transcript).each do |line|
9
+ %li
10
+ %strong= line.keys.first
11
+ = line.values.first
12
+ }.indents(true)
13
+ end
14
+
5
15
  # override commit_pairs to make chat transcripts form the Me: pairs stuff
6
16
  def commit_array(pairs_array)
7
17
  transcript = []
@@ -20,6 +20,15 @@ class Link < PostType
20
20
  result
21
21
  end
22
22
 
23
+ html do
24
+ haml %Q{
25
+ %h2
26
+ %a{:href=>get_attr(:url)}= get_attr(:title) || get_attr(:url)
27
+
28
+ ~ get_attr(:description)
29
+ }.indents(true)
30
+ end
31
+
23
32
  def self.detect?(text)
24
33
  has_required? text
25
34
  end
@@ -19,6 +19,19 @@ class Photo < PostType
19
19
  'http://' + link_content.gsub(/^http:\/\//, '')
20
20
  end
21
21
 
22
+ html do
23
+ haml %Q{
24
+ %p
25
+ - if get_attr?(:url)
26
+ %a{:href=>get_attr(:url)}
27
+ %img{:src=>get_attr(:photo_url)}
28
+ - else
29
+ %img{:src=>get_attr(:photo_url)}
30
+
31
+ ~ get_attr(:caption)
32
+ }.indents(true)
33
+ end
34
+
22
35
  def self.detect?(text)
23
36
  has_required? text
24
37
  end
@@ -13,6 +13,17 @@ class Quote < PostType
13
13
  quote_content.strip
14
14
  end
15
15
 
16
+ html do
17
+ haml %Q{
18
+ - unless blank_attr?(:source)
19
+ %p
20
+ Source:
21
+ = get_attr(:source)
22
+
23
+ %blockquote~ get_attr(:quote)
24
+ }.indents(true)
25
+ end
26
+
16
27
  def self.detect?(text)
17
28
  pairs = get_pairs(text)
18
29
 
@@ -12,6 +12,22 @@ class Review < PostType
12
12
  'http://' + link_content.gsub(/^http:\/\//, '')
13
13
  end
14
14
 
15
+ html do
16
+ haml %Q{
17
+ %h2
18
+ - if get_attr?(:url)
19
+ %a{:href=>get_attr(:url)}= get_attr(:item)
20
+ - else
21
+ = get_attr(:item)
22
+
23
+ %p
24
+ Rating:
25
+ = get_attr(:rating)
26
+
27
+ ~ get_attr(:description)
28
+ }.indents(true)
29
+ end
30
+
15
31
  def self.detect?(text)
16
32
  has_required? text
17
33
  end
@@ -3,6 +3,18 @@ class Video < PostType
3
3
  required :video_url
4
4
  primary :description
5
5
 
6
+ html do
7
+ haml %Q{
8
+ - unless blank_attr?(:embed)
9
+ .embed= get_attr(:embed)
10
+
11
+ %p.url
12
+ %a{:href=>get_attr(:video_url)}= get_attr(:video_url)
13
+
14
+ ~ get_attr(:description)
15
+ }.indents(true)
16
+ end
17
+
6
18
  def self.detect?(text)
7
19
  has_required? text
8
20
  end
@@ -124,7 +124,7 @@ This will be in One."
124
124
  end
125
125
 
126
126
  y = Yellow.new 'hello'
127
-
127
+ y.save # to trigger defaults
128
128
  y.get_attr(:two).should == 2
129
129
  end
130
130
 
@@ -171,4 +171,155 @@ This will be in One."
171
171
  # should save ?
172
172
  # should generate slug
173
173
 
174
+ should "to_s all fields" do
175
+ class Yellow < PostType
176
+ fields :one, :two, :title, :body
177
+ primary :body
178
+ heading :title
179
+ end
180
+
181
+ yellow = Yellow.new %Q{
182
+ One: hello1
183
+ Two: hello2
184
+
185
+ This is the title
186
+ =================
187
+
188
+ this is the body
189
+ }.indents
190
+
191
+ yellow.to_s.should == %Q{
192
+ one: hello1
193
+ two: hello2
194
+ title: This is the title
195
+
196
+ this is the body
197
+ }.indents
198
+ end
199
+
200
+ should "to_s all fields including slug (if it exists - after save)" do
201
+ class Yellow < PostType
202
+ fields :one, :two, :title, :body
203
+ primary :body
204
+ heading :title
205
+ end
206
+
207
+ yellow = Yellow.new %Q{
208
+ One: hello1
209
+ Two: hello2
210
+
211
+ This is the title
212
+ =================
213
+
214
+ this is the body
215
+ }.indents
216
+
217
+ yellow.save # to trigger defaults
218
+
219
+ yellow.to_s.should == %Q{
220
+ one: hello1
221
+ two: hello2
222
+ title: This is the title
223
+ status: published
224
+ slug: this-is-the-title
225
+
226
+ this is the body
227
+ }.indents
228
+ end
229
+
230
+ should "set string_for values" do
231
+ class Yellow < PostType
232
+ fields :one
233
+
234
+ string_for :one do |content|
235
+ content.upcase
236
+ end
237
+ end
238
+
239
+ Yellow.string_for_blocks[:one].should.not.be.blank?
240
+ end
241
+
242
+ should "use string_for values" do
243
+ class Yellow < PostType
244
+ fields :one
245
+
246
+ string_for :one do |content|
247
+ content.upcase
248
+ end
249
+ end
250
+
251
+ yellow = Yellow.new "one: hello"
252
+
253
+ yellow.to_s.should == "one: HELLO"
254
+ end
255
+
256
+ should "have a special block for tags" do
257
+ class Yellow < PostType; end
258
+
259
+ Yellow.specials_blocks[:tags].should.not.be.nil
260
+ end
261
+
262
+ should "store tags as an array" do
263
+ class Yellow < PostType; end
264
+ yellow = Yellow.new "tags: cat, dog, water"
265
+ yellow.get_attr(:tags).should == ['cat', 'dog', 'water']
266
+ end
267
+
268
+ should "have a string_for block for tags" do
269
+ class Yellow < PostType; end
270
+
271
+ Yellow.string_for_blocks[:tags].should.not.be.nil
272
+ end
273
+
274
+ should "always output the tags array as a comma separated string" do
275
+ class Yellow < PostType
276
+ fields :one
277
+ end
278
+
279
+ yellow = Yellow.new "one: hello\ntags: dog, cat, water"
280
+
281
+ yellow.to_s.should == "one: hello\ntags: dog, cat, water"
282
+ end
283
+
284
+ should "provide html" do
285
+ class Yellow < PostType
286
+ fields :one, :two, :three
287
+ primary :three
288
+ end
289
+
290
+ yellow = Yellow.new "one: hello\ntwo: there\n\nhello there"
291
+
292
+ yellow.to_html.should == "<p>one: hello\ntwo: there</p>\n\n<p>hello there</p>\n"
293
+ end
294
+
295
+ should "keep an html block if provided" do
296
+ class Yellow < PostType
297
+ fields :one
298
+
299
+ html {
300
+ haml %Q{
301
+ %h1= get_attr(:one)
302
+ }.indents
303
+ }
304
+ end
305
+
306
+ Yellow.html_block.should.not.be.nil
307
+ end
308
+
309
+ should "use an html block if provided" do
310
+ class Yellow < PostType
311
+ fields :one
312
+
313
+ html {
314
+ haml %Q{
315
+ %h1= get_attr(:one)
316
+ }.indents(true)
317
+ }
318
+ end
319
+
320
+ yellow = Yellow.new "one: hello"
321
+
322
+ yellow.to_html.should == "<h1>hello</h1>\n"
323
+ end
324
+
174
325
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myobie-turbine-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Herald
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-18 00:00:00 -07:00
12
+ date: 2009-04-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -38,7 +38,6 @@ files:
38
38
  - lib/turbine-core/types/quote.rb
39
39
  - lib/turbine-core/types/review.rb
40
40
  - lib/turbine-core/types/video.rb
41
- - spec/general/formats_spec.rb
42
41
  - spec/general/post_type_spec.rb
43
42
  - spec/post_types/chat_spec.rb
44
43
  - spec/post_types/link_spec.rb
@@ -74,7 +73,6 @@ signing_key:
74
73
  specification_version: 2
75
74
  summary: TODO
76
75
  test_files:
77
- - spec/general/formats_spec.rb
78
76
  - spec/general/post_type_spec.rb
79
77
  - spec/post_types/chat_spec.rb
80
78
  - spec/post_types/link_spec.rb
@@ -1,37 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '../spec_helper.rb')
2
-
3
- describe PostType do
4
-
5
- before do
6
- class Yellow; end
7
- Object.send(:remove_const, :Yellow)
8
- end
9
-
10
- should "to_s all fields" do
11
- class Yellow < PostType
12
- fields :one, :two, :title, :body
13
- primary :body
14
- heading :title
15
- end
16
-
17
- yellow = Yellow.new %Q{
18
- One: hello1
19
- Two: hello2
20
-
21
- This is the title
22
- =================
23
-
24
- this is the body
25
- }.indents
26
-
27
- yellow.to_s.should == %Q{
28
- one: hello1
29
- two: hello2
30
- title: This is the title
31
- slug: this-is-the-title
32
-
33
- this is the body
34
- }.indents
35
- end
36
-
37
- end