vex 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. data/Manifest +112 -0
  2. data/Rakefile +53 -0
  3. data/VERSION +1 -0
  4. data/config/README +2 -0
  5. data/config/dependencies.rb +10 -0
  6. data/config/gem.yml +7 -0
  7. data/init.rb +36 -0
  8. data/lib/nokogiri/nokogiri_ext.rb +96 -0
  9. data/lib/vex.rb +5 -0
  10. data/lib/vex/action_controller.rb +4 -0
  11. data/lib/vex/action_controller/verify_action.rb +97 -0
  12. data/lib/vex/action_controller/whitelisted_actions.rb +45 -0
  13. data/lib/vex/active_record.rb +3 -0
  14. data/lib/vex/active_record/__init__.rb +0 -0
  15. data/lib/vex/active_record/advisory_lock.rb +11 -0
  16. data/lib/vex/active_record/advisory_lock/mysql_adapter.rb +16 -0
  17. data/lib/vex/active_record/advisory_lock/sqlite_adapter.rb +78 -0
  18. data/lib/vex/active_record/belongs_to_many.rb +143 -0
  19. data/lib/vex/active_record/find_by_extension.rb +70 -0
  20. data/lib/vex/active_record/gem.rb +8 -0
  21. data/lib/vex/active_record/lite_table.rb +139 -0
  22. data/lib/vex/active_record/lite_view.rb +140 -0
  23. data/lib/vex/active_record/mass_load.rb +65 -0
  24. data/lib/vex/active_record/mysql_backup.rb +21 -0
  25. data/lib/vex/active_record/plugins/default_value_for/LICENSE.TXT +19 -0
  26. data/lib/vex/active_record/plugins/default_value_for/README.rdoc +421 -0
  27. data/lib/vex/active_record/plugins/default_value_for/Rakefile +6 -0
  28. data/lib/vex/active_record/plugins/default_value_for/init.rb +91 -0
  29. data/lib/vex/active_record/plugins/default_value_for/test.rb +279 -0
  30. data/lib/vex/active_record/plugins/default_value_for/test.sqlite3 +0 -0
  31. data/lib/vex/active_record/random_id.rb +56 -0
  32. data/lib/vex/active_record/resolver.rb +49 -0
  33. data/lib/vex/active_record/serialize_hash.rb +125 -0
  34. data/lib/vex/active_record/to_html.rb +53 -0
  35. data/lib/vex/active_record/validate.rb +76 -0
  36. data/lib/vex/active_record/validation_error_ext.rb +68 -0
  37. data/lib/vex/base.rb +2 -0
  38. data/lib/vex/base/app.rb +75 -0
  39. data/lib/vex/base/array/at_random.rb +17 -0
  40. data/lib/vex/base/array/cross.rb +26 -0
  41. data/lib/vex/base/array/each_batch.rb +32 -0
  42. data/lib/vex/base/array/parallel_map.rb +98 -0
  43. data/lib/vex/base/deprecation.rb +41 -0
  44. data/lib/vex/base/enumerable/deep.rb +95 -0
  45. data/lib/vex/base/enumerable/enumerable_ext.rb +59 -0
  46. data/lib/vex/base/enumerable/progress.rb +71 -0
  47. data/lib/vex/base/filesystem/fast_copy.rb +61 -0
  48. data/lib/vex/base/filesystem/grep.rb +34 -0
  49. data/lib/vex/base/filesystem/lock.rb +43 -0
  50. data/lib/vex/base/filesystem/lock.rb.test.lck +0 -0
  51. data/lib/vex/base/filesystem/lock.rb.test.pid +1 -0
  52. data/lib/vex/base/filesystem/make_dirs.rb +94 -0
  53. data/lib/vex/base/filesystem/parse_filename.rb +36 -0
  54. data/lib/vex/base/filesystem/tmp_file.rb +87 -0
  55. data/lib/vex/base/filesystem/write.rb +43 -0
  56. data/lib/vex/base/hash/compact.rb +38 -0
  57. data/lib/vex/base/hash/cross.rb +117 -0
  58. data/lib/vex/base/hash/easy_access.rb +141 -0
  59. data/lib/vex/base/hash/ensure_keys.rb +18 -0
  60. data/lib/vex/base/hash/extract.rb +71 -0
  61. data/lib/vex/base/hash/extras.rb +62 -0
  62. data/lib/vex/base/hash/inspect.rb +17 -0
  63. data/lib/vex/base/hash/simple_access_methods.rb +74 -0
  64. data/lib/vex/base/invalid_argument/invalid_argument.rb +97 -0
  65. data/lib/vex/base/local_conf.rb +35 -0
  66. data/lib/vex/base/net/http_ext.rb +227 -0
  67. data/lib/vex/base/net/socket_ext.rb +43 -0
  68. data/lib/vex/base/object/insp.rb +123 -0
  69. data/lib/vex/base/object/multiple_attributes.rb +58 -0
  70. data/lib/vex/base/object/singleton_methods.rb +23 -0
  71. data/lib/vex/base/object/with_benchmark.rb +110 -0
  72. data/lib/vex/base/range_array.rb +40 -0
  73. data/lib/vex/base/range_ext.rb +28 -0
  74. data/lib/vex/base/safe_token.rb +156 -0
  75. data/lib/vex/base/string/string_ext.rb +136 -0
  76. data/lib/vex/base/thread/deferred.rb +52 -0
  77. data/lib/vex/base/thread/sleep.rb +11 -0
  78. data/lib/vex/base/time/date_ext.rb +12 -0
  79. data/lib/vex/boot.rb +40 -0
  80. data/lib/vex/boot/array.rb +22 -0
  81. data/lib/vex/boot/blank.rb +41 -0
  82. data/lib/vex/boot/string.rb +60 -0
  83. data/migration/create_request_log.rb +28 -0
  84. data/r.rb +35 -0
  85. data/script/console +19 -0
  86. data/script/rebuild +7 -0
  87. data/tasks/echoe.rake +52 -0
  88. data/tasks/validate_db.rake +14 -0
  89. data/test/ar.rb +30 -0
  90. data/test/auto.rb +31 -0
  91. data/test/base-tests/local_conf.rb +25 -0
  92. data/test/base.rb +2 -0
  93. data/test/boot.rb +3 -0
  94. data/test/config/local.defaults.yml +4 -0
  95. data/test/config/local.yml +8 -0
  96. data/test/test.sqlite3 +0 -0
  97. data/test/test.sqlite3.Class#create.lck +0 -0
  98. data/test/test.sqlite3.Class#create.lck.lck +0 -0
  99. data/test/test.sqlite3.Class#create.lck.pid +1 -0
  100. data/test/test.sqlite3.Class#create.pid +1 -0
  101. data/test/test.sqlite3.LiteView.make.holders__view_dummy.lck +0 -0
  102. data/test/test.sqlite3.LiteView.make.holders__view_dummy.lck.lck +0 -0
  103. data/test/test.sqlite3.LiteView.make.holders__view_dummy.lck.pid +1 -0
  104. data/test/test.sqlite3.LiteView.make.holders__view_dummy.pid +1 -0
  105. data/test/test.sqlite3.vex.lck +0 -0
  106. data/test/test_helper.rb +49 -0
  107. data/test/tmp/copy.dat +1 -0
  108. data/test/tmp/lock.sqlite3 +0 -0
  109. data/test/tmp/lock.sqlite3.etest.lck +0 -0
  110. data/test/tmp/lock.sqlite3.etest.pid +1 -0
  111. data/test/tmp/somedata.dat +61 -0
  112. data/vex.gemspec +49 -0
  113. data/vex.tmproj +186 -0
  114. metadata +305 -0
@@ -0,0 +1,52 @@
1
+ class Thread
2
+ def self.uid
3
+ "#{$$}.#{Thread.current.object_id}"
4
+ end
5
+
6
+ def self.deferred(&block)
7
+ new {
8
+ Thread.current.abort_on_exception = true
9
+
10
+ begin
11
+ yield
12
+ rescue
13
+ App.logger.warn "Caught exception in background processing: #{$!}"
14
+ end
15
+ }
16
+ end
17
+ end
18
+
19
+ module Thread::Etest
20
+ def test_deferred
21
+ i = 0
22
+ Thread.deferred do
23
+ i = 1
24
+ end
25
+
26
+ Thread.sleep 0.05
27
+ assert_equal(1, i)
28
+ end
29
+
30
+ def test_deferred_exception
31
+ i = 0
32
+ Thread.deferred do
33
+ i = 1
34
+ raise
35
+ i = 2
36
+ end
37
+
38
+ Thread.sleep 0.05
39
+ assert_equal(1, i)
40
+ end
41
+
42
+ def test_pids
43
+ pids = [ Thread.uid ]
44
+
45
+ Thread.deferred { pids[1] = Thread.uid }
46
+ Thread.deferred { pids[2] = Thread.uid }
47
+
48
+ Thread.sleep 0.05
49
+ assert_equal(pids, pids.compact)
50
+ assert_equal(pids, pids.uniq)
51
+ end
52
+ end if VEX_TEST == "base"
@@ -0,0 +1,11 @@
1
+ class Thread
2
+ (class << self; self; end).class_eval do
3
+ public :sleep
4
+ end
5
+ end
6
+
7
+ module Thread::Etest
8
+ def test_sleep
9
+ Thread.sleep 0.001
10
+ end
11
+ end if VEX_TEST == "base"
@@ -0,0 +1,12 @@
1
+ class Date
2
+ def first_of_month
3
+ self - self.day + 1
4
+ end
5
+ end
6
+
7
+ module Date::Etest
8
+ def test_first_of_month
9
+ date = Date.parse("2010/02/28")
10
+ assert_equal Date.parse("2010/02/01"), date.first_of_month
11
+ end
12
+ end if VEX_TEST == "base"
data/lib/vex/boot.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Vex
2
+ ROOT=File.expand_path "#{File.dirname(__FILE__)}/../vex"
3
+
4
+ def self.version
5
+ @version ||= File.read("#{ROOT}/../../VERSION")
6
+ end
7
+
8
+ #
9
+ # load all modules from a specific directory.
10
+ # This loads first all files in or under that directory,
11
+ # sorted alphabetically. Hint: use files __init__.rb
12
+ # for stuff that must be loaded first.
13
+ def self.load_directory(directory)
14
+ # load plugins first
15
+ plugin_dir = "#{ROOT}/#{directory}/plugins"
16
+ Dir.glob("#{plugin_dir}/*").each do |file|
17
+ load_plugin file if File.directory?(file)
18
+ end
19
+
20
+ (Dir.glob("#{ROOT}/#{directory}/**/*.rb") - [__FILE__]).sort.each do |file|
21
+ next if file[0, plugin_dir.length] == plugin_dir
22
+ load file
23
+ end
24
+ end
25
+
26
+ def self.load_plugin(directory)
27
+ $:.push(directory)
28
+ init_rb = "#{directory}/init.rb"
29
+ require(init_rb) if File.exists?(init_rb)
30
+ end
31
+ end
32
+
33
+ Vex.load_directory "../../config"
34
+ Vex.load_directory "boot"
35
+
36
+ module Vex::Etest
37
+ def test_version
38
+ assert_not_nil(Vex.version)
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ class Array
2
+ def extract_options!
3
+ if last.is_a?(Hash)
4
+ pop
5
+ else
6
+ {}
7
+ end
8
+ end
9
+ end
10
+
11
+ module Array::Etest
12
+ def test_extract_options
13
+ arr = %w(1 2)
14
+ assert_equal({}, arr.extract_options!)
15
+ assert_equal(%w(1 2), arr)
16
+
17
+ arr = [ 1, 2, { :a => :b }]
18
+ assert_equal({ :a => :b }, arr.extract_options!)
19
+ assert_equal( [ 1, 2 ], arr)
20
+ end
21
+ end if VEX_TEST == "boot"
22
+
@@ -0,0 +1,41 @@
1
+ class Object
2
+ def blank?
3
+ false
4
+ end
5
+ end
6
+
7
+ class NilClass
8
+ def blank?
9
+ true
10
+ end
11
+ end
12
+
13
+ class FalseClass
14
+ def blank?
15
+ true
16
+ end
17
+ end
18
+
19
+ module Enumerable
20
+ def blank?
21
+ empty?
22
+ end
23
+ end
24
+
25
+ class String
26
+ alias :blank? :empty?
27
+ end
28
+
29
+ module Blank
30
+ module Etest
31
+ def test_blanks
32
+ assert_equal true, nil.blank?
33
+ assert_equal true, [].blank?
34
+ assert_equal true, false.blank?
35
+ assert_equal true, {}.blank?
36
+ assert_equal true, "".blank?
37
+
38
+ assert_equal false, 1.blank?
39
+ end
40
+ end
41
+ end if VEX_TEST == "boot"
@@ -0,0 +1,60 @@
1
+ class String
2
+ def starts_with?(other)
3
+ return false if other.length > length
4
+
5
+ self[0, other.length] == other
6
+ end
7
+
8
+ def ends_with?(other)
9
+ return false if other.length > length
10
+
11
+ self[self.length-other.length..-1] == other
12
+ end
13
+
14
+ def constantize
15
+ names = self.split('::')
16
+ raise ArgumentError, "Cannot be blank" if blank?
17
+
18
+ names.shift if names.first.empty?
19
+
20
+ names.inject(Object) do |constant, name|
21
+ constant.const_get(name) || constant.const_missing(name)
22
+ end
23
+ end
24
+ end
25
+
26
+ module String::Etest
27
+ def test_starts_with
28
+ assert "abcde".starts_with?("")
29
+ assert "abcde".starts_with?("a")
30
+ assert "abcde".starts_with?("ab")
31
+ assert "abcde".starts_with?("abc")
32
+ assert "abcde".starts_with?("abcd")
33
+ assert "abcde".starts_with?("abcde")
34
+
35
+ assert !("abcde".starts_with?("abcdef"))
36
+ assert !("abcde".starts_with?("xy"))
37
+ end
38
+
39
+ def test_ends_with
40
+ assert "abcde".ends_with?("")
41
+ assert "abcde".ends_with?("e")
42
+ assert "abcde".ends_with?("de")
43
+ assert "abcde".ends_with?("cde")
44
+ assert "abcde".ends_with?("bcde")
45
+ assert "abcde".ends_with?("abcde")
46
+
47
+ assert !("abcde".ends_with?("abcdef"))
48
+ assert !("abcde".ends_with?("xy"))
49
+ end
50
+
51
+ def test_constantize
52
+ assert_equal String, "String".constantize
53
+ assert_raise(NameError) {
54
+ "I::Dont::Know::This".constantize
55
+ }
56
+ assert_raise(ArgumentError) {
57
+ "".constantize
58
+ }
59
+ end
60
+ end if VEX_TEST == "boot"
@@ -0,0 +1,28 @@
1
+ class CreateRequestLog < ActiveRecord::Migration
2
+ TABLE_NAME = ImprovedLogging::TABLE_NAME
3
+
4
+ def self.up
5
+ create_table TABLE_NAME, :force => true do |t|
6
+ t.integer :user_id # account ID
7
+ t.string :ip # remote IP
8
+ t.integer :xhr # yes/no
9
+ t.string :method # get/post etc.
10
+ t.string :protocol # http:/https:
11
+ t.string :host # host
12
+ t.string :path # path
13
+ t.string :query # query param
14
+ t.string :action # controller + action
15
+ t.string :status # result status
16
+ t.integer :msecs # time needed
17
+ t.integer :queries # of SQL queries needed
18
+ t.integer :sql_select # of SQL queries needed
19
+ t.integer :sql_update # of SQL queries needed
20
+ t.integer :sql_insert # of SQL queries needed
21
+ t.integer :sql_delete # of SQL queries needed
22
+ end
23
+ end
24
+
25
+ def self.down
26
+ drop_table(TABLE_NAME) rescue nil
27
+ end
28
+ end
data/r.rb ADDED
@@ -0,0 +1,35 @@
1
+ def a
2
+ puts "a"
3
+ end
4
+
5
+ def b
6
+ puts "b"
7
+ end
8
+
9
+ def f
10
+ a
11
+ yield
12
+ ensure
13
+ b
14
+ end
15
+
16
+
17
+
18
+
19
+
20
+
21
+ f do
22
+ puts "inner"
23
+ end
24
+
25
+
26
+ cc = nil
27
+
28
+ f do
29
+ callcc { |s| cc = s }
30
+ end
31
+
32
+ cc.call
33
+
34
+ #
35
+ #a
data/script/console ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Start a console that initializes the gem
4
+ #
5
+ require "irb"
6
+ require "rubygems"
7
+
8
+ begin
9
+ require 'wirble'
10
+ Wirble.init
11
+ Wirble.colorize
12
+ rescue LoadError
13
+ STDERR.puts "To enable colorized and tab completed run 'gem install wirble'"
14
+ end
15
+
16
+ $: << "#{File.dirname(__FILE__)}/../lib"
17
+ require "#{File.dirname(__FILE__)}/../init.rb"
18
+
19
+ IRB.start
data/script/rebuild ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+ rake default build_gemspec package || exit 1
3
+ echo "============================================="
4
+ echo "I built the gem for you. To upload, run "
5
+ echo
6
+ echo "gem push $(ls -d pkg/*.gem | tail -n 1)"
7
+
data/tasks/echoe.rake ADDED
@@ -0,0 +1,52 @@
1
+ unless defined?(SKIP_ECHOE)
2
+
3
+ #
4
+ # GEM settings
5
+ #
6
+ GEM_ROOT = File.expand_path("#{File.dirname(__FILE__)}/..")
7
+
8
+ if gem_config = YAML.load(File.read("#{GEM_ROOT}/config/gem.yml"))
9
+ require 'echoe'
10
+
11
+ #
12
+ # a dependency reader
13
+ module Dependency
14
+ @@dependencies = []
15
+
16
+ def self.require(file)
17
+ @@dependencies << file
18
+ end
19
+
20
+ def self.load
21
+ eval File.read("#{GEM_ROOT}/config/dependencies.rb"), binding
22
+ @@dependencies
23
+ end
24
+ end
25
+
26
+ Echoe.new(File.basename(GEM_ROOT), File.read("#{GEM_ROOT}/VERSION")) do |p|
27
+ gem_config.each do |k,v|
28
+ p.send "#{k}=",v
29
+ end
30
+
31
+ p.runtime_dependencies = Dependency.load
32
+ end
33
+
34
+ desc "Rebuild and install the gem"
35
+ task :rebuild => %w(manifest default build_gemspec package) do
36
+ gem = Dir.glob("pkg/*.gem").sort_by do |filename|
37
+ File.new(filename).mtime
38
+ end.last
39
+
40
+ puts "============================================="
41
+ puts "Installing gem..."
42
+
43
+ system "gem install #{gem} > /dev/null 2>&1"
44
+
45
+ puts ""
46
+ puts "I built and installed the gem for you. To upload, run "
47
+ puts
48
+ puts " gem push #{gem}"
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,14 @@
1
+ namespace :db do
2
+ desc "Validate all models in the database"
3
+ task :validate => :environment do
4
+ ActiveRecord::Validate.all
5
+ end
6
+
7
+ namespace :validate do
8
+ desc "Purge all invalid models form the database"
9
+ task :purge => :environment do
10
+ ActiveRecord::Validate.purge
11
+ end
12
+ end
13
+
14
+ end
data/test/ar.rb ADDED
@@ -0,0 +1,30 @@
1
+ #
2
+ # set up AR
3
+
4
+ # require "rubygems"
5
+
6
+ require 'rubygems'
7
+ require 'sqlite3'
8
+ require 'active_record'
9
+
10
+ LOGFILE = "log/test.log"
11
+ SQLITE_FILE = ":memory:"
12
+
13
+ #
14
+ # -- set up active record for tests -----------------------------------
15
+ ACTIVE_RECORD = {
16
+ :adapter => "sqlite3",
17
+ :database => ":memory:"
18
+ }
19
+
20
+ ActiveRecord::Base.logger ||= Logger.new $STDERR
21
+
22
+ #
23
+ # setup connection
24
+ ActiveRecord::Base.establish_connection ACTIVE_RECORD
25
+
26
+
27
+ #
28
+ # start tests
29
+ VEX_TEST="active_record"
30
+ load "#{File.dirname(__FILE__)}/test_helper.rb"