gather 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/gather.rb +54 -7
  2. data/test/test_gather.rb +32 -6
  3. metadata +40 -13
@@ -1,8 +1,42 @@
1
- require 'set'
2
-
3
1
  # Provides instance method gather and class method property
4
2
  # Allows properties to be Enumerable.
5
3
  module Gather
4
+ # copied from StackOverflow
5
+ class UniqueArray < Array
6
+ def initialize(*args)
7
+ if args.size == 1 and args[0].is_a? Array then
8
+ super(args[0].uniq)
9
+ else
10
+ super(*args)
11
+ end
12
+ end
13
+
14
+ def insert(i, v)
15
+ super(i, v) unless include?(v)
16
+ end
17
+
18
+ def <<(v)
19
+ super(v) unless include?(v)
20
+ end
21
+
22
+ def []=(*args)
23
+ # note: could just call super(*args) then uniq!, but this is faster
24
+
25
+ # there are three different versions of this call:
26
+ # 1. start, length, value
27
+ # 2. index, value
28
+ # 3. range, value
29
+ # We just need to get the value
30
+ v = case args.size
31
+ when 3 then args[2]
32
+ when 2 then args[1]
33
+ else nil
34
+ end
35
+
36
+ super(*args) if v.nil? or not include?(v)
37
+ end
38
+ end
39
+
6
40
  # Evaluate the block and gather options from args. Even if it's nil.
7
41
  # Return self
8
42
  def gather( args = {}, &block )
@@ -21,17 +55,29 @@ module Gather
21
55
  end
22
56
  self
23
57
  end
58
+
59
+ def properties
60
+ self.class.properties
61
+ end
24
62
 
25
63
  # return a hash of the properties
26
64
  def to_hash( properties = self.class.properties )
27
65
  properties.inject({}) {|s,x| s[x] = send(x); s}
28
66
  end
29
67
 
68
+ def to_a( properties = self.class.properties )
69
+ properties.inject([]) {|s,x| s << send(x) }
70
+ end
71
+ alias_method :values, :to_a
72
+
30
73
  # set all values specified in the hash
31
74
  def merge!( hash )
32
75
  hash.each {|k,v| send( k, v ) }
33
76
  end
34
-
77
+ def start=( value )
78
+ Time.new( value.to_n * 60 * 60 )
79
+ end
80
+
35
81
  # return a new hash merged with the argument
36
82
  def merge( hash )
37
83
  to_hash.merge( hash )
@@ -57,7 +103,7 @@ module Gather
57
103
  module ClassMethods
58
104
  # return properties for only this class
59
105
  def own_properties
60
- @properties ||= Set.new
106
+ @properties ||= UniqueArray.new
61
107
  end
62
108
 
63
109
  # return properties for this class and ancestors
@@ -81,15 +127,16 @@ module Gather
81
127
  if val.empty?
82
128
  @#{stripped}
83
129
  else
84
- @#{stripped} = val.size == 1 ? val[0] : val
130
+ # use the assignment, so only it has to be overloaded
131
+ self.#{stripped} = val.size == 1 ? val[0] : val
85
132
  end
86
133
  else
87
134
  @#{stripped} = block
88
135
  end
89
136
  end
90
137
 
91
- def #{stripped}=(*val)
92
- @#{stripped} = val.size == 1 ? val[0] : val
138
+ def #{stripped}=( value )
139
+ @#{stripped} = value
93
140
  end
94
141
  EOF
95
142
  class_eval st, __FILE__, line + 1
@@ -9,6 +9,16 @@ class SumDumClass
9
9
  property :civ
10
10
  end
11
11
 
12
+ class NoDuplicateProperties
13
+ include Gather
14
+
15
+ property :aac
16
+ property :ben
17
+ property :civ
18
+
19
+ property :aac
20
+ end
21
+
12
22
  class Child < SumDumClass
13
23
  property :epi
14
24
  end
@@ -30,6 +40,10 @@ class TestGather < Test::Unit::TestCase
30
40
  assert_equal @properties, SumDumClass.properties.to_a
31
41
  end
32
42
 
43
+ should "not have duplicate properties" do
44
+ assert_equal @properties, NoDuplicateProperties.properties.to_a
45
+ end
46
+
33
47
  should "have methods" do
34
48
  @properties.each do |prop|
35
49
  assert @instance.respond_to?( prop )
@@ -72,13 +86,23 @@ class TestGather < Test::Unit::TestCase
72
86
  assert_equal "This is another block", @instance.aac.call(1,2,3)
73
87
  end
74
88
 
75
- should "have hash values" do
76
- @instance.aac = 'hey'
77
- @instance.ben = 'thewre'
78
- @instance.civ = 'dude'
89
+ context "values" do
90
+ setup do
91
+ @instance.aac = 'hey'
92
+ @instance.ben = 'thewre'
93
+ @instance.civ = 'dude'
94
+ end
79
95
 
80
- hash = {:aac => 'hey', :ben => 'thewre', :civ => 'dude'}
81
- assert_equal hash, @instance.to_hash
96
+ should "hash" do
97
+ hash = {:aac => 'hey', :civ => 'dude', :ben => 'thewre'}
98
+ assert_equal hash, @instance.to_hash
99
+ end
100
+
101
+ should "array" do
102
+ arr = %w{hey thewre dude}
103
+ assert_equal arr, @instance.to_a
104
+ assert_equal arr, @instance.values
105
+ end
82
106
  end
83
107
 
84
108
  should_eventually "test merge with a hash"
@@ -86,4 +110,6 @@ class TestGather < Test::Unit::TestCase
86
110
  should_eventually "test gather with a self-block"
87
111
  should_eventually "test gather with a hash"
88
112
  should_eventually "test gather with everything"
113
+ should_eventually "no have duplicate properties, even for subclasses"
114
+ should_eventually "test that the assignment accessor is canonical"
89
115
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gather
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
5
11
  platform: ruby
6
12
  authors:
7
13
  - John Anderson
@@ -9,29 +15,41 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-09-02 00:00:00 +02:00
18
+ date: 2011-01-20 00:00:00 +02:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: newgem
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 0
23
34
  version: 1.3.0
24
- version:
35
+ type: :development
36
+ version_requirements: *id001
25
37
  - !ruby/object:Gem::Dependency
26
38
  name: hoe
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
30
42
  requirements:
31
43
  - - ">="
32
44
  - !ruby/object:Gem::Version
45
+ hash: 55
46
+ segments:
47
+ - 1
48
+ - 8
49
+ - 0
33
50
  version: 1.8.0
34
- version:
51
+ type: :development
52
+ version_requirements: *id002
35
53
  description: |-
36
54
  A gem that provides modules to make working with properties a bit easier.
37
55
 
@@ -71,6 +89,9 @@ files:
71
89
  - lib/changes.rb
72
90
  - script/destroy
73
91
  - script/generate
92
+ - test/test_helper.rb
93
+ - test/test_changes.rb
94
+ - test/test_gather.rb
74
95
  has_rdoc: true
75
96
  homepage: http://gather.rubyforge.org
76
97
  licenses: []
@@ -82,21 +103,27 @@ rdoc_options:
82
103
  require_paths:
83
104
  - lib
84
105
  required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
85
107
  requirements:
86
108
  - - ">="
87
109
  - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
88
113
  version: "0"
89
- version:
90
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
91
116
  requirements:
92
117
  - - ">="
93
118
  - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
94
122
  version: "0"
95
- version:
96
123
  requirements: []
97
124
 
98
125
  rubyforge_project: clevic
99
- rubygems_version: 1.3.5
126
+ rubygems_version: 1.3.7
100
127
  signing_key:
101
128
  specification_version: 3
102
129
  summary: A gem that provides modules to make working with properties a bit easier