libraries 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in libraries.gemspec
4
3
  gemspec
4
+
5
+ group :test do
6
+ gem "rspec"
7
+ end
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ libraries (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rspec (2.0.1)
11
+ rspec-core (~> 2.0.1)
12
+ rspec-expectations (~> 2.0.1)
13
+ rspec-mocks (~> 2.0.1)
14
+ rspec-core (2.0.1)
15
+ rspec-expectations (2.0.1)
16
+ diff-lcs (>= 1.1.2)
17
+ rspec-mocks (2.0.1)
18
+ rspec-core (~> 2.0.1)
19
+ rspec-expectations (~> 2.0.1)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ libraries!
26
+ rspec
data/Rakefile CHANGED
@@ -1,2 +1,12 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'spec/rake/spectask'
5
+
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ Spec::Rake::SpecTask.new do |t|
10
+ t.spec_files = FileList['spec/**/*_spec.rb']
11
+ t.spec_opts = %w(-fs --color)
12
+ end
@@ -1,3 +1,7 @@
1
- module Libraries
1
+ require "active_support"
2
2
 
3
- end
3
+ require "libraries/enumerable"
4
+ require "libraries/array"
5
+ require "libraries/hash"
6
+ require "libraries/string"
7
+ require "libraries/object"
@@ -0,0 +1,43 @@
1
+ module ActiveRecord
2
+
3
+ class Base
4
+ before_save(:_clean_whitespace)
5
+
6
+ def _clean_whitespace
7
+ self.attributes.each_pair do |key, value|
8
+ if value && value.respond_to?('strip')
9
+ self[key] = value.strip
10
+ end
11
+ end
12
+ end
13
+
14
+ def to_hash(*fields)
15
+ h = attributes.symbolize_keys
16
+ fields.present? ? h.slice(*fields.flatten) : h
17
+ end
18
+
19
+ class << self
20
+ def conditions(*args)
21
+ scoped :conditions => args
22
+ end
23
+
24
+ def date_range(options)
25
+ options.reverse_merge! :field => "created_at", :end => Time.now
26
+ scoped :conditions => { options[:field] => options[:range] || options[:start]..options[:end] }
27
+ end
28
+
29
+ def sort_scope(options)
30
+ scoped :order => "#{options[:attribute]} #{options[:direction]}"
31
+ end
32
+
33
+ def random(_limit=1)
34
+ scoped :order => "RAND()", :limit => _limit
35
+ end
36
+
37
+ def limit(_limit)
38
+ scoped :limit => _limit
39
+ end
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,17 @@
1
+ class Array
2
+
3
+ def each_pair
4
+ self.in_groups_of(2).each { |g| yield g.first, g.last }
5
+ end
6
+
7
+ # delete all passed element and return the array after delete
8
+ def delete!(*args)
9
+ self.tap { |a| a.delete_if { |e| args.include? e } }
10
+ end
11
+
12
+ # same as delete! but does not alter self
13
+ def without(*args)
14
+ self.dup.tap { |dup| dup.delete!(*args) }
15
+ end
16
+
17
+ end
@@ -0,0 +1,15 @@
1
+ module Enumerable
2
+
3
+ # Call the given block for each element, creating a new hash
4
+ # that uses the element as the key and the block's result as the value.
5
+ def map_into_hash &block
6
+ # The "reduce(:concat)" does a shallow flatten, so that blocks that
7
+ # return arrays will do the Right Thing.
8
+ Hash[*map {|x| [x, block.call(x)]}.inject([], &:concat)]
9
+ end
10
+
11
+ def map_keyvals_into_hash &block
12
+ Hash[*map(&block).inject([], &:concat)]
13
+ end
14
+
15
+ end
@@ -0,0 +1,73 @@
1
+ class Hash
2
+
3
+ def recursive_symbolize_keys!
4
+ symbolize_keys!
5
+ # symbolize each hash in .values
6
+ values.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
7
+ # symbolize each hash inside an array in .values
8
+ values.select{|v| v.is_a?(Array) }.flatten.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
9
+ self
10
+ end
11
+
12
+ # deep lookup a nested hash without failing on non-existing keys
13
+ # in which case returns and empty hash
14
+ # This will only work on NESTED hashes, something like:
15
+ # h = {:a => {:b => {:c => 3}}}
16
+ # h.lookup(:a,:b,:c) => 3
17
+ # h.lookup(:a,:b) => {:c => 3}
18
+ # h.lookup(:a,:b,:X) => nil
19
+ # h.lookup(:a,:X,:Y) => nil
20
+ # h.lookup(:W,:T,:F) => nil
21
+ def nested_lookup(*keys)
22
+ keys.inject(self){ |h,k| (h || {}).fetch(k,nil) } rescue nil
23
+ end
24
+
25
+ # Takes a hash argument of old, new pairs
26
+ # Returns hash with keys renamed
27
+ def rename_keys!(keys_hash)
28
+ keys_hash.each do |old, new|
29
+ self[new] = self.delete(old)
30
+ end
31
+ self
32
+ end
33
+
34
+ def rename_keys(keys_hash)
35
+ self.dup.rename_keys!(keys_hash)
36
+ end
37
+
38
+ # map from a block
39
+ def map_into_hash(&block)
40
+ Hash[*map {|x| [x[0], block.call(x)]}.inject([], &:concat)]
41
+ end
42
+
43
+ def parent_to?(*args)
44
+ return self.slice(*args.first.keys) == args.first if args.size == 1 && args.first.is_a?(Hash)
45
+ args.each_pair do |key, value|
46
+ return false unless self[key].to_s == value.to_s
47
+ end
48
+ true
49
+ end
50
+
51
+ ############################################################
52
+ # The following methods take as an argument, a list of keys
53
+ # See the specs for examples
54
+
55
+ def all_keys?(*keys)
56
+ keys.all? {|k| has_key? k}
57
+ end
58
+
59
+ def any_key?(*keys)
60
+ keys.any? {|k| has_key? k}
61
+ end
62
+
63
+ def all_values?(*keys)
64
+ keys.all?{|k| self[k].present?}
65
+ end
66
+
67
+ def any_value?(*keys)
68
+ keys.any?{|k| self[k].present?}
69
+ end
70
+
71
+ ############################################################
72
+
73
+ end
@@ -0,0 +1,9 @@
1
+ class Object
2
+
3
+ def m regexp=nil
4
+ m = (self.methods - Object.methods).sort
5
+ return m.grep(/#{regexp}/) if regexp
6
+ m
7
+ end
8
+
9
+ end
@@ -0,0 +1,32 @@
1
+ class String
2
+
3
+ def strip_with_arg_support!(char=nil)
4
+ if char.present?
5
+ m = char.each_char.inject('') {|r,c| r+=(c =~ /[\*\.\[\]\^\$\?\\\|\=\+\:\/\(\)]/ ? "\\" : "") + c}
6
+ self.gsub!(/^[#{m}]*(.*?)[#{m}]*$/,'\1')
7
+ else
8
+ strip_without_arg_support!
9
+ end
10
+ end
11
+ alias_method_chain :strip!, :arg_support
12
+
13
+ def strip_with_arg_support(char=nil)
14
+ char.present? ? self.dup.strip!(char) : strip_without_arg_support
15
+ end
16
+ alias_method_chain :strip, :arg_support
17
+
18
+ # In the sense of normal characters only, it also downcases the string
19
+ # Can be used, for example, in file names and css classes
20
+ def normalize!
21
+ return if normalize.blank?
22
+ self.gsub!(/[^A-Z0-9]/i, '_')
23
+ self.squeeze!("_")
24
+ self.strip!("_")
25
+ self.downcase!
26
+ end
27
+
28
+ def normalize
29
+ self.gsub(/[^A-Z0-9]/i, '_').squeeze("_").strip("_").downcase
30
+ end
31
+
32
+ end
@@ -1,3 +1,3 @@
1
1
  module Libraries
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,2 @@
1
+ require "libraries"
2
+ require "libraries/active_record" if defined?(ActiveRecord)
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+
5
+ describe "#each_pair" do
6
+ it "yeilds pairs" do
7
+ out = ''
8
+ [1,2,3,4].each_pair { |a1,a2| out << "*#{a1}#{a2}*" }
9
+ out.should == "*12**34*"
10
+ end
11
+ end
12
+
13
+ describe "#delete!" do
14
+ it "accepts one argument and returns the array after delete" do
15
+ [1,:a,3.14,"x"].delete!(1).should == [:a,3.14,"x"]
16
+ end
17
+ it "accepts a list of arguments and returns the array after delete" do
18
+ [1,:a,3.14,"x"].delete!(1, "x").should == [:a,3.14]
19
+ end
20
+ it "modifies receiver" do
21
+ a = [1,2,3]
22
+ a.delete!(2,3)
23
+ a.should == [1]
24
+ end
25
+ end
26
+
27
+ describe "#without" do
28
+ it "does not modify receiver" do
29
+ a = [1,2,3]
30
+ a.without(2,3).should == [1]
31
+ a.should == [1,2,3]
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,146 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hash do
4
+
5
+ let(:names) { {:first => 'Homer', :mi => '', :last => 'Simpson', "A" => true} }
6
+ let(:numbers) { {:b => "0", "c" => 1} }
7
+
8
+ describe "#parent_to?" do
9
+ it "returns true for existing slices" do
10
+ names.should be_parent_to(:first => "Homer", :last => "Simpson")
11
+ names.should be_parent_to(:first => "Homer")
12
+ end
13
+ it "returns false for non existing slices" do
14
+ names.should_not be_parent_to(:first => "Bart", :last => "Simpson")
15
+ names.should_not be_parent_to(:first => "Bart")
16
+ end
17
+ it "supports key, value pairs" do
18
+ names.should be_parent_to(:first, "Homer")
19
+ names.should be_parent_to(:first, "Homer", :last, "Simpson")
20
+ names.should_not be_parent_to(:first, "Bart")
21
+ names.should_not be_parent_to(:first, "Bart", :last, "Simpson")
22
+ end
23
+ it "considers string integers with the key, value pairs call" do
24
+ numbers.should be_parent_to(:b, 0)
25
+ numbers.should_not be_parent_to(:b => 0)
26
+ numbers.should be_parent_to("c", "1")
27
+ numbers.should_not be_parent_to("c" => "1")
28
+ numbers.should be_parent_to(:b, 0, "c", "1")
29
+ end
30
+ end
31
+
32
+ describe "#all_keys?" do
33
+ it "returns true when all keys are there" do
34
+ names.all_keys?(:first, :last).should be
35
+ end
36
+ it "returns false if one keys is not there" do
37
+ names.all_keys?(:first, :title).should_not be
38
+ end
39
+ it "returns false if all keys are not there" do
40
+ names.all_keys?(:title, :other).should_not be
41
+ end
42
+ end
43
+
44
+ describe "#any_key?" do
45
+ it "returns true when any keys is there" do
46
+ names.any_key?(:first, :title).should be
47
+ end
48
+ it "returns true if all keys are there" do
49
+ names.any_key?(:first, :last).should be
50
+ end
51
+ it "returns false if all keys are not there" do
52
+ names.any_key?(:title, :other).should_not be
53
+ end
54
+ end
55
+
56
+ describe "#any_values?" do
57
+ it 'returns true if one key has a value' do
58
+ names.any_value?(:first, :others).should be
59
+ end
60
+ it "returns false if all keys don't exist or don't have values" do
61
+ names.any_value?(:others, :mi).should_not be
62
+ end
63
+ end
64
+
65
+ describe "#all_values?" do
66
+ it 'returns true if all key have values' do
67
+ names.all_values?(:first, :last).should be
68
+ end
69
+ it "returns false if one keys doesn't exist or doesn't have value" do
70
+ names.all_values?(:others, :mi).should_not be
71
+ names.all_values?(:others, :last).should_not be
72
+ names.all_values?(:mi, :last).should_not be
73
+ end
74
+ end
75
+
76
+ describe "#recursive_symbolize_keys!" do
77
+ let(:hash) {
78
+ {"name" => "names", "value" => names, "value_in_array" => [names] }
79
+ }
80
+ before do
81
+ hash.recursive_symbolize_keys!
82
+ end
83
+
84
+ it 'symobolizes keys' do
85
+ hash.keys.should =~ [:name, :value, :value_in_array]
86
+ end
87
+
88
+ it 'symbolizes keys in sub hashes' do
89
+ hash[:value].keys.should =~ [:first, :mi, :last, :A]
90
+ end
91
+
92
+ it 'symbolizes keys in hashes inside sub arrays' do
93
+ hash[:value_in_array].first.keys.should =~ [:first, :mi, :last, :A]
94
+ end
95
+ end
96
+
97
+ describe "#nested_lookup" do
98
+ let(:hash) { {:a => {:b => {:c => 3}}}}
99
+ it "returns nil for non existing keys" do
100
+ hash.nested_lookup(:X).should be_nil
101
+ end
102
+ it "returns nil for non existing nested keys" do
103
+ hash.nested_lookup(:X, 1, "F").should be_nil
104
+ end
105
+ it "returns nil for non existing keys after valid keys" do
106
+ hash.nested_lookup(:a,:b,:X).should be_nil
107
+ hash.nested_lookup(:a,:X,:Y).should be_nil
108
+ end
109
+ it "returns hash value for partial lookup" do
110
+ hash.nested_lookup(:a, :b).should == {:c => 3}
111
+ end
112
+ it "returns value for valid lookup" do
113
+ hash.nested_lookup(:a,:b,:c).should == 3
114
+ end
115
+ it "returns nil for lookups that goes beyond valid ones" do
116
+ hash.nested_lookup(:a,:b,:c,:d).should be_nil
117
+ end
118
+ end
119
+
120
+ describe "#rename_keys" do
121
+ let(:source_hash) { {:a => "Symbol", 1 => "Integer", -1 => 100, "a" => "Text"} }
122
+ let(:renamed_hash) { source_hash.rename_keys(:a => :b, 1 => 2, -1 => -2, "a" => "b") }
123
+
124
+ it "renames symbol key" do
125
+ renamed_hash[:b].should == "Symbol"
126
+ end
127
+ it "renames integer key" do
128
+ renamed_hash[2].should == "Integer"
129
+ end
130
+ it "renames text key" do
131
+ renamed_hash["b"].should == "Text"
132
+ end
133
+ it "doesn't care about value type" do
134
+ renamed_hash[-2].should == 100
135
+ end
136
+ it "does not modify the source hash" do
137
+ source_hash[:a].should == "Symbol"
138
+ source_hash[:b].should_not be
139
+ end
140
+ it "supports a bang version" do
141
+ source_hash.rename_keys! :a => :b, 1 => 2, -1 => -2, "a" => "b"
142
+ source_hash.should == {:b => "Symbol", 2 => "Integer", -2 => 100, "b" => "Text"}
143
+ end
144
+ end
145
+
146
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Object do
4
+
5
+ describe "#m" do
6
+ it "returns receiver methods" do
7
+ "".m.should include("downcase")
8
+ end
9
+ it "does not include Object methods" do
10
+ "".m.should_not include(Object.methods)
11
+ end
12
+ it "retruns sorted output" do
13
+ "".m.should == "".m.sort
14
+ end
15
+ it "accepts a regexp and returns only method matching it" do
16
+ boolean_methods = "".m /\?$/
17
+ boolean_methods.should include("all?", "one?")
18
+ boolean_methods.should_not include("downcase")
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+
5
+ describe "#strip" do
6
+ it "accepts an argument and strips it from both ends of reciever" do
7
+ "**COOL**".strip("*").should == "COOL"
8
+ "^COOL^".strip("^").should == "COOL"
9
+ end
10
+
11
+ it "supports more than one character" do
12
+ "((((((((COOL))))))))))".strip("()").should == "COOL"
13
+ "((((((((COOL))))))))))".strip("()").should == "COOL"
14
+ end
15
+
16
+ it "supports normal and special characters" do
17
+ "!!@$()#^^COOLxxyyzzz".strip("!@$()#^xyz").should == "COOL"
18
+ end
19
+
20
+ it "has a bang version" do
21
+ a = "!!@$()#^^COOLxxyyzzz"
22
+ a.strip!("!@$()#^xyz")
23
+ a.should == "COOL"
24
+ end
25
+ end
26
+
27
+ describe "#normalize" do
28
+ it "strips anyting but A-Z and 0-9, squeezes and underscores" do
29
+ "** 10:10 (That's _IT_, I'm *OuT*)".normalize.should == "10_10_that_s_it_i_m_out"
30
+ end
31
+
32
+ it "has a bang version" do
33
+ a = "** 10:10 (That's _IT_, I'm *OuT*)"
34
+ a.normalize!
35
+ a.should == "10_10_that_s_it_i_m_out"
36
+ end
37
+
38
+ it "returns empty string, but does not alter reveiver if no A-Z|0-9 exist" do
39
+ a = "**%^^$%^#%*("
40
+ a.normalize.should == ""
41
+ a.normalize!.should_not be
42
+ a.should == "**%^^$%^#%*("
43
+ end
44
+
45
+ it "keeps normal letters, including repeated letters" do
46
+ "buzzz".normalize.should == "buzzz"
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'libraries')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libraries
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - On-Site.com
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-25 00:00:00 -07:00
18
+ date: 2010-11-01 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,11 +29,25 @@ extensions: []
29
29
  extra_rdoc_files: []
30
30
 
31
31
  files:
32
+ - .gitignore
32
33
  - Gemfile
34
+ - Gemfile.lock
33
35
  - Rakefile
34
36
  - lib/libraries.rb
37
+ - lib/libraries/active_record.rb
38
+ - lib/libraries/array.rb
39
+ - lib/libraries/enumerable.rb
40
+ - lib/libraries/hash.rb
41
+ - lib/libraries/object.rb
42
+ - lib/libraries/string.rb
35
43
  - lib/libraries/version.rb
44
+ - lib/rails.rb
36
45
  - libraries.gemspec
46
+ - spec/libraries/array_spec.rb
47
+ - spec/libraries/hash_spec.rb
48
+ - spec/libraries/object_spec.rb
49
+ - spec/libraries/string_spec.rb
50
+ - spec/spec_helper.rb
37
51
  has_rdoc: true
38
52
  homepage: http://rubygems.org/gems/libraries
39
53
  licenses: []