traitorous 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +42 -0
- data/VERSION +1 -1
- data/lib/traitorous/converter/method_keyed_uniform_hash.rb +8 -8
- data/lib/traitorous/converter/uniform_array.rb +4 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 147aa556988fc13509f445eb0887d4d943c6384b
|
4
|
+
data.tar.gz: 3eeb33815f1a8bd1c9f3ee83aa19961c053b15a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0613a5b93cacd2a046d2ae996b606adcbc1051421ddec3e1b3b88906927816e1b0daa3c15618990f74a8f5fe50af7b8fe6cacb9f8048270ea7562f221459932e
|
7
|
+
data.tar.gz: 05c16f0da36240e35a124d086f10c0860027d60128b9137c6e09bc1e86fb50985cae32dff2ed937bfcc28ab4bbf03689fc2a392bcc840f1131e6905c8c37e603
|
data/README.md
CHANGED
@@ -86,6 +86,48 @@ puts Area.new(area.export) == area
|
|
86
86
|
# true
|
87
87
|
```
|
88
88
|
|
89
|
+
## Converters
|
90
|
+
The purpose of the converters are to facilitate the importation of simple
|
91
|
+
JSON or YAML data and import that data into an arbitrarily nested tree
|
92
|
+
of objects. And then to take those object and be able to export that data
|
93
|
+
in a simple form ready to save.
|
94
|
+
|
95
|
+
This system should be flexible enough to account for an large variety of data
|
96
|
+
structures that can be read in and out of storage easily and in 1 tree.
|
97
|
+
|
98
|
+
#### Traitorous::Converter::Identity
|
99
|
+
This converter is meant as a pass through converter that doesn't alter the
|
100
|
+
incoming value on either do_import or do_export.
|
101
|
+
|
102
|
+
#### Traitorous::Converter::DefaultValueStatic
|
103
|
+
THis converter is similar to the Traitorous::Converter::Identity except that
|
104
|
+
when doing do_import it will return a default value if the opts are nil or
|
105
|
+
false.
|
106
|
+
|
107
|
+
#### Traitorous::Converter::Model
|
108
|
+
This converter takes a model_klass argument and on do_import, will instantiate
|
109
|
+
a new object of that class passing in the opts as params. do_export calls
|
110
|
+
.export on the object and returns the result.
|
111
|
+
|
112
|
+
#### Traitorous::Converter::UniformArray
|
113
|
+
This converter takes a uniform_klass argument. It expects an array as input for
|
114
|
+
.do_import and will instantiate an uniform_klass object for each element of the
|
115
|
+
array and return the resulting array.
|
116
|
+
|
117
|
+
#### Traitorous::Converter::MethodKeyedUniformHash
|
118
|
+
This converter takes key_method and uniform_class arguments. It expects an
|
119
|
+
array as input for .do_import and will instantiate an uniform_klass object for
|
120
|
+
each element of the array, then call key_method on that object and add them
|
121
|
+
to a hash as a key instance pair.
|
122
|
+
|
123
|
+
### More Converters
|
124
|
+
|
125
|
+
more intelligent conversions? Expand the model, array and hash converters to
|
126
|
+
accept an override to instantiating with ::new to allow for more flexibility in
|
127
|
+
usage. This especially would be important if you wanted to import a list of
|
128
|
+
object that represents different klasses that are given with a sub_type or
|
129
|
+
sub_class attributes that are part of the do_import data.
|
130
|
+
|
89
131
|
## Roadmap
|
90
132
|
|
91
133
|
1. Add better documentation
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
@@ -7,26 +7,26 @@ module Traitorous
|
|
7
7
|
# Exported data will be converted into an array calling do_export on each
|
8
8
|
# element
|
9
9
|
class MethodKeyedUniformHash
|
10
|
-
attr_accessor :key_method, :
|
11
|
-
# @param key_method [Symbol] the method to call on the
|
10
|
+
attr_accessor :key_method, :uniform_klass
|
11
|
+
# @param key_method [Symbol] the method to call on the uniform_klass instance
|
12
12
|
# to generate the key in the returned hash
|
13
|
-
# @param
|
13
|
+
# @param uniform_klass [Class, #new] the class to instantiate with each
|
14
14
|
# element of the do_import array
|
15
|
-
def initialize(key_method,
|
15
|
+
def initialize(key_method, uniform_klass)
|
16
16
|
@key_method = key_method
|
17
|
-
@
|
17
|
+
@uniform_klass = uniform_klass
|
18
18
|
end
|
19
19
|
|
20
20
|
# The import instantiates each element of the array as an instance of
|
21
|
-
# the
|
21
|
+
# the uniform_klass, the key is determined by calling key_method on the
|
22
22
|
# instance and then they are joined to the result hash as a key,
|
23
23
|
# instance pair.
|
24
24
|
# @param arr_data [Array] the array of data to instantiate
|
25
25
|
# @return [Hash] hash containing key, instance pairs
|
26
26
|
def do_import(arr_data)
|
27
27
|
out = {}
|
28
|
-
arr_data.each do |elem_data|
|
29
|
-
obj =
|
28
|
+
Array(arr_data).each do |elem_data|
|
29
|
+
obj = uniform_klass.new(elem_data)
|
30
30
|
out[obj.send(key_method)] = obj
|
31
31
|
end
|
32
32
|
out
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module Traitorous
|
2
2
|
module Converter
|
3
3
|
class UniformArray
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(uniform_klass)
|
5
|
+
@uniform_klass ||= uniform_klass
|
6
6
|
end
|
7
7
|
def do_export(data_arr)
|
8
|
-
data_arr.map(&:export)
|
8
|
+
Array(data_arr).map(&:export)
|
9
9
|
end
|
10
10
|
|
11
11
|
def do_import(opts_arr)
|
12
|
-
opts_arr.map{|d| @
|
12
|
+
Array(opts_arr).map{|d| @uniform_klass.new(d)}
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|