monkeysupport 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ #MonkeySupport#
2
+
3
+ MonkeySupport is a collection of monkeypatches to Rails, replacing
4
+ existing methods with (somewhat) optimized C equivalents.
5
+
6
+ If this interests you, you should also check out methodmissing's
7
+ excellent HashWithIndifferentAccess at
8
+ http://github.com/methodmissing/hwia .
9
+
10
+ ##Modules##
11
+
12
+ MonkeySupport is largely a bunch of modules, each overwriting a part
13
+ of ActiveSupport. By setting `$MonkeyModuleExcludes` to an array of
14
+ module names before loading MonkeySupport, those modules will not be
15
+ loaded. For example:
16
+
17
+ $MonkeyModuleExcludes = ["inflector", "output_safety"]
18
+ require 'monkeysupport'
19
+
20
+ You can also specify a whitelist of modules to load with
21
+ `$MonkeyModuleIncludes`, if you'd like. Typically, leaving both these
22
+ variables undefined is what you'll want, though.
23
+
24
+ MonkeySupport is currently comprised of the following modules:
25
+
26
+ * inflector
27
+ * output_safety
28
+
29
+ ##Note on Patches/Pull Requests##
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but
37
+ bump version in a commit by itself I can ignore when I pull)
38
+ * Send me a pull request. Bonus points for topic branches.
39
+
40
+ ##Testing##
41
+
42
+ There are some simple specs in `./spec`. To run against the
43
+ activesupport test suite, add `require 'monkeysupport'` below the
44
+ activesupport requires in activesupport's `Rakefile`. and rake away.
45
+
46
+ ##Problems / TODO##
47
+
48
+ * Certain functions used to be memoized, then rails added test cases
49
+ for adding new inflections on the fly. The memoization is currently
50
+ disabled, but could be tweaked to be invalidated when the inflection
51
+ rules change.
52
+ * Need to do a better job of identifying ruby versions for
53
+ output_safety module.
54
+ * There's always more to port...
55
+
56
+ ##Copyright##
57
+
58
+ Copyright (c) 2009 Burke Libbey. MIT License. See LICENSE for details.
data/Rakefile CHANGED
@@ -6,30 +6,23 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "monkeysupport"
8
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."
9
+ gem.description = "MonkeySupport monkeypatches some of the performance-sink parts of rails with speedy C extensions."
10
10
  gem.email = "burke@burkelibbey.org"
11
11
  gem.homepage = "http://github.com/burke/monkeysupport"
12
12
  gem.authors = ["Burke Libbey"]
13
- gem.files.include '{test,lib,ext}/**/*'
14
- gem.extensions = ["ext/monkeysupport_c/extconf.rb", "ext/output_safety_ext/extconf.rb"]
13
+ gem.files.include '{spec,lib,ext}/**/*'
14
+ gem.extensions = ["ext/monkeysupport_c/extconf.rb", "ext/monkeysupport_output_safety/extconf.rb"]
15
15
  gem.add_development_dependency "shoulda"
16
16
  end
17
17
  rescue LoadError
18
18
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
19
  end
20
20
 
21
- require 'rake/testtask'
22
- Rake::TestTask.new(:test) do |test|
23
- test.libs << 'lib' << 'test'
24
- test.pattern = 'test/**/*_test.rb'
25
- test.verbose = true
26
- end
27
-
28
21
  begin
29
22
  require 'rcov/rcovtask'
30
23
  Rcov::RcovTask.new do |test|
31
- test.libs << 'test'
32
- test.pattern = 'test/**/*_test.rb'
24
+ test.libs << 'spec'
25
+ test.pattern = 'spec/**/*_spec.rb'
33
26
  test.verbose = true
34
27
  end
35
28
  rescue LoadError
@@ -38,10 +31,6 @@ rescue LoadError
38
31
  end
39
32
  end
40
33
 
41
- task :test => :check_dependencies
42
-
43
- task :default => :test
44
-
45
34
  require 'rake/rdoctask'
46
35
  Rake::RDocTask.new do |rdoc|
47
36
  if File.exist?('VERSION')
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 1.0.0
@@ -8,7 +8,6 @@ Init_monkeysupport_c()
8
8
  VALUE mMonkeySupport = rb_define_module("MonkeySupport");
9
9
  VALUE cMSC = rb_define_class_under(mMonkeySupport, "C", rb_cObject);
10
10
 
11
- /* ActiveSupport::ASC.camelize("my_string") */
12
11
  rb_define_singleton_method(cMSC, "activesupport_inflector_camelize", activesupport_inflector_camelize, 2);
13
12
  rb_define_singleton_method(cMSC, "activesupport_inflector_demodulize", activesupport_inflector_demodulize, 1);
14
13
  rb_define_singleton_method(cMSC, "activesupport_inflector_dasherize", activesupport_inflector_dasherize, 1);
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = 'monkeysupport_output_safety'
4
+ dir_config(extension_name)
5
+ create_makefile(extension_name, 'src')
@@ -1,13 +1,11 @@
1
- #include "stdbool.h"
1
+ #include <stdbool.h>
2
2
 
3
3
  #include "ruby.h"
4
- #include "ruby/version.h"
5
4
 
6
5
  // Use a FL_USER flag for storing html safety information.
7
6
  // fingers crossed here, this is black black magic.
8
- #ifdef FL_USER4
9
7
 
10
- #define HFLAG FL_USER4
8
+ #define HFLAG FL_USER7
11
9
 
12
10
  // by default, objects are not safe for html.
13
11
  VALUE obj_html_safe_qmark(VALUE self) { return Qfalse; }
@@ -71,12 +69,14 @@ str_concat(VALUE self, VALUE other)
71
69
  return self;
72
70
  }
73
71
 
72
+
74
73
  void
75
- Init_output_safety_ext()
74
+ Init_monkeysupport_output_safety()
76
75
  {
76
+
77
77
  rb_define_method(rb_cObject, "html_safe?", obj_html_safe_qmark, 0);
78
78
  rb_define_method(rb_cFixnum, "html_safe?", fix_html_safe_qmark, 0);
79
-
79
+
80
80
  rb_define_method(rb_cString, "html_safe", str_html_safe, 0);
81
81
  rb_define_method(rb_cString, "html_safe!", str_html_safe_bang, 0);
82
82
  rb_define_method(rb_cString, "html_safe?", str_html_safe_qmark, 0);
@@ -84,6 +84,5 @@ Init_output_safety_ext()
84
84
 
85
85
  rb_define_method(rb_cString, "+", str_plus, 1);
86
86
  rb_define_method(rb_cString, "<<", str_concat, 1);
87
- }
88
87
 
89
- #endif
88
+ }
@@ -0,0 +1,91 @@
1
+ #include "ruby.h"
2
+
3
+ // Use a FL_USER flag for storing html safety information.
4
+ // fingers crossed here, this is black black magic.
5
+ //#ifdef FL_USER4
6
+
7
+ /*
8
+
9
+ //#define HFLAG FL_USER4
10
+
11
+ // by default, objects are not safe for html.
12
+ VALUE obj_html_safe_qmark(VALUE self) { return Qfalse; }
13
+
14
+ //fixnums are always safe for html.
15
+ VALUE fix_html_safe_qmark(VALUE self) { return Qtrue; }
16
+
17
+ VALUE // mark a string as safe for html.
18
+ str_html_safe_bang(VALUE self)
19
+ {
20
+ FL_SET(self, HFLAG);
21
+ return self;
22
+ }
23
+
24
+ VALUE // is this string safe for html?
25
+ str_html_safe_qmark(VALUE self)
26
+ {
27
+ return FL_TEST(self, HFLAG) ? Qtrue : Qfalse;
28
+ }
29
+
30
+ void
31
+ set_html_safe(VALUE obj, bool val)
32
+ {
33
+ if (val) {
34
+ FL_SET(obj, HFLAG);
35
+ } else {
36
+ FL_UNSET(obj, HFLAG);
37
+ }
38
+ }
39
+
40
+
41
+ VALUE // return a copy of this string, marked as safe for html.
42
+ str_html_safe(VALUE self)
43
+ {
44
+ VALUE other = rb_obj_dup(self);
45
+ return str_html_safe_bang(other);
46
+ }
47
+
48
+ VALUE
49
+ str_plus(VALUE self, VALUE other)
50
+ {
51
+ VALUE ret;
52
+ bool safe = ((str_html_safe_qmark(self) == Qtrue) &&
53
+ (str_html_safe_qmark(other) == Qtrue));
54
+
55
+ ret = rb_str_plus(self, other);
56
+
57
+ set_html_safe(ret, safe);
58
+ return ret;
59
+ }
60
+
61
+ VALUE
62
+ str_concat(VALUE self, VALUE other)
63
+ {
64
+ bool safe = ((str_html_safe_qmark(self) == Qtrue) &&
65
+ (str_html_safe_qmark(other) == Qtrue));
66
+
67
+ rb_str_concat(self, other);
68
+
69
+ set_html_safe(self, safe);
70
+ return self;
71
+ }
72
+
73
+ */
74
+
75
+ void
76
+ Init_monkeysupport_output_safety()
77
+ {
78
+
79
+ /* rb_define_method(rb_cObject, "html_safe?", obj_html_safe_qmark, 0); */
80
+ /* rb_define_method(rb_cFixnum, "html_safe?", fix_html_safe_qmark, 0); */
81
+
82
+ /* rb_define_method(rb_cString, "html_safe", str_html_safe, 0); */
83
+ /* rb_define_method(rb_cString, "html_safe!", str_html_safe_bang, 0); */
84
+ /* rb_define_method(rb_cString, "html_safe?", str_html_safe_qmark, 0); */
85
+ /* rb_define_method(rb_cString, "_rails_html_safe", str_html_safe_qmark, 0); */
86
+
87
+ /* rb_define_method(rb_cString, "+", str_plus, 1); */
88
+ /* rb_define_method(rb_cString, "<<", str_concat, 1); */
89
+ }
90
+
91
+ //#endif
data/lib/monkeysupport.rb CHANGED
@@ -1,12 +1,30 @@
1
- require 'monkeysupport_c'
2
-
3
1
  require 'monkeysupport/c_proxy'
4
2
  require 'monkeysupport/memoizable'
5
3
 
6
- require 'monkeysupport/activesupport/inflector'
4
+ require 'monkeysupport_c'
5
+
6
+ module_loaders = {
7
+ "inflector" => lambda{
8
+ require 'monkeysupport/activesupport/inflector'
9
+ },
10
+
11
+ "output_safety" => lambda{
12
+ if ["1.8.7", "1.9.1"].include? RUBY_VERSION
13
+ require 'monkeysupport_output_safety'
14
+ else
15
+ puts "##MonkeySupport: output_safety module not supported by #{RUBY_VERSION}. Module not loaded."
16
+ end
17
+ }
18
+ }
19
+
20
+ modules = module_loaders.keys
21
+ if $MonkeyModuleIncludes
22
+ modules &= $MonkeyModuleIncludes # intersection
23
+ end
24
+ if $MonkeyModuleExcludes
25
+ modules -= $MonkeyModuleExcludes # difference
26
+ end
7
27
 
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."
28
+ modules.each do |mdl|
29
+ module_loaders[mdl].call
12
30
  end
@@ -3,8 +3,9 @@ module ActiveSupport
3
3
 
4
4
  extend MonkeySupport::Memoizable
5
5
  extend MonkeySupport::CProxy
6
-
7
- monkey_memoize :pluralize, :singularize, :humanize
6
+
7
+ # This breaks a test case in rails now :(
8
+ # monkey_memoize :pluralize, :singularize, :humanize
8
9
 
9
10
  monkey_c_proxy(:camelize,
10
11
  :activesupport_inflector_camelize,
@@ -5,42 +5,41 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{monkeysupport}
8
- s.version = "0.1.2"
8
+ s.version = "1.0.0"
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
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.}
13
+ s.description = %q{MonkeySupport monkeypatches some of the performance-sink parts of rails with speedy C extensions.}
14
14
  s.email = %q{burke@burkelibbey.org}
15
- s.extensions = ["ext/monkeysupport_c/extconf.rb", "ext/output_safety_ext/extconf.rb"]
15
+ s.extensions = ["ext/monkeysupport_c/extconf.rb", "ext/monkeysupport_output_safety/extconf.rb"]
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE",
18
- "README.rdoc"
18
+ "README.md"
19
19
  ]
20
20
  s.files = [
21
21
  ".document",
22
22
  ".gitignore",
23
23
  "LICENSE",
24
- "README.rdoc",
24
+ "README.md",
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "ext/monkeysupport_c/extconf.rb",
28
28
  "ext/monkeysupport_c/src/activesupport_inflector.c",
29
29
  "ext/monkeysupport_c/src/activesupport_inflector.h",
30
30
  "ext/monkeysupport_c/src/monkeysupport_c.c",
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",
31
+ "ext/monkeysupport_output_safety/Makefile",
32
+ "ext/monkeysupport_output_safety/extconf.rb",
33
+ "ext/monkeysupport_output_safety/monkeysupport_output_safety.c",
34
+ "ext/monkeysupport_output_safety/src/monkeysupport_output_safety.c",
36
35
  "lib/monkeysupport.rb",
37
36
  "lib/monkeysupport/activesupport/inflector.rb",
38
37
  "lib/monkeysupport/c_proxy.rb",
39
38
  "lib/monkeysupport/lru_cache.rb",
40
39
  "lib/monkeysupport/memoizable.rb",
41
40
  "monkeysupport.gemspec",
42
- "test/monkeysupport_test.rb",
43
- "test/test_helper.rb"
41
+ "spec/output_safety_spec.rb",
42
+ "spec/spec_helper.rb"
44
43
  ]
45
44
  s.homepage = %q{http://github.com/burke/monkeysupport}
46
45
  s.rdoc_options = ["--charset=UTF-8"]
@@ -48,8 +47,8 @@ Gem::Specification.new do |s|
48
47
  s.rubygems_version = %q{1.3.5}
49
48
  s.summary = %q{Monkeypatching Rails with C since 2009}
50
49
  s.test_files = [
51
- "test/monkeysupport_test.rb",
52
- "test/test_helper.rb"
50
+ "spec/output_safety_spec.rb",
51
+ "spec/spec_helper.rb"
53
52
  ]
54
53
 
55
54
  if s.respond_to? :specification_version then
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe String do
4
+ context "newly created" do
5
+ it "should not be html_safe" do
6
+ "foo bar baz".html_safe?.should be_false
7
+ end
8
+ end
9
+
10
+ context "not marked as html_safe" do
11
+ it "should be html_safe once marked as html_safe" do
12
+ "foo".html_safe.html_safe?.should be_true
13
+ end
14
+ it "should be equal to the same html_safe string" do
15
+ "foo".html_safe.should ==("foo")
16
+ end
17
+ it "should return the same string after calling html_safe!" do
18
+ foo = "bar"
19
+ bar = foo.html_safe!
20
+ foo.should equal(bar)
21
+ end
22
+ it "should be modified when html_safe! is called" do
23
+ foo = "bar"
24
+ foo.html_safe!
25
+ foo.html_safe?.should be_true
26
+ end
27
+ end
28
+
29
+ context "marked as html_safe" do
30
+ it "should become html_unsafe when an unsafe string is concatenated to it" do
31
+ foo = "bar".html_safe
32
+ foo << "baz"
33
+ foo.html_safe?.should be_false
34
+ end
35
+ it "should remain html_safe when a safe string is concatenated to it" do
36
+ foo = "bar".html_safe
37
+ foo << "baz".html_safe
38
+ foo.html_safe?.should be_true
39
+ end
40
+ it "should return an unsafe string when added to an unsafe string" do
41
+ ("foo".html_safe + "bar").html_safe?.should be_false
42
+ end
43
+ it "should return safe string when added to a safe string" do
44
+ ("foo".html_safe + "bar".html_safe).html_safe?.should be_true
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_support'
2
+ require File.join(File.dirname(__FILE__),'../lib/monkeysupport')
3
+
4
+ require 'spec/autorun'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ #Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ Spec::Runner.configure do |config|
11
+ # config.use_transactional_fixtures = true
12
+ # config.use_instantiated_fixtures = false
13
+ # config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
14
+
15
+
16
+ end
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.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Burke Libbey
@@ -22,40 +22,39 @@ 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.
25
+ description: MonkeySupport monkeypatches some of the performance-sink parts of rails with speedy C extensions.
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
+ - ext/monkeysupport_output_safety/extconf.rb
32
32
  extra_rdoc_files:
33
33
  - LICENSE
34
- - README.rdoc
34
+ - README.md
35
35
  files:
36
36
  - .document
37
37
  - .gitignore
38
38
  - LICENSE
39
- - README.rdoc
39
+ - README.md
40
40
  - Rakefile
41
41
  - VERSION
42
42
  - ext/monkeysupport_c/extconf.rb
43
43
  - ext/monkeysupport_c/src/activesupport_inflector.c
44
44
  - ext/monkeysupport_c/src/activesupport_inflector.h
45
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
46
+ - ext/monkeysupport_output_safety/Makefile
47
+ - ext/monkeysupport_output_safety/extconf.rb
48
+ - ext/monkeysupport_output_safety/monkeysupport_output_safety.c
49
+ - ext/monkeysupport_output_safety/src/monkeysupport_output_safety.c
51
50
  - lib/monkeysupport.rb
52
51
  - lib/monkeysupport/activesupport/inflector.rb
53
52
  - lib/monkeysupport/c_proxy.rb
54
53
  - lib/monkeysupport/lru_cache.rb
55
54
  - lib/monkeysupport/memoizable.rb
56
55
  - monkeysupport.gemspec
57
- - test/monkeysupport_test.rb
58
- - test/test_helper.rb
56
+ - spec/output_safety_spec.rb
57
+ - spec/spec_helper.rb
59
58
  has_rdoc: true
60
59
  homepage: http://github.com/burke/monkeysupport
61
60
  licenses: []
@@ -85,5 +84,5 @@ signing_key:
85
84
  specification_version: 3
86
85
  summary: Monkeypatching Rails with C since 2009
87
86
  test_files:
88
- - test/monkeysupport_test.rb
89
- - test/test_helper.rb
87
+ - spec/output_safety_spec.rb
88
+ - spec/spec_helper.rb
data/README.rdoc DELETED
@@ -1,34 +0,0 @@
1
- = MonkeySupport
2
-
3
- MonkeySupport is a collection of monkeypatches to Rails, replacing
4
- existing methods with (somewhat) optimized C equivalents.
5
-
6
- If this interests you, you should also check out methodmissing's
7
- excellent HashWithIndifferentAccess at
8
- http://github.com/methodmissing/hwia .
9
-
10
- == Note on Patches/Pull Requests
11
-
12
- * Fork the project.
13
- * Make your feature addition or bug fix.
14
- * Add tests for it. This is important so I don't break it in a
15
- future version unintentionally.
16
- * Commit, do not mess with rakefile, version, or history.
17
- (if you want to have your own version, that is fine but
18
- bump version in a commit by itself I can ignore when I pull)
19
- * Send me a pull request. Bonus points for topic branches.
20
-
21
- == Problems / TODO
22
-
23
- * I haven't figured out a simple way to get this running with the
24
- rails test suite. That would be handy.
25
- * Certain functions are memoized now, that rails doesn't memoize by
26
- default. I've tried to choose carefully, but with unusual usage
27
- patterns, this could cause memory issues. Simplest solution that
28
- comes to my mind would be to use a simple LRU cache instead of a
29
- plain hash.
30
- * There's always more to port...
31
-
32
- == Copyright
33
-
34
- Copyright (c) 2009 Burke Libbey. See LICENSE for details.
@@ -1,5 +0,0 @@
1
- require 'mkmf'
2
-
3
- extension_name = 'output_safety_ext'
4
- dir_config(extension_name)
5
- create_makefile(extension_name, '.')
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class MonkeysupportTest < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end
data/test/test_helper.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
4
-
5
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
- $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'monkeysupport'
8
-
9
- class Test::Unit::TestCase
10
- end