subtle 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -23,7 +23,7 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
This feature exists to make the creation of sets of objects with data easily.
|
25
25
|
|
26
|
-
* Create an array of symbols
|
26
|
+
* Create an array of symbols that match the properties on the objects you want, and
|
27
27
|
* Pass a block that returns an array of arrays with matching data.
|
28
28
|
|
29
29
|
````ruby
|
@@ -44,6 +44,21 @@ records[2].first_name # "Dagny"
|
|
44
44
|
records[2].last_name # "Taggart"
|
45
45
|
````
|
46
46
|
|
47
|
+
### Array to Object
|
48
|
+
|
49
|
+
This feature exists to make the creation of an object with assignable properties easy.
|
50
|
+
|
51
|
+
* Create an array of symbols that match the properties you want on the object.
|
52
|
+
|
53
|
+
````ruby
|
54
|
+
person = [:first_name, :last_name].to_object
|
55
|
+
|
56
|
+
person.first_name = "John"
|
57
|
+
person.first_name # Will be John
|
58
|
+
person.last_name = "Galt"
|
59
|
+
person.last_name # Will be Galt
|
60
|
+
````
|
61
|
+
|
47
62
|
### Safety Proc
|
48
63
|
|
49
64
|
This feature was written because I hate wrapping code in begin/rescue/end blocks. If I have a line of code and I don't particularly care if it fails, I have to wrap it in three more lines of care to stop exceptions.
|
data/lib/subtle/version.rb
CHANGED
@@ -1,98 +1,40 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "array to object" do
|
4
4
|
|
5
5
|
[:name, :street].each do |property|
|
6
6
|
describe "with an array with one symbol named #{property.to_s}" do
|
7
7
|
describe "and no data" do
|
8
8
|
before do
|
9
|
-
@
|
9
|
+
@result = [property].to_object
|
10
10
|
end
|
11
11
|
|
12
|
-
it "should return an
|
13
|
-
@
|
12
|
+
it "should return an object" do
|
13
|
+
@result.nil?.must_equal false
|
14
14
|
end
|
15
|
-
end
|
16
|
-
|
17
|
-
["Dangy", "Taggart"].each do |name|
|
18
|
-
describe "and one record with #{name}" do
|
19
|
-
before do
|
20
|
-
@values = [property].to_objects { [name] }
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should return one record" do
|
24
|
-
@values.count.must_equal 1
|
25
|
-
end
|
26
15
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "and two records with John and Howard" do
|
34
|
-
before do
|
35
|
-
@values = [property].to_objects {["John", "Howard"]}
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should return two records" do
|
39
|
-
@values.count.must_equal 2
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should set the first to John" do
|
43
|
-
@values[0].send(property).must_equal "John"
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should set the second to Howard" do
|
47
|
-
@values[1].send(property).must_equal "Howard"
|
16
|
+
it "should create an attr_accessor for the property" do
|
17
|
+
@result.respond_to?(property).must_equal true
|
18
|
+
@result.respond_to?("#{property}=".to_sym).must_equal true
|
48
19
|
end
|
49
20
|
end
|
50
21
|
end
|
22
|
+
end
|
51
23
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should return one record" do
|
59
|
-
@values.count.must_equal 1
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should set the first name to Ellis" do
|
63
|
-
@values[0].first_name.must_equal 'Ellis'
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should set the last name to Wyatt" do
|
67
|
-
@values[0].last_name.must_equal 'Wyatt'
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe "with 'Ellis' and 'Wyatt', then 'Dagny' and 'Taggart'" do
|
72
|
-
before do
|
73
|
-
@values = [:first_name, :last_name].to_objects { [['Ellis', 'Wyatt'], ['Dagny', 'Taggart']] }
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should return one record" do
|
77
|
-
@values.count.must_equal 2
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should set the first name to Ellis on the first record" do
|
81
|
-
@values[0].first_name.must_equal 'Ellis'
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should set the last name to Wyatt on the first record" do
|
85
|
-
@values[0].last_name.must_equal 'Wyatt'
|
86
|
-
end
|
24
|
+
describe "an array with two symbols" do
|
25
|
+
before do
|
26
|
+
@result = [:first_name, :last_name].to_object
|
27
|
+
end
|
87
28
|
|
88
|
-
|
89
|
-
|
90
|
-
|
29
|
+
it "should return an object" do
|
30
|
+
@result.nil?.must_equal false
|
31
|
+
end
|
91
32
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
33
|
+
it "should have both properties set" do
|
34
|
+
@result.respond_to?(:first_name).must_equal true
|
35
|
+
@result.respond_to?(:first_name=).must_equal true
|
36
|
+
@result.respond_to?(:last_name).must_equal true
|
37
|
+
@result.respond_to?(:last_name=).must_equal true
|
96
38
|
end
|
97
39
|
end
|
98
40
|
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "arrays to objects" do
|
4
|
+
|
5
|
+
[:name, :street].each do |property|
|
6
|
+
describe "with an array with one symbol named #{property.to_s}" do
|
7
|
+
describe "and no data" do
|
8
|
+
before do
|
9
|
+
@values = [property].to_objects { [] }
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return an empty array" do
|
13
|
+
@values.must_be_empty
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
["Dangy", "Taggart"].each do |name|
|
18
|
+
describe "and one record with #{name}" do
|
19
|
+
before do
|
20
|
+
@values = [property].to_objects { [name] }
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return one record" do
|
24
|
+
@values.count.must_equal 1
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should have a property with the name of Dangy" do
|
28
|
+
@values[0].send(property).must_equal name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "and two records with John and Howard" do
|
34
|
+
before do
|
35
|
+
@values = [property].to_objects {["John", "Howard"]}
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return two records" do
|
39
|
+
@values.count.must_equal 2
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should set the first to John" do
|
43
|
+
@values[0].send(property).must_equal "John"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should set the second to Howard" do
|
47
|
+
@values[1].send(property).must_equal "Howard"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "with an array with two symbols named first_name and last_name" do
|
53
|
+
describe "with 'Ellis' and 'Wyatt'" do
|
54
|
+
before do
|
55
|
+
@values = [:first_name, :last_name].to_objects { [['Ellis', 'Wyatt']] }
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return one record" do
|
59
|
+
@values.count.must_equal 1
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should set the first name to Ellis" do
|
63
|
+
@values[0].first_name.must_equal 'Ellis'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should set the last name to Wyatt" do
|
67
|
+
@values[0].last_name.must_equal 'Wyatt'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "with 'Ellis' and 'Wyatt', then 'Dagny' and 'Taggart'" do
|
72
|
+
before do
|
73
|
+
@values = [:first_name, :last_name].to_objects { [['Ellis', 'Wyatt'], ['Dagny', 'Taggart']] }
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return one record" do
|
77
|
+
@values.count.must_equal 2
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should set the first name to Ellis on the first record" do
|
81
|
+
@values[0].first_name.must_equal 'Ellis'
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should set the last name to Wyatt on the first record" do
|
85
|
+
@values[0].last_name.must_equal 'Wyatt'
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should set the first name to Dagny on the second record" do
|
89
|
+
@values[1].first_name.must_equal 'Dagny'
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should set the last name to Wyatt on the second record" do
|
93
|
+
@values[1].last_name.must_equal 'Taggart'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subtle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: blankslate
|
@@ -75,7 +75,7 @@ files:
|
|
75
75
|
- README.md
|
76
76
|
- Rakefile
|
77
77
|
- lib/subtle.rb
|
78
|
-
- lib/subtle/
|
78
|
+
- lib/subtle/array_to_objects.rb
|
79
79
|
- lib/subtle/cover.rb
|
80
80
|
- lib/subtle/lambda_to_object.rb
|
81
81
|
- lib/subtle/lazy_cover.rb
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- lib/subtle/version.rb
|
85
85
|
- spec/spec_helper.rb
|
86
86
|
- spec/subtle/array_to_object_spec.rb
|
87
|
+
- spec/subtle/array_to_objects_spec.rb
|
87
88
|
- spec/subtle/cover_spec.rb
|
88
89
|
- spec/subtle/lambda_to_object_spec.rb
|
89
90
|
- spec/subtle/lazy_cover_spec.rb
|
@@ -104,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
105
|
version: '0'
|
105
106
|
segments:
|
106
107
|
- 0
|
107
|
-
hash:
|
108
|
+
hash: -1455677586639048133
|
108
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
110
|
none: false
|
110
111
|
requirements:
|
@@ -113,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
114
|
version: '0'
|
114
115
|
segments:
|
115
116
|
- 0
|
116
|
-
hash:
|
117
|
+
hash: -1455677586639048133
|
117
118
|
requirements: []
|
118
119
|
rubyforge_project:
|
119
120
|
rubygems_version: 1.8.24
|
@@ -123,6 +124,7 @@ summary: A few small updates to make even more concise Ruby.
|
|
123
124
|
test_files:
|
124
125
|
- spec/spec_helper.rb
|
125
126
|
- spec/subtle/array_to_object_spec.rb
|
127
|
+
- spec/subtle/array_to_objects_spec.rb
|
126
128
|
- spec/subtle/cover_spec.rb
|
127
129
|
- spec/subtle/lambda_to_object_spec.rb
|
128
130
|
- spec/subtle/lazy_cover_spec.rb
|