has_meta 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/has_meta.png)](http://badge.fury.io/rb/has_meta)
4
4
 
5
5
  Adds convenience methods to extract "meta" (as in http meta) strings from
6
- models by using existing fields for source data. Strings are stripped of html
6
+ models by using existing fields or lambda/Procs for source data. Result is stripped of html
7
7
  tags and truncated to length (default 255).
8
8
 
9
9
  ## Installation
@@ -23,7 +23,13 @@ Or install it yourself as:
23
23
  ## Usage
24
24
 
25
25
  class BlogPost < ActiveRecord::Base
26
- has_meta :keywords => :keywords, :description => [:short_description, :content]
26
+ has_meta :keywords => :keywords,
27
+ :description => [:short_description, :content],
28
+ :foo => lambda {|o| o.some_instance_method }
29
+
30
+ def some_instance_method
31
+ Time.now
32
+ end
27
33
  end
28
34
 
29
35
  bp = BlogPost.new(...)
@@ -36,6 +42,11 @@ Or install it yourself as:
36
42
  # if short_description is blank then
37
43
  bp.meta_description == bp.content.slice(0,255)
38
44
 
45
+ # blocks will be passed an instance of the object itself
46
+ bp.meta_foo == "Feb 27, 4:36:00 PM" # for example
47
+ sleep 1
48
+ bp.meta_foo == "Feb 27, 4:36:01 PM" # one second later
49
+
39
50
  ## Contributing
40
51
 
41
52
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module HasMeta
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/has_meta.rb CHANGED
@@ -14,9 +14,15 @@ module HasMeta
14
14
  define_method("meta_#{meth}") {|*args|
15
15
  length = args.first if args.is_a? Array
16
16
  length ||= 255
17
- field = [*fields].detect{|f| send(f).present?}
18
- return nil if field.nil?
19
- str = send(field).to_s.strip
17
+
18
+ if fields.is_a? Proc
19
+ str = fields.call(self)
20
+ else
21
+ field = [*fields].detect{|f| send(f).present?}
22
+ return nil if field.nil?
23
+ str = send(field).to_s.strip
24
+ end
25
+
20
26
  str.gsub!('&nbsp;', ' ')
21
27
  str.gsub!(/<.*?>/, '')
22
28
  str = ::CGI::unescapeHTML(str)
@@ -8,8 +8,16 @@ require 'has_meta'
8
8
  class Widget
9
9
  include ActiveModel::AttributeMethods
10
10
  include HasMeta::Extensions
11
- attr_accessor :short_description, :content, :keywords
12
- has_meta :description => [:short_description, :content], :keywords => :keywords
11
+
12
+ attr_accessor :short_description, :content, :keywords, :some_ivar
13
+
14
+ has_meta :description => [:short_description, :content],
15
+ :keywords => :keywords,
16
+ :keyword_block => lambda {|o| o.some_instance_method }
17
+
18
+ def some_instance_method
19
+ "ivar is #{@some_ivar}"
20
+ end
13
21
  end
14
22
 
15
23
  ################################################################################
@@ -21,6 +29,7 @@ class HasMetaTest < Test::Unit::TestCase
21
29
  @widget.short_description = 'Short Description'
22
30
  @widget.content = 'Long Description'
23
31
  @widget.keywords = ''
32
+ @widget.some_ivar = 1
24
33
  end
25
34
 
26
35
  def test_knows_its_meta_description
@@ -45,4 +54,9 @@ class HasMetaTest < Test::Unit::TestCase
45
54
  assert_equal nil, @widget.meta_keywords
46
55
  end
47
56
 
57
+ def test_knows_its_meta_keyword_block
58
+ @widget.some_ivar = 2 # to make sure block is lazily executed
59
+ assert_equal "ivar is 2", @widget.meta_keyword_block
60
+ end
61
+
48
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_meta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-24 00:00:00.000000000 Z
12
+ date: 2013-02-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Adds convenience methods to extract "meta" (as in http meta) strings
15
15
  from models.