thyng 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -8
- data/lib/thyng.rb +1 -0
- data/lib/thyng/crypted_aspect.rb +22 -0
- data/lib/thyng/version.rb +1 -1
- data/test/crypted/crypted_test.rb +34 -0
- data/test/test_config.rb +7 -1
- data/thyng.gemspec +6 -14
- metadata +39 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0b5dc2a14fbbdd60ebf9ccabf39ba7941135625
|
4
|
+
data.tar.gz: 68e9d08bda758ec2cccb6757a000aaaa87b81f69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b0163c7ad518c69df3795cdc8389b74892722a523cc8db8b75d64b5ceae86f10fd9efcf1ee0b6c73ee6e4d143f8a3c1437a8e1686a0e3230b87eb5a8fd8b98f
|
7
|
+
data.tar.gz: 27f6f95c63327a67fa76ca4d190b95cfbc31baf80cdd36eff8f54d371941e2758a98fb650d53d4e648492490d717db3969b8ed74f53c866ffe0814efd644510c
|
data/README.md
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
|
3
3
|
##### Ruby objects with public state to encourage storage as data only collections.
|
4
4
|
|
5
|
-
|
5
|
+
#### What?
|
6
6
|
A `thyng` is a ruby object with the convention of saving state in **key:value** pairs that are public on the object rather than in private **instance variables**.
|
7
7
|
|
8
|
-
|
8
|
+
#### How?
|
9
9
|
Instead of private attributes a `thyng` has public aspects. Basic access to these can be declared with `asect_reader`, `aspect_write` & `aspect_accessor`.
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
#### Why?
|
12
|
+
With all state external a `thyng` can be reconstructed completly from its data. This allows it to be passed as JSON or easily stored in a database.
|
13
13
|
|
14
|
-
|
14
|
+
#### Simple example
|
15
15
|
|
16
16
|
```ruby
|
17
17
|
class Person < Thyng
|
@@ -27,7 +27,7 @@ puts person
|
|
27
27
|
# => {name: 'Fester'}
|
28
28
|
```
|
29
29
|
|
30
|
-
|
30
|
+
#### Show me the money
|
31
31
|
*Thyng objects are subclassed from ruby hashes. This means most database orm's know exactly how handle them*
|
32
32
|
|
33
33
|
```ruby
|
@@ -37,6 +37,8 @@ class Person < Thyng
|
|
37
37
|
end
|
38
38
|
|
39
39
|
class Credentials < Thyng
|
40
|
+
extend Thyng::CryptedAspect
|
41
|
+
|
40
42
|
aspect_accessor :email
|
41
43
|
crypted_accessor :password # <- Oooh!
|
42
44
|
end
|
@@ -70,11 +72,49 @@ Or install it yourself as:
|
|
70
72
|
|
71
73
|
## Usage
|
72
74
|
|
73
|
-
|
75
|
+
#### Plain aspects
|
76
|
+
*See introduction Example*
|
77
|
+
|
78
|
+
#### Crypted aspects
|
79
|
+
To use this you must first add the module to thyng or your subclass of thyng. This will add the `crypted_accessor` method to your class.
|
80
|
+
|
81
|
+
`crypted_accessor` takes an aspect and adds five methods to you class. These are reader and writer methods for both the Bcrypt object and raw crypted string, as well as a check_aspect method
|
82
|
+
|
83
|
+
**Example**
|
84
|
+
```ruby
|
85
|
+
class Credentials < Thyng
|
86
|
+
extend Thyng::CryptedAspect
|
87
|
+
|
88
|
+
aspect_accessor :email
|
89
|
+
crypted_accessor :password
|
90
|
+
end
|
91
|
+
|
92
|
+
credentials = Credentials.new email: 'test@example.com', password: 'password'
|
93
|
+
# => {:email=>"test@example.com", "crypted_password"=>"$2a$10$Bw4qH9Hp3iMG7c97SIBgJ.ahpXbL8M95FyDU7O.UHo4zgnxcm3bBi"}
|
94
|
+
|
95
|
+
credentials.password.class
|
96
|
+
# => BCrypt::Password
|
97
|
+
|
98
|
+
credentials.crypted_password.class
|
99
|
+
# => BCrypt::Password
|
100
|
+
|
101
|
+
credentials.check_password? 'password'
|
102
|
+
# => true
|
103
|
+
|
104
|
+
password_hash = BCrypt::Password.create('secret')
|
105
|
+
# => "$2a$10$O5DChGjqYxAU7yxo/J9.7uK55XPeWQxP0hU7nPYQNE85yDbl3H3I6"
|
106
|
+
|
107
|
+
credentials.crypted_password = password_hash
|
108
|
+
# => "$2a$10$O5DChGjqYxAU7yxo/J9.7uK55XPeWQxP0hU7nPYQNE85yDbl3H3I6"
|
109
|
+
|
110
|
+
credentials.check_password? 'secret'
|
111
|
+
# => true
|
112
|
+
|
113
|
+
```
|
74
114
|
|
75
115
|
## Contributing
|
76
116
|
|
77
|
-
1. Fork it ( https://github.com/
|
117
|
+
1. Fork it ( https://github.com/CrowdHailer/thyng/fork )
|
78
118
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
119
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
120
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/thyng.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'bcrypt'
|
2
|
+
|
3
|
+
class Thyng < Hash
|
4
|
+
module CryptedAspect
|
5
|
+
def crypted_accessor(aspect)
|
6
|
+
crypted_aspect = "crypted_#{aspect}"
|
7
|
+
aspect_accessor crypted_aspect
|
8
|
+
|
9
|
+
define_method "#{aspect}=", ->(value) {
|
10
|
+
send "#{crypted_aspect}=", BCrypt::Password.create(value)
|
11
|
+
}
|
12
|
+
|
13
|
+
define_method aspect, -> () {
|
14
|
+
BCrypt::Password.new send(crypted_aspect)
|
15
|
+
}
|
16
|
+
|
17
|
+
define_method "check_#{aspect}?", -> (test_value) {
|
18
|
+
send(aspect) == test_value
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/thyng/version.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../test_config'
|
2
|
+
|
3
|
+
class CryptedTest < MiniTest::Test
|
4
|
+
def password
|
5
|
+
'password'
|
6
|
+
end
|
7
|
+
|
8
|
+
def crypted_example
|
9
|
+
Class.new Thyng do
|
10
|
+
extend Thyng::CryptedAspect
|
11
|
+
crypted_accessor :password
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_creates_writer_to_crypted_value
|
16
|
+
example = crypted_example.new
|
17
|
+
assert example.respond_to?('crypted_password='), 'Should be able to write crypted value'
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_creates_reader_to_crypted_value
|
21
|
+
example = crypted_example.new
|
22
|
+
assert example.respond_to?('crypted_password'), 'Should be able to read crypted value'
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_aspect_is_saved_encrypted
|
26
|
+
example = crypted_example.new password: password
|
27
|
+
assert_equal BCrypt::Password, example.password.class
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_can_check_against_encrypted_value
|
31
|
+
example = crypted_example.new password: password
|
32
|
+
assert_equal true, example.check_password?(password)
|
33
|
+
end
|
34
|
+
end
|
data/test/test_config.rb
CHANGED
@@ -1,2 +1,8 @@
|
|
1
1
|
require 'thyng'
|
2
|
-
require 'minitest/autorun'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'minitest/reporters'
|
4
|
+
|
5
|
+
reporter_options = {color: true, slow_count: 5}
|
6
|
+
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(reporter_options)]
|
7
|
+
|
8
|
+
BCrypt::Engine.cost = 1
|
data/thyng.gemspec
CHANGED
@@ -10,21 +10,10 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["peterhsaxton@gmail.com"]
|
11
11
|
spec.summary = %q{Ruby objects with public state to encourage storage as data only collections}
|
12
12
|
spec.description = %q{
|
13
|
-
|
14
|
-
|
15
|
-
Ruby objects with public state to encourage storage as data only collections.
|
16
|
-
|
17
|
-
What?
|
18
|
-
|
19
|
-
A thyng is a ruby object with the convention of saving state in key:value pairs that are public on the object rather than in private instance variables.
|
20
|
-
|
21
|
-
How?
|
22
|
-
|
13
|
+
Create Ruby objects with public state to encourage their storage as data only collections.
|
23
14
|
Instead of private attributes a thyng has public aspects. Basic access to these can be declared with asect_reader, aspect_write & aspect_accessor.
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
Will all internal state a thyng can be reconstructed completly from its data. This allows it to be passed as JSON a json object and easily be stored in a database.
|
15
|
+
Thyng is a subclass of a plain Ruby Hash allowing merging, filtering etc of the objects aspects.
|
16
|
+
With all state external a thyng can be reconstructed completly from its data. This allows it to be passed as JSON or easily stored in a database.
|
28
17
|
}
|
29
18
|
spec.homepage = "https://github.com/CrowdHailer/Thyng"
|
30
19
|
spec.license = "MIT"
|
@@ -37,4 +26,7 @@ Gem::Specification.new do |spec|
|
|
37
26
|
spec.add_development_dependency "bundler", "~> 1.7"
|
38
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
39
28
|
spec.add_development_dependency "minitest", "~> 5.4.3"
|
29
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.0.6"
|
30
|
+
|
31
|
+
spec.add_dependency "bcrypt", "~> 3.1.7"
|
40
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thyng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Saxton
|
@@ -52,14 +52,41 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 5.4.3
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-reporters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.6
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.0.6
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bcrypt
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.1.7
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.1.7
|
83
|
+
description: "\n Create Ruby objects with public state to encourage their storage
|
84
|
+
as data only collections.\n Instead of private attributes a thyng has public
|
85
|
+
aspects. Basic access to these can be declared with asect_reader, aspect_write &
|
86
|
+
aspect_accessor.\n Thyng is a subclass of a plain Ruby Hash allowing merging,
|
87
|
+
filtering etc of the objects aspects.\n With all state external a thyng can be
|
88
|
+
reconstructed completly from its data. This allows it to be passed as JSON or easily
|
89
|
+
stored in a database.\n "
|
63
90
|
email:
|
64
91
|
- peterhsaxton@gmail.com
|
65
92
|
executables: []
|
@@ -72,12 +99,14 @@ files:
|
|
72
99
|
- README.md
|
73
100
|
- Rakefile
|
74
101
|
- lib/thyng.rb
|
102
|
+
- lib/thyng/crypted_aspect.rb
|
75
103
|
- lib/thyng/version.rb
|
76
104
|
- tasks/test.rake
|
77
105
|
- test/core/accessor_test.rb
|
78
106
|
- test/core/initializer_test.rb
|
79
107
|
- test/core/reader_test.rb
|
80
108
|
- test/core/writer_test.rb
|
109
|
+
- test/crypted/crypted_test.rb
|
81
110
|
- test/test_config.rb
|
82
111
|
- test/version/version_test.rb
|
83
112
|
- thyng.gemspec
|
@@ -110,5 +139,6 @@ test_files:
|
|
110
139
|
- test/core/initializer_test.rb
|
111
140
|
- test/core/reader_test.rb
|
112
141
|
- test/core/writer_test.rb
|
142
|
+
- test/crypted/crypted_test.rb
|
113
143
|
- test/test_config.rb
|
114
144
|
- test/version/version_test.rb
|