rubyexts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/CHANGELOG +2 -0
  2. data/LICENSE +20 -0
  3. data/README.textile +0 -0
  4. data/Rakefile +79 -0
  5. data/TODO.txt +6 -0
  6. data/lib/rubyexts.rb +102 -0
  7. data/lib/rubyexts/array.rb +20 -0
  8. data/lib/rubyexts/attribute.rb +151 -0
  9. data/lib/rubyexts/capture_io.rb +32 -0
  10. data/lib/rubyexts/class.rb +177 -0
  11. data/lib/rubyexts/colored_string.rb +103 -0
  12. data/lib/rubyexts/enumerable.rb +30 -0
  13. data/lib/rubyexts/file.rb +84 -0
  14. data/lib/rubyexts/hash.rb +180 -0
  15. data/lib/rubyexts/kernel.rb +91 -0
  16. data/lib/rubyexts/module.rb +53 -0
  17. data/lib/rubyexts/object.rb +56 -0
  18. data/lib/rubyexts/object_space.rb +16 -0
  19. data/lib/rubyexts/os.rb +89 -0
  20. data/lib/rubyexts/platform.rb +27 -0
  21. data/lib/rubyexts/random.rb +25 -0
  22. data/lib/rubyexts/string.rb +92 -0
  23. data/lib/rubyexts/time.rb +15 -0
  24. data/lib/rubyexts/time_dsl.rb +62 -0
  25. data/lib/rubyexts/try_dup.rb +43 -0
  26. data/lib/rubyexts/unique_array.rb +16 -0
  27. data/rubyexts.gemspec +36 -0
  28. data/script/spec +12 -0
  29. data/spec/rubyexts/array_spec.rb +19 -0
  30. data/spec/rubyexts/attribute_spec.rb +37 -0
  31. data/spec/rubyexts/class_spec.rb +1 -0
  32. data/spec/rubyexts/cli_spec.rb +0 -0
  33. data/spec/rubyexts/colored_string_spec.rb +9 -0
  34. data/spec/rubyexts/enumerable_spec.rb +52 -0
  35. data/spec/rubyexts/file_spec.rb +78 -0
  36. data/spec/rubyexts/hash_spec.rb +91 -0
  37. data/spec/rubyexts/kernel_spec.rb +9 -0
  38. data/spec/rubyexts/module_spec.rb +0 -0
  39. data/spec/rubyexts/object_space_spec.rb +17 -0
  40. data/spec/rubyexts/object_spec.rb +57 -0
  41. data/spec/rubyexts/os_spec.rb +121 -0
  42. data/spec/rubyexts/platform_spec.rb +0 -0
  43. data/spec/rubyexts/random_spec.rb +31 -0
  44. data/spec/rubyexts/string_spec.rb +23 -0
  45. data/spec/rubyexts/time_dsl_spec.rb +88 -0
  46. data/spec/rubyexts/time_spec.rb +11 -0
  47. data/spec/rubyexts/unique_array_spec.rb +0 -0
  48. data/spec/rubyexts_spec.rb +182 -0
  49. data/spec/spec.opts +5 -0
  50. data/spec/spec_helper.rb +16 -0
  51. metadata +104 -0
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ class TrueClass
4
+ def try_dup
5
+ self
6
+ end
7
+ end
8
+
9
+ class FalseClass
10
+ def try_dup
11
+ self
12
+ end
13
+ end
14
+
15
+ class Symbol
16
+ def try_dup
17
+ self
18
+ end
19
+ end
20
+
21
+ class Object
22
+ def try_dup
23
+ self.dup
24
+ end
25
+ end
26
+
27
+ class Numeric
28
+ def try_dup
29
+ self
30
+ end
31
+ end
32
+
33
+ class NilClass
34
+ def try_dup
35
+ self
36
+ end
37
+ end
38
+
39
+ class Module
40
+ def try_dup
41
+ self
42
+ end
43
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ # Get is using it
4
+ # co treba udelat to pres observery?
5
+ module RubyExts
6
+ class UniqueArray < Array
7
+ def push(*args)
8
+ args.each do |arg|
9
+ unless self.include?(arg)
10
+ super(arg)
11
+ end
12
+ end
13
+ end
14
+ alias_method :<<, :push
15
+ end
16
+ end
data/rubyexts.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env gem1.9 build
2
+ # encoding: utf-8
3
+
4
+ Dir[File.join(File.dirname(__FILE__), "vendor", "*")].each do |path|
5
+ if File.directory?(path) && Dir["#{path}/*"].empty?
6
+ warn "Dependency #{File.basename(path)} in vendor seems to be empty. Run git submodule init && git submodule update to checkout it."
7
+ elsif File.directory?(path) && File.directory?(File.join(path, "lib"))
8
+ $:.unshift File.join(path, "lib")
9
+ end
10
+ end
11
+
12
+ # Run thor package:gem or gem build rango.gemspec
13
+ # NOTE: we can't use require_relative because when we run gem build, it use eval for executing this file
14
+ require File.join(File.dirname(__FILE__), "lib", "rubyexts")
15
+
16
+ Gem::Specification.new do |s|
17
+ s.name = "rubyexts"
18
+ s.version = RubyExts::VERSION
19
+ s.authors = ["Jakub Šťastný aka Botanicus"]
20
+ s.homepage = "http://github.com/botanicus/rubyexts"
21
+ s.summary = "Tiny library with Ruby extensions"
22
+ s.description = "" # TODO: long description
23
+ s.cert_chain = nil
24
+ s.email = ["knava.bestvinensis", "gmail.com"].join("@")
25
+ s.has_rdoc = true
26
+
27
+ # files
28
+ s.files = Dir.glob("**/*") - Dir.glob("pkg/*")
29
+ s.require_paths = ["lib"]
30
+
31
+ # Ruby version
32
+ s.required_ruby_version = ::Gem::Requirement.new(">= 1.9.1")
33
+
34
+ # RubyForge
35
+ s.rubyforge_project = "rubyexts"
36
+ end
data/script/spec ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # encoding: utf-8
3
+
4
+ Dir[File.join(File.dirname(__FILE__), "..", "vendor", "*")].each do |path|
5
+ if File.directory?(path) && Dir["#{path}/*"].empty?
6
+ warn "Dependency #{File.basename(path)} in vendor seems to be empty. Run git submodule init && git submodule update to checkout it."
7
+ elsif File.directory?(path) && File.directory?(File.join(path, "lib"))
8
+ $:.unshift File.join(path, "lib")
9
+ end
10
+ end
11
+
12
+ load File.expand_path(File.join(File.dirname(__FILE__), "..", "vendor", "rspec", "bin", "spec"))
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/array"
4
+
5
+ describe Array do
6
+ describe "#only" do
7
+ it "should returns the only item in array" do
8
+ [:first].only.should eql(:first)
9
+ end
10
+
11
+ it "should raise IndexError if the array has more items" do
12
+ -> { [1, 2].only }.should raise_error(IndexError)
13
+ end
14
+
15
+ it "should raise IndexError if the array is empty" do
16
+ -> { Array.new.only }.should raise_error(IndexError)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/attribute"
4
+
5
+ describe AttributeMixin do
6
+ describe "#private_alias" do
7
+ before(:each) do
8
+ @class = Class.new { private_alias :class }
9
+ end
10
+
11
+ it "should creates __alias__ of given method" do
12
+ @class.new.tap do |instance|
13
+ instance.class.should eql(instance.send(:__class__))
14
+ end
15
+ end
16
+
17
+ it "should be private" do
18
+ @class.private_instance_methods.should include(:__class__)
19
+ end
20
+ end
21
+
22
+ describe "#attribute" do
23
+ it
24
+ end
25
+
26
+ describe "#hattribute" do
27
+ it
28
+ end
29
+
30
+ describe "#questionable" do
31
+ it
32
+ end
33
+
34
+ describe "#default_hattribute_names" do
35
+ it
36
+ end
37
+ end
@@ -0,0 +1 @@
1
+ # TODO: take it from Rails
File without changes
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/colored_string"
4
+
5
+ describe ColoredString do
6
+ describe "#TODO" do
7
+ it
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/enumerable"
4
+
5
+ describe Enumerable do
6
+ describe "#none?" do
7
+ describe "withtout block" do
8
+ it "should be true for empty collections " do
9
+ Array.new.should be_none
10
+ Hash.new.should be_none
11
+ end
12
+
13
+ it "should returns true for collections with just nils and false" do
14
+ [nil].should be_none
15
+ [false].should be_none
16
+ [false, nil].should be_none
17
+ end
18
+
19
+ it "should returns false if collections contains some objects" do
20
+ [1].should_not be_none
21
+ {one: 1, two: 2}.should_not be_none
22
+ {key: nil, other: false}.should_not be_none
23
+ end
24
+ end
25
+
26
+ describe "with block" do
27
+ describe "for array" do
28
+ it "should returns false if all iteractions returns true" do
29
+ array = Array.new(3) { Array.new }
30
+ array.none? { |item| item.empty? }.should be_false
31
+ end
32
+
33
+ it "should returns true if all iteractions returns false" do
34
+ array = Array.new(3) { [1] }
35
+ array.none? { |item| item.empty? }.should be_true
36
+ end
37
+ end
38
+
39
+ describe "for hash" do
40
+ it "should returns false if all iteractions returns true" do
41
+ hash = {key: Array.new, another: Hash.new, last: Array.new}
42
+ hash.none? { |key, value| value.empty? }.should be_false
43
+ end
44
+
45
+ it "should returns true if all iteractions returns false" do
46
+ hash = {key: [1], another: [2], last: [nil]}
47
+ hash.none? { |item| item.empty? }.should be_true
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/file"
4
+
5
+ # TODO: don't test everything in $HOME
6
+ describe File do
7
+ before(:each) do
8
+ @token = rand(360 ** 50).to_s(36)
9
+ @path = File.join("~", @token)
10
+ end
11
+
12
+ after(:each) do
13
+ FileUtils.rm(File.expand_path(@path), force: true)
14
+ FileUtils.rm(File.expand_path("#{@path}.rw"), force: true)
15
+ end
16
+
17
+ describe ".puts" do
18
+ it "should write each argument as a line" do
19
+ path = File.puts(@path, "first", "second")
20
+ File.read(path).should eql("first\nsecond\n")
21
+ end
22
+ end
23
+
24
+ describe ".print" do
25
+ it "should write all arguments without any extra whitespaces" do
26
+ path = File.print(@path, "first", "second")
27
+ File.read(path).should eql("firstsecond")
28
+ end
29
+ end
30
+
31
+ describe ".append" do
32
+ it "should works like File.puts for new files" do
33
+ path = File.append(@path, "first", "second")
34
+ File.read(path).should eql("first\nsecond\n")
35
+ end
36
+
37
+ it "should append all arguments separated by \\n" do
38
+ File.puts(@path, "first")
39
+ path = File.append(@path, "second")
40
+ File.read(path).should eql("first\nsecond\n")
41
+ end
42
+ end
43
+
44
+ describe ".add" do
45
+ it "should works like File.print for new files" do
46
+ path = File.add(@path, "first", "second")
47
+ File.read(path).should eql("firstsecond")
48
+ end
49
+
50
+ it "should append all arguments without any extra whitespaces" do
51
+ File.print(@path, "first")
52
+ path = File.add(@path, "second")
53
+ File.read(path).should eql("firstsecond")
54
+ end
55
+ end
56
+
57
+ describe ".write" do
58
+ it "should expand file path" do
59
+ File.write(:puts, "w", @path, "content")
60
+ File.exist?(File.expand_path(@path)).should be_true
61
+ end
62
+
63
+ it "should returns expanded file path" do
64
+ File.write(:puts, "w", @path, "test").should eql(File.expand_path(@path))
65
+ end
66
+
67
+ it "should use first argument as method sended to file" do
68
+ first = File.puts("#{@path}.rw", "first", "second")
69
+ second = File.write(:puts, "w", @path, "first", "second")
70
+ File.read(first).should eql(File.read(second))
71
+ end
72
+
73
+ it "should use 3..n arguments as arguments of given method" do
74
+ path = File.write(:printf, "w", @path, "%d %04x", 123, 123)
75
+ File.read(path).should eql("123 007b")
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/hash"
4
+
5
+ describe Hash do
6
+ before(:each) do
7
+ @hash = {"one" => 1, "two" => {"inner" => 3}}
8
+ @one = {class: "post", id: 12}
9
+ @two = {class: "post", id: 14}
10
+ end
11
+
12
+ describe "#symbolize_keys" do
13
+ it "should transform keys into symbols" do
14
+ @hash.symbolize_keys.should eql(one: 1, two: {"inner" => 3})
15
+ end
16
+ end
17
+
18
+ describe "#symbolize_keys!" do
19
+ it "should transform keys into symbols" do
20
+ @hash.symbolize_keys!
21
+ @hash.should eql(one: 1, two: {"inner" => 3})
22
+ end
23
+ end
24
+
25
+ describe "#deep_symbolize_keys" do
26
+ it "should transform keys into symbols" do
27
+ @hash.deep_symbolize_keys.should eql(one: 1, two: {inner: 3})
28
+ end
29
+ end
30
+
31
+ describe "#deep_symbolize_keys!" do
32
+ it "should transform keys into symbols" do
33
+ @hash.deep_symbolize_keys!
34
+ @hash.should eql(one: 1, two: {inner: 3})
35
+ end
36
+ end
37
+
38
+ describe "#extract!" do
39
+ it "should returns just values for given keys" do
40
+ hash = {foo: "bar"}.merge(@one)
41
+ hash.extract!(:id, :foo).should eql([12, "bar"])
42
+ end
43
+ end
44
+
45
+ describe "#to_html_attrs" do
46
+ it "should convert hash into URL format" do
47
+ @one.to_html_attrs.should eql("class='post' id='12'")
48
+ end
49
+ end
50
+
51
+ describe "#to_url_attrs" do
52
+ it "should convert hash into URL format" do
53
+ @one.to_url_attrs.should eql("class=post&id=12")
54
+ end
55
+ end
56
+
57
+ describe "#reverse_merge" do
58
+ it "should returns result of merge the origin hash with first argument" do
59
+ @one.reverse_merge(@two).should eql(class: "post", id: 12)
60
+ end
61
+ end
62
+
63
+ describe "#reverse_merge!" do
64
+ it "should replace hash by result of merge with first argument which" do
65
+ @one.reverse_merge!(@two)
66
+ @one.should eql(class: "post", id: 12)
67
+ end
68
+ end
69
+
70
+ describe "#get" do
71
+ before(:each) do
72
+ @hash = {inner: @one}
73
+ end
74
+
75
+ it "should raise argument error any argument given" do
76
+ -> { @hash.get }.should raise_error(ArgumentError)
77
+ end
78
+
79
+ it "should get item if the path exist" do
80
+ @hash.get(:inner, :class).should eql("post")
81
+ end
82
+
83
+ it "should returns nil if the path doesn't exist" do
84
+ @hash.get(:inner, :foobar).should be_nil
85
+ end
86
+
87
+ it "should raise index error if trying to work with non-hash-like object like hash" do
88
+ -> { @hash.get(:inner, :class, :inner) }.should raise_error(IndexError)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/kernel"
4
+
5
+ describe Kernel do
6
+ describe "#TODO" do
7
+ it
8
+ end
9
+ end
File without changes
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/object_space"
4
+
5
+ describe ObjectSpace do
6
+ describe "#classes" do
7
+ it "should returns the only item in array" do
8
+ -> { Class.new }.should change { ObjectSpace.classes.length }.by(1)
9
+ end
10
+
11
+ it "should returns the only item in array" do
12
+ ObjectSpace.classes.each do |klass|
13
+ klass.should be_kind_of(Class)
14
+ end
15
+ end
16
+ end
17
+ end