smoke_monster 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +12 -0
- data/README.md +49 -16
- data/lib/smoke_monster/param_constructor.rb +10 -0
- data/lib/smoke_monster/version.rb +1 -1
- data/smoke_monster.gemspec +1 -0
- data/spec/smoke_monster/param_constructor_spec.rb +19 -0
- metadata +21 -6
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,29 +1,62 @@
|
|
1
1
|
# SmokeMonster
|
2
2
|
|
3
|
-
|
3
|
+
### Arrays to Objects
|
4
4
|
|
5
|
-
|
5
|
+
This feature exists to make the creation of sets of objects with data easily.
|
6
6
|
|
7
|
-
|
7
|
+
* Create an array of symbols of symbols that match the properties on the objects you want, and
|
8
|
+
* Pass a block that returns an array of arrays with matching data.
|
8
9
|
|
9
|
-
|
10
|
+
````ruby
|
11
|
+
records = [:first_name, :last_name].to_objects {
|
12
|
+
[
|
13
|
+
["John", "Galt"],
|
14
|
+
["Howard", "Roark"]
|
15
|
+
["Dagny", "Taggart"]
|
16
|
+
]}
|
10
17
|
|
11
|
-
|
18
|
+
records[0].first_name # "John"
|
19
|
+
records[0].last_name # "Galt"
|
12
20
|
|
13
|
-
|
21
|
+
records[1].first_name # "Howard"
|
22
|
+
records[1].last_name # "Roark"
|
14
23
|
|
15
|
-
|
24
|
+
records[2].first_name # "Dagny"
|
25
|
+
records[2].last_name # "Taggart"
|
26
|
+
````
|
16
27
|
|
17
|
-
|
28
|
+
### Safety Proc
|
18
29
|
|
19
|
-
|
30
|
+
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.
|
20
31
|
|
21
|
-
|
32
|
+
To me, this is most useful in imports or other code where I might want to check to run a small block of code that
|
22
33
|
|
23
|
-
|
34
|
+
````ruby
|
35
|
+
person.name = document.at_xpath('./h1').text
|
36
|
+
|
37
|
+
# if this call fails then we will move on
|
38
|
+
-> { person.bio = document.xpath('./div[@class="bio_info"]//span') }.call_safely
|
39
|
+
|
40
|
+
# if this call fails then the second block will be called
|
41
|
+
-> { person.special = document.xpath('./div[@class="active"]//a')[1].text == "special" }.call_safely { person.special = false }
|
42
|
+
````
|
43
|
+
|
44
|
+
### Param Constructor
|
45
|
+
|
46
|
+
One thing I liked about C# was the ability to instantiate my objects like this:
|
47
|
+
|
48
|
+
````c#
|
49
|
+
var person = new Person() { FirstName = "John", LastName = "Galt" };
|
50
|
+
````
|
51
|
+
|
52
|
+
This syntax is not built into Ruby syntax today, but it does exist in Rails models. So I took that idea from Rails and wrote an implementation that works like this:
|
53
|
+
|
54
|
+
````ruby
|
55
|
+
class Person
|
56
|
+
params_constructor
|
57
|
+
attr_accessor :first_name, :last_name
|
58
|
+
end
|
59
|
+
|
60
|
+
person = Person.new { first_name: "John", last_name: "Galt" }
|
61
|
+
````
|
24
62
|
|
25
|
-
1. Fork it
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|
data/smoke_monster.gemspec
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
class ParamConstructorTest
|
4
|
+
param_constructor
|
5
|
+
attr_accessor :first_name, :last_name
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "param_constructor" do
|
9
|
+
it "should let the object be instantiated with a hash" do
|
10
|
+
test = ParamConstructorTest.new(first_name: "John", last_name: "Galt")
|
11
|
+
test.first_name.must_equal "John"
|
12
|
+
test.last_name.must_equal "Galt"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should allow the object to be instantiated with no params" do
|
16
|
+
test = ParamConstructorTest.new
|
17
|
+
# should not throw an error
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smoke_monster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: blankslate
|
16
|
-
requirement: &
|
16
|
+
requirement: &70329318266900 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70329318266900
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mocha
|
27
|
-
requirement: &
|
27
|
+
requirement: &70329318266260 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,18 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70329318266260
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70329318265640 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70329318265640
|
36
47
|
description: Unexplained, dark magic.
|
37
48
|
email:
|
38
49
|
- darren@cauthon.com
|
@@ -41,6 +52,7 @@ extensions: []
|
|
41
52
|
extra_rdoc_files: []
|
42
53
|
files:
|
43
54
|
- .gitignore
|
55
|
+
- .travis.yml
|
44
56
|
- Gemfile
|
45
57
|
- Guardfile
|
46
58
|
- LICENSE
|
@@ -51,6 +63,7 @@ files:
|
|
51
63
|
- lib/smoke_monster/cover.rb
|
52
64
|
- lib/smoke_monster/lambda_to_object.rb
|
53
65
|
- lib/smoke_monster/lazy_cover.rb
|
66
|
+
- lib/smoke_monster/param_constructor.rb
|
54
67
|
- lib/smoke_monster/safety_proc.rb
|
55
68
|
- lib/smoke_monster/version.rb
|
56
69
|
- smoke_monster.gemspec
|
@@ -58,6 +71,7 @@ files:
|
|
58
71
|
- spec/smoke_monster/cover_spec.rb
|
59
72
|
- spec/smoke_monster/lambda_to_object_spec.rb
|
60
73
|
- spec/smoke_monster/lazy_cover_spec.rb
|
74
|
+
- spec/smoke_monster/param_constructor_spec.rb
|
61
75
|
- spec/smoke_monster/safety_proc_spec.rb
|
62
76
|
- spec/spec_helper.rb
|
63
77
|
homepage: http://www.github.com/darrencauthon/smoke_monster
|
@@ -89,5 +103,6 @@ test_files:
|
|
89
103
|
- spec/smoke_monster/cover_spec.rb
|
90
104
|
- spec/smoke_monster/lambda_to_object_spec.rb
|
91
105
|
- spec/smoke_monster/lazy_cover_spec.rb
|
106
|
+
- spec/smoke_monster/param_constructor_spec.rb
|
92
107
|
- spec/smoke_monster/safety_proc_spec.rb
|
93
108
|
- spec/spec_helper.rb
|