tags 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +1 -1
- data/README.rdoc +34 -0
- data/lib/tags/version.rb +2 -2
- data/lib/tags.rb +48 -3
- data/spec/lib/tags_spec.rb +70 -0
- data/spec/spec_helper.rb +1 -0
- data/tags.gemspec +4 -0
- metadata +35 -7
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
= Tags
|
2
|
+
|
3
|
+
A Ruby object that acts like a set of tags.
|
4
|
+
|
5
|
+
== Notice
|
6
|
+
|
7
|
+
This gem is still an infant. Although at a stable version, many expected features aren't available. See "Future API" below.
|
8
|
+
|
9
|
+
== Current API
|
10
|
+
|
11
|
+
=== Removing tags
|
12
|
+
|
13
|
+
Tags.new("one two three") - Tags.new("one three")
|
14
|
+
# Tags.new("two")
|
15
|
+
|
16
|
+
=== Adding tags
|
17
|
+
|
18
|
+
Tags.new("one two three") - Tags.new("three four five")
|
19
|
+
# Tags.new("one two three four five")
|
20
|
+
|
21
|
+
=== Conversions
|
22
|
+
|
23
|
+
Tags.new("one two three").to_s
|
24
|
+
# "one two three"
|
25
|
+
|
26
|
+
Tags.new("one two three").to_a
|
27
|
+
# ["one", "two", "three"]
|
28
|
+
|
29
|
+
Tags.new("one two three").to_set
|
30
|
+
# #<Set: {"one", "two", "three"}>
|
31
|
+
|
32
|
+
== Future API
|
33
|
+
|
34
|
+
- I will fill this out soon
|
data/lib/tags/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0
|
1
|
+
class Tags
|
2
|
+
VERSION = "1.0.0"
|
3
3
|
end
|
data/lib/tags.rb
CHANGED
@@ -1,3 +1,48 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'set'
|
2
|
+
require 'active_support'
|
3
|
+
|
4
|
+
class Tags
|
5
|
+
|
6
|
+
include Comparable
|
7
|
+
|
8
|
+
##################################################
|
9
|
+
|
10
|
+
def initialize(str_or_ary = nil)
|
11
|
+
ary = str_or_ary.is_a?(Array) ? str_or_ary : str_or_ary.to_s.split(' ')
|
12
|
+
@tags_set = Set.new(ary)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
to_a.join(' ')
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_a
|
20
|
+
tags_set.to_a
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_set
|
24
|
+
tags_set.dup
|
25
|
+
end
|
26
|
+
|
27
|
+
def -(other)
|
28
|
+
raise ArgumentError, "must be a #{self.class} object" unless other.is_a?(self.class)
|
29
|
+
ary_diff = (tags_set - other.to_set).to_a
|
30
|
+
self.class.new(ary_diff)
|
31
|
+
end
|
32
|
+
|
33
|
+
def +(other)
|
34
|
+
raise ArgumentError, "must be a #{self.class} object" unless other.is_a?(self.class)
|
35
|
+
ary_add = (tags_set + other.to_set).to_a
|
36
|
+
self.class.new(ary_add)
|
37
|
+
end
|
38
|
+
|
39
|
+
def <=>(other)
|
40
|
+
tags_set <=> other.to_set
|
41
|
+
end
|
42
|
+
|
43
|
+
##################################################
|
44
|
+
private
|
45
|
+
|
46
|
+
attr_reader :tags_set
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Tags do
|
4
|
+
|
5
|
+
describe "#to_s" do
|
6
|
+
it { Tags.new("one two").to_s.should eq("one two") }
|
7
|
+
it { Tags.new.to_s.should eq("") }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#to_a" do
|
11
|
+
it { Tags.new("one two").to_a.should eq(["one", "two"]) }
|
12
|
+
it { Tags.new.to_a.should eq([]) }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#to_set" do
|
16
|
+
it { Tags.new("one two").to_set.should eq(Set.new(["one", "two"])) }
|
17
|
+
it { Tags.new.to_set.should eq(Set.new) }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#-" do
|
21
|
+
context "with a Tags" do
|
22
|
+
it { (Tags.new - Tags.new("one")).should eq(Tags.new) }
|
23
|
+
it { (Tags.new("one") - Tags.new("one")).should eq(Tags.new) }
|
24
|
+
it { (Tags.new("one two") - Tags.new("one")).should eq(Tags.new("two")) }
|
25
|
+
it { (Tags.new("one two") - Tags.new("three")).should eq(Tags.new("one two")) }
|
26
|
+
it { (Tags.new("one two three") - Tags.new("one three")).should eq(Tags.new("two")) }
|
27
|
+
end
|
28
|
+
|
29
|
+
it "argument must be of the Tags type" do
|
30
|
+
lambda do
|
31
|
+
Tags.new("one") - ["one"]
|
32
|
+
end.should raise_error(ArgumentError, "must be a Tags object")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#+" do
|
37
|
+
context "with a Tags" do
|
38
|
+
it { (Tags.new + Tags.new("one")).should eq(Tags.new("one")) }
|
39
|
+
it { (Tags.new("one") + Tags.new("one")).should eq(Tags.new("one")) }
|
40
|
+
it { (Tags.new("one two") + Tags.new).should eq(Tags.new("one two")) }
|
41
|
+
it { (Tags.new("one two") + Tags.new("three")).should eq(Tags.new("one two three")) }
|
42
|
+
end
|
43
|
+
|
44
|
+
it "argument must be of the Tags type" do
|
45
|
+
lambda do
|
46
|
+
Tags.new("one") + ["one"]
|
47
|
+
end.should raise_error(ArgumentError, "must be a Tags object")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#==" do
|
52
|
+
context "with a Tags containing the same items" do
|
53
|
+
it { Tags.new("one two").should eq(Tags.new("one two")) }
|
54
|
+
it { Tags.new("one two three").should eq(Tags.new("one two three")) }
|
55
|
+
it { Tags.new("one two").should eq(Tags.new("one two")) }
|
56
|
+
it { Tags.new.should eq(Tags.new) }
|
57
|
+
|
58
|
+
it { Tags.new("one two").should_not eq(Tags.new("one two three")) }
|
59
|
+
it { Tags.new.should_not eq(Tags.new("one two three")) }
|
60
|
+
end
|
61
|
+
|
62
|
+
context "with a non-Tags" do
|
63
|
+
it { Tags.new("one").should_not eq("str") }
|
64
|
+
it { Tags.new("one").should_not eq(["str"]) }
|
65
|
+
it { Tags.new("one").should_not eq(Set.new(["str"])) }
|
66
|
+
it { Tags.new.should_not eq(nil) }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/tags')
|
data/tags.gemspec
CHANGED
@@ -17,4 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.test_files= `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec", "~> 2"
|
22
|
+
|
23
|
+
s.add_dependency "activesupport", "~> 3"
|
20
24
|
end
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: tags
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.1
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Austin Schneider
|
@@ -14,10 +14,35 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-02-
|
17
|
+
date: 2011-02-16 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
version: "2"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activesupport
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 3
|
43
|
+
version: "3"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
21
46
|
description: A Ruby object with the behavior of a list of tags.
|
22
47
|
email:
|
23
48
|
- soccer022483@gmail.com
|
@@ -34,6 +59,8 @@ files:
|
|
34
59
|
- Rakefile
|
35
60
|
- lib/tags.rb
|
36
61
|
- lib/tags/version.rb
|
62
|
+
- spec/lib/tags_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
37
64
|
- tags.gemspec
|
38
65
|
has_rdoc: true
|
39
66
|
homepage: http://github.com/soccer022483/tags
|
@@ -67,5 +94,6 @@ rubygems_version: 1.3.7
|
|
67
94
|
signing_key:
|
68
95
|
specification_version: 3
|
69
96
|
summary: A Ruby object with the behavior of a list of tags.
|
70
|
-
test_files:
|
71
|
-
|
97
|
+
test_files:
|
98
|
+
- spec/lib/tags_spec.rb
|
99
|
+
- spec/spec_helper.rb
|