monkeysupport 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,15 +5,14 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "monkeysupport"
8
- gem.summary = %Q{Monkeypatching Rails with C since 2009}
9
- gem.description = %Q{MonkeySupport provides C implementations for some of the more intensive string manipulation methods in activesupport. ActionView is up next.}
8
+ gem.summary = "Monkeypatching Rails with C since 2009"
9
+ gem.description = "MonkeySupport provides C implementations for some of the more intensive string manipulation methods in activesupport."
10
10
  gem.email = "burke@burkelibbey.org"
11
11
  gem.homepage = "http://github.com/burke/monkeysupport"
12
12
  gem.authors = ["Burke Libbey"]
13
13
  gem.files.include '{test,lib,ext}/**/*'
14
- gem.extensions = ["ext/monkeysupport_c/extconf.rb"]
15
- gem.add_development_dependency "thoughtbot-shoulda"
16
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ gem.extensions = ["ext/monkeysupport_c/extconf.rb", "ext/output_safety_ext/extconf.rb"]
15
+ gem.add_development_dependency "shoulda"
17
16
  end
18
17
  rescue LoadError
19
18
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = 'output_safety_ext'
4
+ dir_config(extension_name)
5
+ create_makefile(extension_name, '.')
@@ -0,0 +1,89 @@
1
+ #include "stdbool.h"
2
+
3
+ #include "ruby.h"
4
+ #include "ruby/version.h"
5
+
6
+ // Use a FL_USER flag for storing html safety information.
7
+ // fingers crossed here, this is black black magic.
8
+ #ifdef FL_USER4
9
+
10
+ #define HFLAG FL_USER4
11
+
12
+ // by default, objects are not safe for html.
13
+ VALUE obj_html_safe_qmark(VALUE self) { return Qfalse; }
14
+
15
+ //fixnums are always safe for html.
16
+ VALUE fix_html_safe_qmark(VALUE self) { return Qtrue; }
17
+
18
+ VALUE // mark a string as safe for html.
19
+ str_html_safe_bang(VALUE self)
20
+ {
21
+ FL_SET(self, HFLAG);
22
+ return self;
23
+ }
24
+
25
+ VALUE // is this string safe for html?
26
+ str_html_safe_qmark(VALUE self)
27
+ {
28
+ return FL_TEST(self, HFLAG) ? Qtrue : Qfalse;
29
+ }
30
+
31
+ void
32
+ set_html_safe(VALUE obj, bool val)
33
+ {
34
+ if (val) {
35
+ FL_SET(obj, HFLAG);
36
+ } else {
37
+ FL_UNSET(obj, HFLAG);
38
+ }
39
+ }
40
+
41
+
42
+ VALUE // return a copy of this string, marked as safe for html.
43
+ str_html_safe(VALUE self)
44
+ {
45
+ VALUE other = rb_obj_dup(self);
46
+ return str_html_safe_bang(other);
47
+ }
48
+
49
+ VALUE
50
+ str_plus(VALUE self, VALUE other)
51
+ {
52
+ VALUE ret;
53
+ bool safe = ((str_html_safe_qmark(self) == Qtrue) &&
54
+ (str_html_safe_qmark(other) == Qtrue));
55
+
56
+ ret = rb_str_plus(self, other);
57
+
58
+ set_html_safe(ret, safe);
59
+ return ret;
60
+ }
61
+
62
+ VALUE
63
+ str_concat(VALUE self, VALUE other)
64
+ {
65
+ bool safe = ((str_html_safe_qmark(self) == Qtrue) &&
66
+ (str_html_safe_qmark(other) == Qtrue));
67
+
68
+ rb_str_concat(self, other);
69
+
70
+ set_html_safe(self, safe);
71
+ return self;
72
+ }
73
+
74
+ void
75
+ Init_output_safety_ext()
76
+ {
77
+ rb_define_method(rb_cObject, "html_safe?", obj_html_safe_qmark, 0);
78
+ rb_define_method(rb_cFixnum, "html_safe?", fix_html_safe_qmark, 0);
79
+
80
+ rb_define_method(rb_cString, "html_safe", str_html_safe, 0);
81
+ rb_define_method(rb_cString, "html_safe!", str_html_safe_bang, 0);
82
+ rb_define_method(rb_cString, "html_safe?", str_html_safe_qmark, 0);
83
+ rb_define_method(rb_cString, "_rails_html_safe", str_html_safe_qmark, 0);
84
+
85
+ rb_define_method(rb_cString, "+", str_plus, 1);
86
+ rb_define_method(rb_cString, "<<", str_concat, 1);
87
+ }
88
+
89
+ #endif
data/lib/monkeysupport.rb CHANGED
@@ -4,3 +4,9 @@ require 'monkeysupport/c_proxy'
4
4
  require 'monkeysupport/memoizable'
5
5
 
6
6
  require 'monkeysupport/activesupport/inflector'
7
+
8
+ if ["1.8.7", "1.9.1"].include? RUBY_VERSION
9
+ require 'output_safety_ext'
10
+ else
11
+ puts "** MonkeySupport: output_safety module was not designed to work with #{RUBY_VERSION}. Extension not loaded."
12
+ end
@@ -0,0 +1,54 @@
1
+ require 'pp'
2
+
3
+ class LRUCache
4
+
5
+ def initialize(size=200, cleanup_threshold=size*2)
6
+ @size = size
7
+ @cleanup_threshold = cleanup_threshold
8
+
9
+ @cache = {}
10
+
11
+ @access = [0]*size
12
+
13
+ @counter = 0
14
+ end
15
+
16
+ def []=(key, value)
17
+ @cache[key] = [Time.now, value]
18
+ cleanup if @cache.size > @cleanup_threshold
19
+ end
20
+
21
+ def [](key)
22
+ # @cache[key][0] = @count
23
+ @cache[key]
24
+ end
25
+
26
+ def cleanup
27
+ num_delete = @cache.size - @size
28
+ @cache.sort_by{|k,v|v[0]}[0...num_delete].each do |arr|
29
+ @cache.delete(arr[0])
30
+ end
31
+ end
32
+
33
+ def size
34
+ @cache.size
35
+ end
36
+
37
+ end
38
+
39
+ if __FILE__ == $0
40
+ c = LRUCache.new(2)
41
+ c[1] = :a
42
+ puts c.size
43
+ c[2] = :b
44
+ puts c.size
45
+ c[3] = :c
46
+ puts c.size
47
+ c[4] = :d
48
+ puts c.size
49
+ c[5] = :e
50
+ puts c.size
51
+ c[6] = :f
52
+ puts c.size
53
+
54
+ end
@@ -1,6 +1,6 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
@@ -9,10 +9,10 @@ Gem::Specification.new do |s|
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Burke Libbey"]
12
- s.date = %q{2009-09-04}
13
- s.description = %q{MonkeySupport provides C implementations for some of the more intensive string manipulation methods in activesupport. ActionView is up next.}
12
+ s.date = %q{2009-12-28}
13
+ s.description = %q{MonkeySupport provides C implementations for some of the more intensive string manipulation methods in activesupport.}
14
14
  s.email = %q{burke@burkelibbey.org}
15
- s.extensions = ["ext/monkeysupport_c/extconf.rb"]
15
+ s.extensions = ["ext/monkeysupport_c/extconf.rb", "ext/output_safety_ext/extconf.rb"]
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE",
18
18
  "README.rdoc"
@@ -25,31 +25,27 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "ext/monkeysupport_c/extconf.rb",
28
- "ext/monkeysupport_c/extconf.rb",
29
- "ext/monkeysupport_c/src/activesupport_inflector.c",
30
28
  "ext/monkeysupport_c/src/activesupport_inflector.c",
31
29
  "ext/monkeysupport_c/src/activesupport_inflector.h",
32
- "ext/monkeysupport_c/src/activesupport_inflector.h",
33
30
  "ext/monkeysupport_c/src/monkeysupport_c.c",
34
- "ext/monkeysupport_c/src/monkeysupport_c.c",
35
- "lib/monkeysupport.rb",
31
+ "ext/output_safety_ext/Makefile",
32
+ "ext/output_safety_ext/extconf.rb",
33
+ "ext/output_safety_ext/output_safety_ext.bundle",
34
+ "ext/output_safety_ext/output_safety_ext.c",
35
+ "ext/output_safety_ext/output_safety_ext.o",
36
36
  "lib/monkeysupport.rb",
37
37
  "lib/monkeysupport/activesupport/inflector.rb",
38
- "lib/monkeysupport/activesupport/inflector.rb",
39
- "lib/monkeysupport/c_proxy.rb",
40
38
  "lib/monkeysupport/c_proxy.rb",
41
- "lib/monkeysupport/memoizable.rb",
39
+ "lib/monkeysupport/lru_cache.rb",
42
40
  "lib/monkeysupport/memoizable.rb",
43
41
  "monkeysupport.gemspec",
44
42
  "test/monkeysupport_test.rb",
45
- "test/monkeysupport_test.rb",
46
- "test/test_helper.rb",
47
43
  "test/test_helper.rb"
48
44
  ]
49
45
  s.homepage = %q{http://github.com/burke/monkeysupport}
50
46
  s.rdoc_options = ["--charset=UTF-8"]
51
47
  s.require_paths = ["lib"]
52
- s.rubygems_version = %q{1.3.4}
48
+ s.rubygems_version = %q{1.3.5}
53
49
  s.summary = %q{Monkeypatching Rails with C since 2009}
54
50
  s.test_files = [
55
51
  "test/monkeysupport_test.rb",
@@ -61,11 +57,12 @@ Gem::Specification.new do |s|
61
57
  s.specification_version = 3
62
58
 
63
59
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
64
- s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
65
61
  else
66
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
62
+ s.add_dependency(%q<shoulda>, [">= 0"])
67
63
  end
68
64
  else
69
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
65
+ s.add_dependency(%q<shoulda>, [">= 0"])
70
66
  end
71
67
  end
68
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monkeysupport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Burke Libbey
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-04 00:00:00 -05:00
12
+ date: 2009-12-28 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: thoughtbot-shoulda
16
+ name: shoulda
17
17
  type: :development
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
@@ -22,12 +22,13 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
- description: MonkeySupport provides C implementations for some of the more intensive string manipulation methods in activesupport. ActionView is up next.
25
+ description: MonkeySupport provides C implementations for some of the more intensive string manipulation methods in activesupport.
26
26
  email: burke@burkelibbey.org
27
27
  executables: []
28
28
 
29
29
  extensions:
30
30
  - ext/monkeysupport_c/extconf.rb
31
+ - ext/output_safety_ext/extconf.rb
31
32
  extra_rdoc_files:
32
33
  - LICENSE
33
34
  - README.rdoc
@@ -42,9 +43,15 @@ files:
42
43
  - ext/monkeysupport_c/src/activesupport_inflector.c
43
44
  - ext/monkeysupport_c/src/activesupport_inflector.h
44
45
  - ext/monkeysupport_c/src/monkeysupport_c.c
46
+ - ext/output_safety_ext/Makefile
47
+ - ext/output_safety_ext/extconf.rb
48
+ - ext/output_safety_ext/output_safety_ext.bundle
49
+ - ext/output_safety_ext/output_safety_ext.c
50
+ - ext/output_safety_ext/output_safety_ext.o
45
51
  - lib/monkeysupport.rb
46
52
  - lib/monkeysupport/activesupport/inflector.rb
47
53
  - lib/monkeysupport/c_proxy.rb
54
+ - lib/monkeysupport/lru_cache.rb
48
55
  - lib/monkeysupport/memoizable.rb
49
56
  - monkeysupport.gemspec
50
57
  - test/monkeysupport_test.rb