freighthopper 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/freighthopper.rb CHANGED
@@ -1,141 +1,6 @@
1
- require 'active_support'
1
+ %w{
2
+ activerecord array float/to_s hash/map_keys string
3
+ antonym_accessor array define_and_alias eval_with_options
4
+ is_one_of lazy_alias or_if_blank soft_send stdout_extensions
5
+ }.each {|lib| require "freighthopper/#{lib}"}
2
6
 
3
- class Module
4
- def define_and_alias(target, feature, &blk)
5
- aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
6
- define_method :"#{aliased_target}_with_#{feature}#{punctuation}", &blk
7
- alias_method_chain target, feature
8
- end
9
-
10
- def lazy_alias(to, from)
11
- define_method(to){|*args| send from, *args}
12
- end
13
-
14
- def antonym_accessor(*args)
15
- if 2 == args.length
16
- from, to = args
17
- define_method("#{to}?") { not send "#{from}?" }
18
- define_method("#{to}=") {|val| send "#{from}=", ! val }
19
- elsif args.singular.is_a?(Hash)
20
- args.singular.each {|from, to| antonym_accessor from, to}
21
- else
22
- message = <<-EXCEPTION
23
- antonym_accessor expects either
24
- "antonym_accessor :from, :to"
25
- or "antonym_accessor :from => :to"
26
- EXCEPTION
27
-
28
- raise message.unindent
29
- end
30
- end
31
- end
32
-
33
- class Array
34
- def singular?() size == 1 end
35
- def singular() singular?? first : nil end
36
- def singular!() singular or raise "not singular" end
37
- def symbols() map {|x| x.to_sym} end
38
- def exclude?(item) ! include? item end
39
- end
40
-
41
- class Object
42
- def soft_send(method, *args)
43
- send method, *args if respond_to? method
44
- end
45
-
46
- def or_if_blank(val = nil)
47
- if soft_send :blank?
48
- val || (block_given? ? yield : nil)
49
- else
50
- self
51
- end
52
- end
53
-
54
- def is_one_of?(*args)
55
- args.flatten.any?{|arg| arg === self }
56
- end
57
-
58
- def eval_with_options(*args, &blk)
59
- with_options *args do |map|
60
- map.instance_eval(&blk)
61
- end
62
- end
63
- end
64
-
65
- class String
66
- def strip(what = /\s/)
67
- gsub /^#{what}*|#{what}*$/, ''
68
- end
69
-
70
- def /(num)
71
- scan /.{1,#{(size / num.to_f).ceil}}/
72
- end
73
-
74
- def _() split(/\s+/).symbols end
75
-
76
- def unindent(options = {})
77
- tablength = options[:tablength] || 2
78
- lines = gsub("\t", " " * tablength).split("\n")
79
-
80
- whitespace = lines.map do |line|
81
- line.match(/^(\s+)/).captures.first
82
- end.min{ |l, r| l.length <=> r.length }
83
-
84
- lines.map{ |l| l.gsub /^#{whitespace}/, ''}.join("\n")
85
- end
86
- end
87
-
88
- class Hash
89
- def map_keys(key_hash)
90
- inject({}) do |h, (k, v)|
91
- h.merge((key_hash[k] || k) => v)
92
- end
93
- end
94
- end
95
-
96
- class Float
97
- define_and_alias :to_s, :format do |*args|
98
- format = args.first
99
- return to_s_without_format if format.nil?
100
- format = "$%0.2f" if format == :usd
101
- (format.is_a?(Fixnum) ? "%0.#{format}f" : format) % self
102
- end
103
- end
104
-
105
- require 'pp'
106
- module Kernel
107
- def self.trace_output() @@trace_output ||= false end
108
- def self.trace_output=(t) @@trace_output = t end
109
- %w(pp p puts).each do |method|
110
- define_and_alias method, :source_and_passthrough do |*args|
111
- puts_without_source_and_passthrough caller.first if Kernel.trace_output
112
- send :"#{method}_without_source_and_passthrough", *args
113
- puts_without_source_and_passthrough if Kernel.trace_output
114
- return *args
115
- end
116
- end
117
- end
118
-
119
- # Kernel.trace_output = true
120
-
121
- if defined?(ActiveRecord)
122
- module ActiveRecord
123
- class Errors
124
- def clear_on(attribute)
125
- @errors.delete attribute.to_s
126
- end
127
- end
128
-
129
- class Base
130
- extend ActiveSupport::Memoizable
131
-
132
- def cache_key
133
- [self.class.to_s.underscore, to_param, soft_send(:cached_at).try(:to_i) || soft_send(:updated_at).try(:to_i)].compact.join("/")
134
- end
135
-
136
- def element_id
137
- "#{self.class.to_s.underscore}_#{self.to_param}"
138
- end
139
- end
140
- end
141
- end
@@ -0,0 +1,3 @@
1
+ require 'freighthopper/activerecord/cache_key'
2
+ require 'freighthopper/activerecord/clear_errors'
3
+ require 'freighthopper/activerecord/element_id'
@@ -0,0 +1,10 @@
1
+ if defined?(ActiveRecord)
2
+ class ActiveRecord::Base
3
+ def cache_key
4
+ [ self.class.to_s.underscore, to_param,
5
+ ( soft_send(:cached_at).try(:to_i) ||
6
+ soft_send(:updated_at).try(:to_i) )
7
+ ].compact.join "/"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ if defined?(ActiveRecord)
2
+ class ActiveRecord::Errors
3
+ def clear_on(attribute)
4
+ @errors.delete attribute.to_s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ if defined?(ActiveRecord)
2
+ class ActiveRecord::Base
3
+ def element_id
4
+ "#{self.class.to_s.underscore}_#{self.to_param}"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'freighthopper/array/singular'
2
+ require 'freighthopper/string/unindent'
3
+
4
+ class Module
5
+ def antonym_accessor(*args)
6
+ if 2 == args.length
7
+ from, to = args
8
+ define_method("#{to}?") { not send "#{from}?" }
9
+ define_method("#{to}=") {|val| send "#{from}=", ! val }
10
+ elsif args.singular.is_a?(Hash)
11
+ args.singular.each {|from, to| antonym_accessor from, to}
12
+ else
13
+ message = <<-EXCEPTION
14
+ antonym_accessor expects either
15
+ "antonym_accessor :from, :to"
16
+ or "antonym_accessor :from => :to"
17
+ EXCEPTION
18
+
19
+ raise message.unindent
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'freighthopper/array/exclude.rb'
2
+ require 'freighthopper/array/singular.rb'
3
+ require 'freighthopper/array/symbols.rb'
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def exclude?(item) ! include? item end
3
+ end
4
+
5
+
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def singular?() size == 1 end
3
+ def singular() singular?? first : nil end
4
+ def singular!() singular or raise "not singular" end
5
+ end
@@ -0,0 +1,3 @@
1
+ class Array
2
+ def symbols() map {|x| x.to_sym} end
3
+ end
@@ -0,0 +1,7 @@
1
+ class Module
2
+ def define_and_alias(target, feature, &blk)
3
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
4
+ define_method :"#{aliased_target}_with_#{feature}#{punctuation}", &blk
5
+ alias_method_chain target, feature
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Object
2
+ def eval_with_options(*args, &blk)
3
+ with_options *args do |map|
4
+ map.instance_eval(&blk)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'freighthopper/define_and_alias'
2
+
3
+ class Float
4
+ define_and_alias :to_s, :format do |*args|
5
+ format = args.first
6
+ return to_s_without_format if format.nil?
7
+ format = "$%0.2f" if format == :usd
8
+ (format.is_a?(Fixnum) ? "%0.#{format}f" : format) % self
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ class Hash
2
+ def map_keys(key_hash)
3
+ inject({}) do |h, (k, v)|
4
+ h.merge((key_hash[k] || k) => v)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def is_one_of?(*args)
3
+ args.flatten.any?{|arg| arg === self }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Module
2
+ def lazy_alias(to, from)
3
+ define_method(to){|*args| send from, *args}
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class Object
2
+ def or_if_blank(val = nil)
3
+ if soft_send :blank?
4
+ val || (block_given? ? yield : nil)
5
+ else
6
+ self
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def soft_send(method, *args)
3
+ send method, *args if respond_to? method
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'pp'
2
+ require 'freighthopper/define_and_alias'
3
+ module Kernel
4
+ def self.trace_output() @@trace_output ||= false end
5
+ def self.trace_output=(t) @@trace_output = t end
6
+ %w(print pp p puts).each do |method|
7
+ define_and_alias method, :source_and_passthrough do |*args|
8
+ puts_without_source_and_passthrough caller.first if Kernel.trace_output
9
+ send :"#{method}_without_source_and_passthrough", *args
10
+ puts_without_source_and_passthrough if Kernel.trace_output
11
+ return *args
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ require 'freighthopper/string/strip'
2
+ require 'freighthopper/string/unindent'
3
+ require 'freighthopper/string/divide'
@@ -0,0 +1,5 @@
1
+ class String
2
+ def /(num)
3
+ scan /.{1,#{(size / num.to_f).ceil}}/
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def strip(what = /\s/)
3
+ gsub /^#{what}*|#{what}*$/, ''
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class String
2
+ def unindent(options = {})
3
+ tablength = options[:tablength] || 2
4
+ lines = gsub("\t", " " * tablength).split("\n")
5
+
6
+ whitespace = lines.map do |line|
7
+ line.match(/^(\s+)/).captures.first
8
+ end.min{ |l, r| l.length <=> r.length }
9
+
10
+ lines.map{ |l| l.gsub /^#{whitespace}/, ''}.join("\n")
11
+ end
12
+ end
@@ -1,6 +1,5 @@
1
- require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
2
- require 'freighthopper'
3
-
1
+ require File.expand_path("../test_helper", __FILE__)
2
+ require 'freighthopper/antonym_accessor'
4
3
 
5
4
  class TestClass
6
5
  def private?() @private end
data/test/string_test.rb CHANGED
@@ -34,12 +34,6 @@ class StringTest < Test::Unit::TestCase
34
34
  end
35
35
  end
36
36
 
37
- context '._' do
38
- should 'split and map to sym' do
39
- assert_equal [:bark, :claim, :deep, :everest], "bark claim \n\ndeep\teverest"._
40
- end
41
- end
42
-
43
37
  context 'unindent' do
44
38
  should 'remove the least common leading whitespace' do
45
39
  unindented = (<<-END).unindent
@@ -61,4 +55,4 @@ class StringTest < Test::Unit::TestCase
61
55
  assert_equal "test\n test", unindented
62
56
  end
63
57
  end
64
- end
58
+ end
data/test/test_helper.rb CHANGED
@@ -12,9 +12,5 @@ end
12
12
 
13
13
  Bundler.require :default, :test
14
14
 
15
- $:.unshift File.instance_eval { expand_path join(dirname(__FILE__), "..", "lib") }
15
+ Test::Unit::TestCase.send :include, TestRig
16
16
 
17
-
18
- class Test::Unit::TestCase
19
- include TestRig
20
- end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freighthopper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 8
10
- version: 0.1.8
9
+ - 9
10
+ version: 0.1.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jacob Rothstein
@@ -16,31 +16,44 @@ bindir: bin
16
16
  cert_chain: []
17
17
 
18
18
  date: 2010-06-24 00:00:00 -07:00
19
- default_executable: convert_to_should_syntax
19
+ default_executable:
20
20
  dependencies: []
21
21
 
22
22
  description: More core ext
23
23
  email: github@jacobrothstein.com
24
- executables:
25
- - convert_to_should_syntax
24
+ executables: []
25
+
26
26
  extensions: []
27
27
 
28
28
  extra_rdoc_files:
29
29
  - LICENSE
30
30
  - README.textile
31
31
  files:
32
- - .gitignore
33
- - Gemfile
34
- - Gemfile.lock
32
+ - lib/freighthopper.rb
33
+ - lib/freighthopper/activerecord.rb
34
+ - lib/freighthopper/activerecord/cache_key.rb
35
+ - lib/freighthopper/activerecord/clear_errors.rb
36
+ - lib/freighthopper/activerecord/element_id.rb
37
+ - lib/freighthopper/antonym_accessor.rb
38
+ - lib/freighthopper/array.rb
39
+ - lib/freighthopper/array/exclude.rb
40
+ - lib/freighthopper/array/singular.rb
41
+ - lib/freighthopper/array/symbols.rb
42
+ - lib/freighthopper/define_and_alias.rb
43
+ - lib/freighthopper/eval_with_options.rb
44
+ - lib/freighthopper/float/to_s.rb
45
+ - lib/freighthopper/hash/map_keys.rb
46
+ - lib/freighthopper/is_one_of.rb
47
+ - lib/freighthopper/lazy_alias.rb
48
+ - lib/freighthopper/or_if_blank.rb
49
+ - lib/freighthopper/soft_send.rb
50
+ - lib/freighthopper/stdout_extensions.rb
51
+ - lib/freighthopper/string.rb
52
+ - lib/freighthopper/string/divide.rb
53
+ - lib/freighthopper/string/strip.rb
54
+ - lib/freighthopper/string/unindent.rb
35
55
  - LICENSE
36
56
  - README.textile
37
- - Rakefile
38
- - VERSION
39
- - bin/convert_to_should_syntax
40
- - freighthopper.gemspec
41
- - init.rb
42
- - lib/freighthopper.rb
43
- - pkg/.gitkeep
44
57
  - test/antonym_accessor_test.rb
45
58
  - test/array_test.rb
46
59
  - test/define_and_alias_test.rb
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- pkg/*
2
- .bundle
3
- vendor/cache/*
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- gem 'activesupport', '2.3.5', :require => 'active_support'
2
-
3
- group :test do
4
- gem "test_rig", '0.0.3'
5
- gem 'shoulda'
6
- end
data/Gemfile.lock DELETED
@@ -1,26 +0,0 @@
1
- ---
2
- specs:
3
- - activesupport:
4
- version: 2.3.5
5
- - shoulda:
6
- version: 2.10.3
7
- - test_rig:
8
- version: 0.0.3
9
- hash: 36f75b5b994d20b47841bc6cc30c3e8bdd387129
10
- sources: []
11
-
12
- dependencies:
13
- shoulda:
14
- version: ">= 0"
15
- group:
16
- - :test
17
- test_rig:
18
- version: = 0.0.3
19
- group:
20
- - :test
21
- activesupport:
22
- version: = 2.3.5
23
- require:
24
- - active_support
25
- group:
26
- - :default
data/Rakefile DELETED
@@ -1,27 +0,0 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "freighthopper"
8
- gem.summary = %Q{Some extensions for riding the rails}
9
- gem.description = %Q{More core ext}
10
- gem.email = "github@jacobrothstein.com"
11
- gem.homepage = "http://github.com/jbr/freighthopper"
12
- gem.authors = ["Jacob Rothstein"]
13
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
- end
15
- Jeweler::GemcutterTasks.new
16
- rescue LoadError
17
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
- end
19
-
20
- require 'rake/testtask'
21
- Rake::TestTask.new(:test) do |test|
22
- test.libs << 'lib' << 'test'
23
- test.pattern = 'test/**/*_test.rb'
24
- test.verbose = true
25
- end
26
-
27
- task :default => :test
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.8
@@ -1,3 +0,0 @@
1
- #!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
2
- require File.join(File.dirname(__FILE__), "../vendor/gems/environment")
3
- load File.join(File.dirname(__FILE__), "../vendor/gems/gems/shoulda-2.10.2/bin/convert_to_should_syntax")
@@ -1,73 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{freighthopper}
8
- s.version = "0.1.8"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Jacob Rothstein"]
12
- s.date = %q{2010-06-24}
13
- s.default_executable = %q{convert_to_should_syntax}
14
- s.description = %q{More core ext}
15
- s.email = %q{github@jacobrothstein.com}
16
- s.executables = ["convert_to_should_syntax"]
17
- s.extra_rdoc_files = [
18
- "LICENSE",
19
- "README.textile"
20
- ]
21
- s.files = [
22
- ".gitignore",
23
- "Gemfile",
24
- "Gemfile.lock",
25
- "LICENSE",
26
- "README.textile",
27
- "Rakefile",
28
- "VERSION",
29
- "bin/convert_to_should_syntax",
30
- "freighthopper.gemspec",
31
- "init.rb",
32
- "lib/freighthopper.rb",
33
- "pkg/.gitkeep",
34
- "test/antonym_accessor_test.rb",
35
- "test/array_test.rb",
36
- "test/define_and_alias_test.rb",
37
- "test/float_test.rb",
38
- "test/hash_test.rb",
39
- "test/kernel_test.rb",
40
- "test/lazy_alias_test.rb",
41
- "test/object_test.rb",
42
- "test/string_test.rb",
43
- "test/test_helper.rb"
44
- ]
45
- s.homepage = %q{http://github.com/jbr/freighthopper}
46
- s.rdoc_options = ["--charset=UTF-8"]
47
- s.require_paths = ["lib"]
48
- s.rubygems_version = %q{1.3.7}
49
- s.summary = %q{Some extensions for riding the rails}
50
- s.test_files = [
51
- "test/antonym_accessor_test.rb",
52
- "test/array_test.rb",
53
- "test/define_and_alias_test.rb",
54
- "test/float_test.rb",
55
- "test/hash_test.rb",
56
- "test/kernel_test.rb",
57
- "test/lazy_alias_test.rb",
58
- "test/object_test.rb",
59
- "test/string_test.rb",
60
- "test/test_helper.rb"
61
- ]
62
-
63
- if s.respond_to? :specification_version then
64
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
65
- s.specification_version = 3
66
-
67
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
68
- else
69
- end
70
- else
71
- end
72
- end
73
-
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'freighthopper'