ostend 0.0.2.alpha.1 → 0.0.2.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +116 -0
- data/lib/ostend/core.rb +1 -0
- data/lib/ostend/version.rb +1 -1
- metadata +3 -3
- data/README +0 -13
data/README.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# We All Have Hashes
|
2
|
+
|
3
|
+
#### And at some point we just want to:
|
4
|
+
```ruby
|
5
|
+
hash = { :name => 'Twilight Sparkle', :enemy => 'Ahuizotl', :size => 'little' }
|
6
|
+
|
7
|
+
my_correctly_sized_pony = SizedPony.new(hash)
|
8
|
+
|
9
|
+
my_correctly_sized_pony.name
|
10
|
+
# => 'Twilight Sparkle'
|
11
|
+
|
12
|
+
my_correctly_sized_pony.enemy = 'Queen Chrysalis'
|
13
|
+
# => 'Queen Chrysalis'
|
14
|
+
```
|
15
|
+
|
16
|
+
We can use OpenStruct, but then we can't directly inherit from the real class!
|
17
|
+
|
18
|
+
### Why in Ponyland is that so hard?
|
19
|
+
|
20
|
+
# It's Not Hard
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class SizedPony < Pony
|
24
|
+
include Ostend
|
25
|
+
|
26
|
+
def initialize(hash)
|
27
|
+
ostendify(hash)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
Two new lines. That's it! And nothing is forced on you.
|
33
|
+
|
34
|
+
If you don't want to instantiate the variables in new(), you don't have to. You can call ostendify() anywhere you like.
|
35
|
+
|
36
|
+
## There's Always More
|
37
|
+
|
38
|
+
I have added a few additional features:
|
39
|
+
#### Accessor Type control
|
40
|
+
|
41
|
+
Be default, Ostend creates attr_accessors.
|
42
|
+
|
43
|
+
You say you'd prefer writers (or readers)? Set @ostend_attr_type before you call ostendify()
|
44
|
+
```ruby
|
45
|
+
pony.ostend_attr_type = :writer # or = :reader
|
46
|
+
pony.ostendify( hash )
|
47
|
+
|
48
|
+
pony.size
|
49
|
+
# => NoMethodError
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Attribute Filter
|
53
|
+
|
54
|
+
Whitelist hash keys for attributes that you don't want assigned
|
55
|
+
You don't want your ponies to have enemies? Set @ostend_attr_filter before you call ostendify()
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
pony.ostend_attr_filter = [:name, :size]
|
59
|
+
# Now Ostend happily and silenty drops the other hash keys
|
60
|
+
pony.ostendify( hash )
|
61
|
+
|
62
|
+
pony.enemy
|
63
|
+
# => NoMethodError
|
64
|
+
```
|
65
|
+
|
66
|
+
#### Strict Assignment
|
67
|
+
|
68
|
+
Lastly, you can force exceptions if you don't want arguments other that what you specified in the filter.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
pony.ostend_attr_filter = [:name, :size]
|
72
|
+
pony.ostend_strict = true
|
73
|
+
# Now Ostend throws a hissy fit when you attempt instantiaion
|
74
|
+
|
75
|
+
pony.ostendify( hash )
|
76
|
+
# => Exception :: The following are not allowed attributes: :enemies
|
77
|
+
```
|
78
|
+
|
79
|
+
### Still More
|
80
|
+
Combine this with ActiveRecord validations, and you can do some awesome stuff.
|
81
|
+
|
82
|
+
There are other gem combinations that could be really killer.
|
83
|
+
|
84
|
+
Let Me Know What You Discover!
|
85
|
+
|
86
|
+
## Reasoning
|
87
|
+
|
88
|
+
#### Ostend is meant to be a simple, Unix-like tool that doesn't get in your way. It helps you maintain your sanity and SRP.
|
89
|
+
|
90
|
+
###### But you can just use OpenStruct as your parent.
|
91
|
+
**Sure but then you can't inherit from what the class you really should be inheriting from.**
|
92
|
+
|
93
|
+
###### But you can create an abstraction layer between OpenStruct and your class
|
94
|
+
**Yup! Except this is easier and cleaner (imo).**
|
95
|
+
|
96
|
+
Using this as a mix in we are able to maintain a meaningful class structure while exposing the hash keys as viable and visable methods.
|
97
|
+
This helps to keep your Classes focused on what they need to do. They don't run around, crazily setting
|
98
|
+
|
99
|
+
The accessors are also created on each individual instance of your class. In this way, you can have object of the same class with different configurations.
|
100
|
+
|
101
|
+
Keep in mind, there are awesome gems like Virtus [solnic/virtus](https://github.com/solnic/virtus) that can enable a bunch of what I do here.
|
102
|
+
|
103
|
+
However, Virtus requires full attribute definition as part of it's explicit goal.
|
104
|
+
|
105
|
+
## Where Is This Going
|
106
|
+
I have a few ideas for expansion.
|
107
|
+
|
108
|
+
1) Enabling class level accessor creation
|
109
|
+
2) Create the Ostend variable as class_level variables to allow easier control over the entire class
|
110
|
+
3) I could start to get into crazy, ActiveRecord relation assignments
|
111
|
+
|
112
|
+
However, I'm not sure how far I will go as it's current form is just about the size I want it to be.
|
113
|
+
|
114
|
+
Thoughts? Comments? Is their an easier, clearer way (ActiveModel if working in rails)?
|
115
|
+
|
116
|
+
.jpg
|
data/lib/ostend/core.rb
CHANGED
data/lib/ostend/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ostend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.2.
|
4
|
+
version: 0.0.2.beta.1
|
5
5
|
prerelease: 6
|
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: 2013-02-
|
12
|
+
date: 2013-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: This is a set of tools to help instantiating objects with a hash
|
15
15
|
email:
|
@@ -20,7 +20,7 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- Gemfile
|
22
22
|
- LICENSE.txt
|
23
|
-
- README
|
23
|
+
- README.md
|
24
24
|
- Rakefile
|
25
25
|
- lib/ostend.rb
|
26
26
|
- lib/ostend/core.rb
|
data/README
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
Using this as a mix in we are able to maintain a meaningful class structure while exposing the hash keys as viable and visable methods.
|
2
|
-
I had a lot more I wanted to do in my FileCopyTask, and I didn't feel that the objectification logic was correctly placed in my class.
|
3
|
-
|
4
|
-
The really neat part about this is that you could extend an individual instance of an object and objectify it in place.
|
5
|
-
You could do all sorts of other crazy things with definition of reader/writer/accessor permissions in the objectify call.
|
6
|
-
|
7
|
-
This may be entirely useless and silly but I think it is a slightly better option than returning an object of the class Hashit/ObjectifyFromHash
|
8
|
-
|
9
|
-
OpenStruct is a viable alternative. However, I was unable to find a way to use the Plain class as a mixin.
|
10
|
-
|
11
|
-
Thoughts? Comments? Need more of the code around what I'm doing? Is their an easier, clearer way (ActiveModel if working in rails)?
|
12
|
-
|
13
|
-
.jpg
|