rubyhelpers 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +12 -0
- data/README.rdoc +64 -0
- data/Rakefile +16 -0
- data/init.rb +0 -0
- data/lib/rubyhelpers.rb +6 -0
- data/lib/rubyhelpers/hash.rb +13 -0
- data/lib/rubyhelpers/object.rb +11 -0
- data/lib/rubyhelpers/string.rb +7 -0
- data/rubyhelpers.gemspec +34 -0
- data/tasks/test.rake +4 -0
- data/tests/hash_test.rb +27 -0
- data/tests/object_test.rb +31 -0
- data/tests/string_test.rb +15 -0
- metadata +84 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
== RubyHelpers
|
2
|
+
|
3
|
+
RubyHelpers a set of helpers for Ruby objects such as Object, String and Hash. Ideal for use with lightweight frameworks like Sinatra that don't have all the bells and whistles of Rails.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
gem install chalkers-rubyhelpers --source http://gems.github.com
|
8
|
+
|
9
|
+
== Object Helpers Usage
|
10
|
+
|
11
|
+
Following is an overview of Object#blank?, String#to_html, Hash dot notation and Hash#{key}_exists?
|
12
|
+
|
13
|
+
== Object#blank?
|
14
|
+
Implementation from http://redhanded.hobix.com/inspect/objectBlank.html
|
15
|
+
|
16
|
+
require 'rubyhelpers'
|
17
|
+
"".blank? #=> true
|
18
|
+
false.blank? #=> true
|
19
|
+
0.blank? #=> true
|
20
|
+
nil.blank? #=> true
|
21
|
+
[].blank? #=> true
|
22
|
+
{}.blank? #=>true
|
23
|
+
|
24
|
+
== String#to_html
|
25
|
+
Uses RedCloth to format textile to HTML
|
26
|
+
|
27
|
+
require 'rubyhelpers'
|
28
|
+
"h1. %_*Hello Word!*_%".to_html #=> "<h1><span><em><strong>Hello World!</strong></em></span></h1>"
|
29
|
+
|
30
|
+
== Hash Dot Notation and {key}_exists?
|
31
|
+
Accesses hash values via dot notation and checking if attributes exist or not.
|
32
|
+
|
33
|
+
require 'rubyhelpers'
|
34
|
+
person = { "first_name" => "Andrew", "last_name" => "Chalkley", "phone_numbers" => {"home" => "555-555", "mobile" => "555-123"} }
|
35
|
+
person.first_name #=> "Andrew"
|
36
|
+
person.phone_numbers.home #=> "555-555"
|
37
|
+
|
38
|
+
person.phone_numbers.mobile_exists? #=> true
|
39
|
+
person.phone_numbers.office_exists? #=> false
|
40
|
+
person.age_exists? #=> false
|
41
|
+
|
42
|
+
This is really handy when parsing JSON
|
43
|
+
|
44
|
+
require 'rubyhelpers'
|
45
|
+
requite 'json'
|
46
|
+
person = JSON.parse('{"name": "Andrew Chalkley"}')
|
47
|
+
person.name #=> "Andrew Chalkley"
|
48
|
+
|
49
|
+
== License
|
50
|
+
THE MIT LICENSE
|
51
|
+
|
52
|
+
Copyright (c) 2009 Andrew Chalkley
|
53
|
+
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
55
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
56
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
57
|
+
|
58
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
59
|
+
|
60
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
61
|
+
|
62
|
+
http://creativecommons.org/licenses/MIT/
|
63
|
+
|
64
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
task :default => :test
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
require 'echoe'
|
6
|
+
|
7
|
+
Echoe.new('rubyhelpers', '0.1.1') do |p|
|
8
|
+
p.description = "Ruby Helpers for Object, String and Hash. Ideal for use with lightweight frameworks like Sinatra."
|
9
|
+
p.url = "http://github.com/chalkers/rubyhelpers"
|
10
|
+
p.author = "Andrew Chalkley"
|
11
|
+
p.email = "andrew@chalkely.org"
|
12
|
+
p.ignore_pattern = ["tmp/*", "scripts/*"]
|
13
|
+
p.development_dependencies = ["redcloth"]
|
14
|
+
end
|
15
|
+
|
16
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/init.rb
ADDED
File without changes
|
data/lib/rubyhelpers.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
class Hash
|
2
|
+
def method_missing(sym, *args)
|
3
|
+
method = sym.to_s
|
4
|
+
if method[-1..-1] == "="
|
5
|
+
self[method[0..-2]] = args[0]
|
6
|
+
elsif method[-8..-1] == "_exists?"
|
7
|
+
self.has_key?(method[0..-9])
|
8
|
+
else
|
9
|
+
raise NoMethodError unless self.has_key?(method)
|
10
|
+
self[method]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/rubyhelpers.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{rubyhelpers}
|
5
|
+
s.version = "0.1.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Andrew Chalkley"]
|
9
|
+
s.date = %q{2009-04-01}
|
10
|
+
s.description = %q{Ruby Helpers for Object, String and Hash. Ideal for use with lightweight frameworks like Sinatra.}
|
11
|
+
s.email = %q{andrew@chalkely.org}
|
12
|
+
s.extra_rdoc_files = ["lib/rubyhelpers/hash.rb", "lib/rubyhelpers/object.rb", "lib/rubyhelpers/string.rb", "lib/rubyhelpers.rb", "README.rdoc", "tasks/test.rake"]
|
13
|
+
s.files = ["init.rb", "lib/rubyhelpers/hash.rb", "lib/rubyhelpers/object.rb", "lib/rubyhelpers/string.rb", "lib/rubyhelpers.rb", "Manifest", "Rakefile", "README.rdoc", "tasks/test.rake", "tests/hash_test.rb", "tests/object_test.rb", "tests/string_test.rb", "rubyhelpers.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/chalkers/rubyhelpers}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rubyhelpers", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{rubyhelpers}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Ruby Helpers for Object, String and Hash. Ideal for use with lightweight frameworks like Sinatra.}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_development_dependency(%q<redcloth>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<redcloth>, [">= 0"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<redcloth>, [">= 0"])
|
33
|
+
end
|
34
|
+
end
|
data/tasks/test.rake
ADDED
data/tests/hash_test.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
class HashTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def teardown
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_hash
|
12
|
+
hash_one = { "first" => nil }
|
13
|
+
assert_nil(hash_one.first)
|
14
|
+
hash_two = { "second" => nil }
|
15
|
+
hash_one.first= hash_two
|
16
|
+
assert_equal(hash_two, hash_one.first)
|
17
|
+
hash_one.first.second= true
|
18
|
+
assert(hash_one.first.second)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_hash_has_method
|
22
|
+
hash_one = { "first" => nil }
|
23
|
+
assert(hash_one.first_exists?)
|
24
|
+
assert(!hash_one.second_exists?)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
class ObjectTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_booleans
|
9
|
+
assert(false.blank?)
|
10
|
+
assert(!true.blank?)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_nil
|
14
|
+
assert(nil.blank?)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_integers
|
18
|
+
assert(0.blank?)
|
19
|
+
assert(!-1.blank?)
|
20
|
+
assert(!1.blank?)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_strings
|
24
|
+
assert("".blank?)
|
25
|
+
assert(!"abc".blank?)
|
26
|
+
end
|
27
|
+
|
28
|
+
def teardown
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
class StringTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_to_html
|
9
|
+
assert_equal("<h1><span><em><strong>Hello World</strong></em></span></h1>", "h1. %_*Hello World*_%".to_html)
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyhelpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Chalkley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-01 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: redcloth
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Ruby Helpers for Object, String and Hash. Ideal for use with lightweight frameworks like Sinatra.
|
26
|
+
email: andrew@chalkely.org
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- lib/rubyhelpers/hash.rb
|
33
|
+
- lib/rubyhelpers/object.rb
|
34
|
+
- lib/rubyhelpers/string.rb
|
35
|
+
- lib/rubyhelpers.rb
|
36
|
+
- README.rdoc
|
37
|
+
- tasks/test.rake
|
38
|
+
files:
|
39
|
+
- init.rb
|
40
|
+
- lib/rubyhelpers/hash.rb
|
41
|
+
- lib/rubyhelpers/object.rb
|
42
|
+
- lib/rubyhelpers/string.rb
|
43
|
+
- lib/rubyhelpers.rb
|
44
|
+
- Manifest
|
45
|
+
- Rakefile
|
46
|
+
- README.rdoc
|
47
|
+
- tasks/test.rake
|
48
|
+
- tests/hash_test.rb
|
49
|
+
- tests/object_test.rb
|
50
|
+
- tests/string_test.rb
|
51
|
+
- rubyhelpers.gemspec
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/chalkers/rubyhelpers
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --line-numbers
|
57
|
+
- --inline-source
|
58
|
+
- --title
|
59
|
+
- Rubyhelpers
|
60
|
+
- --main
|
61
|
+
- README.rdoc
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "1.2"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project: rubyhelpers
|
79
|
+
rubygems_version: 1.3.1
|
80
|
+
signing_key:
|
81
|
+
specification_version: 2
|
82
|
+
summary: Ruby Helpers for Object, String and Hash. Ideal for use with lightweight frameworks like Sinatra.
|
83
|
+
test_files: []
|
84
|
+
|