skn_utils 2.0.6 → 3.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cefc06c3edeecb51df5198df7b9e66f7bd6436d9
4
- data.tar.gz: cfcb3e42124bfc01175839f88ac54f97ce352b55
3
+ metadata.gz: 7bf64e0d7a57214a2195f952bb5b5ecf723210ee
4
+ data.tar.gz: 14abf27a51c90ac5617ed05533cf6cab3205e425
5
5
  SHA512:
6
- metadata.gz: 1668823c468aacec8b9dd450c0f7c3188370a48553b60285aa0aa7a66f50c2c6a29b54601b4ca75ec1f83cab30194d2ea927701f49e344ffdefbb2191b935946
7
- data.tar.gz: 523243ed0ceedf0fa67cace2ba399a075918eaefcb21689089a9031929f51c3d9ca94aff6a5e6a8c847131ecf8de478a5ce3364eb07436b9477fe368c4d25531
6
+ metadata.gz: 16506a2525da4abaa56589af2b191cb3205872e8f5983d19a97d1b83fe32b260225a5d001ebda1a2360d8363636a5c6d2b3ba7f0fa1a69cf358544f176fd170a
7
+ data.tar.gz: 25246180c86f60110a9098df8761cea4d0df6de2bd4ed66073bb58ab7e9b1dbd0bebad195992b6e11ceff9748c6d70aa633d327af31a39605b61711b81e4c6ea
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.2.3
1
+ ruby-2.4.1
data/README.md CHANGED
@@ -1,29 +1,26 @@
1
- [![Gem Version](https://badge.fury.io/rb/skn_utils.svg)](http://badge.fury.io/rb/skn_utils)
2
-
3
- # SknUtils
4
- Ruby Gem containing a Ruby PORO (Plain Old Ruby Object) that can be instantiated at runtime with an input hash. This library creates
5
- an Object with instance variables and associated getters and setters for Dot or Hash notational access to each instance variable. Additional
6
- instance variables can be added post-create by 'obj.my_new_var = "some value"', or simply assigning it.
7
-
8
1
 
9
- The intent of this gem is to be a container of data results or value bean, with easy access to its contents with on-demand transformation back to a hash (#to_hash)
10
- for easy serialization using standard ruby Hash serialization methods.
2
+ [![Gem Version](https://badge.fury.io/rb/skn_utils.svg)](http://badge.fury.io/rb/skn_utils)
11
3
 
4
+ # SknUtils
5
+ #### SknUtils::NestedResult class; dynamic key/value container
6
+ The intent of this gem is to be a container of data results or key/value pairs, with easy access to its contents, and on-demand transformation back to the hash (#to_hash).
12
7
 
13
- * Transforms the initialization hash into object instance variables, with their Keys as the method names.
14
- * If the key's value is also a hash, it too can optionally become an Object.
15
- * if the key's value is a Array of Hashes, each element of the Array can optionally become an Object.
8
+ Ruby Gem containing a Ruby PORO (Plain Old Ruby Object) that can be instantiated at runtime with an input hash. This library creates
9
+ an Object with Dot or Hash notational accessors to each key's value. Additional key/value pairs can be added post-create
10
+ by 'obj.my_new_var = "some value"', or simply assigning it.
16
11
 
17
-
18
- This nesting action is controlled by the value of the options key ':depth'.
19
- The key :depth defaults to :multi, and has options of :single, :multi, or :multi_with_arrays
20
-
21
- The ability of the resulting Object to be Marshalled(dump/load) can be preserved by merging configuration options
22
- into the input params key ':enable_serialization' set to true. It defaults to false for speed purposes
12
+ * Transforms the initialization hash into accessable object instance values, with their keys as method names.
13
+ * If the key's value is also a hash, it too will become an Object.
14
+ * if the key's value is a Array of Hashes, or Array of Arrays of Hashes, each element of the Arrays will become an Object.
15
+ * The current key/value (including nested) pairs are returned via #to_hash or #to_json when and if needed.
23
16
 
24
17
 
25
- ### New Features
26
- --------------------------------
18
+ ## New Features
19
+ 03/2017 V3.0.0
20
+ Added SknUtils::NestedResult to replace, or be an alternate, to ResultBean, GenericBean, PageControls, ValueBean, and AttributeHelper.
21
+ NestedResult overcome issues with serialization via Marshal and Yaml/Psych.
22
+ NestedResult will properly encode all hash based key/value pairs of input and decodes it via #to_h or #to_json
23
+ NestedResult encodes everything given no matter how deeply its nested, unlike the prior version where you had control over nesting.
27
24
 
28
25
  10/2016 V2.0.6
29
26
  Added an SknUtils::NullObject and SknUtils::nullable?(value) extracted from [Avdi Grimm's Confident Code](https://gist.github.com/jschoolcraft/979827)
@@ -42,20 +39,16 @@ into the input params key ':enable_serialization' set to true. It defaults to f
42
39
  Last Version to depend on Rails (ActiveModel) for #to_json and #to_xml serialization
43
40
 
44
41
 
45
- ### Configuration Options
46
- --------------------------------
47
-
48
- Include in initialization hash
49
- :enable_serialization = false -- [ true | false ], for speed, omits creation of attr_accessor
50
- :depth = :multi -- [ :single | :multi | :multi_with_arrays ]
51
-
42
+ ## Configuration Options
43
+ None required other than initialization hash
52
44
 
53
- ### Public Methods
54
- --------------------------------
55
45
 
46
+ ## Public Methods
56
47
  Each concrete Class supports the following utility methods:
57
- #to_hash -- returns a hash of all user attributes
58
- #to_hash(true) -- returns a hash of all user and internal attributes -- use for serialization
48
+ #to_hash -- returns a hash of current key/value pairs, including nested
49
+ #to_json -- returns a json string of current key/value pairs, including nested
50
+ #hash_from(:base_key) -- exports the internal hash starting with this base level key
51
+ #obj.obj2.hash_from(:base) -- exports the internal hash starting from this nested base level key
59
52
  #[] -- returns value of attr, when #[<attr_name_symbol>]
60
53
  #[]=(attr, value) -- assigns value to existing attr, or creates a new key/value pair
61
54
  #<attr>? -- detects true/false presence? of attr, and non-blank existance of attr's value; when #address?
@@ -63,45 +56,44 @@ into the input params key ':enable_serialization' set to true. It defaults to f
63
56
  #<attr> = (value) -- assigns value to existing attr, or creates a new key/value pair
64
57
  -- Where <attr> is a key value from the initial hash, or a key that was/will be dynamically added
65
58
 
66
- #depth_level -- returns parsing depth level, see :depth
67
- #serialization_required? -- returns true/false if serialization is enabled
68
- #clear_<attr> -- assigns nil to existing attr, when #clear_attr
69
-
70
59
 
71
- ### Public Components
72
- --------------------------------
60
+
61
+ ## Public Components
62
+ SknUtils::NestedResult # >= V 3.0.0 Primary Key/Value Container with Dot/Hash notiation support.
63
+
64
+
65
+ *** <= V 2.0.6 Depreciated, will be removed in next release ***
73
66
 
74
67
  Inherit from NestedResultBase or instantiate an pre-built Class:
75
- SknUtils::ResultBean # => Not Serializable and follows hash values only.
76
- SknUtils::PageControls # => Serializable and follows hash values and arrays of hashes.
77
- SknUtils::GenericBean # => Serializable and follows hash values only.
78
- SknUtils::ValueBean # => Serializable and DOES NOT follows hash values.
79
- or Include AttributeHelpers # => Add getter/setters, and hash notation access to instance vars of any object.
68
+ SknUtils::ResultBean # => Not Serializable and follows hash values only.
69
+ SknUtils::PageControls # => Serializable and follows hash values and arrays of hashes.
70
+ SknUtils::GenericBean # => Serializable and follows hash values only.
71
+ SknUtils::ValueBean # => Serializable and DOES NOT follows hash values.
72
+ or Include SknUtils::AttributeHelpers # => Adds getter/setters, and hash notation access to instance vars of any object.
80
73
 
81
74
 
82
75
  ## Basic features include:
83
76
  ```ruby
84
- - provides the hash or dot notation methods of accessing values from object created; i.e
85
- 'obj = SknUtils::ResultBean.new({value1: "some value", value2: {one: 1, two: "two"}})
86
- 'x = obj.value1' or 'x = obj.value2.one'
87
- 'x = obj["value1"]'
88
- 'x = obj[:value1]'
77
+ - provides the hash or dot notation methods of accessing values:
89
78
 
90
- - enables serialization by avoiding the use of ''singleton_class'' methods, #attr_accessor, which breaks Serializers:
91
- Serializer supports #to_hash, and standard Marshall''ing. Notice use of #to_hash to convert object back to a Ruby Hash before
92
- using #to_json and #to_xml; presumed to be methods enabled on the standard Ruby Hash class.
79
+ $ obj = SknUtils::NestedResult.new({value1: "some value", value2: {one: 1, two: "two"}})
80
+ $ x = obj.value1
81
+ $ x = obj.value2.one
82
+ $ x = obj["value1"]
83
+ $ x = obj[:value1]
93
84
 
94
- person = SknUtils::PageControls.new({name: "Bob"})
95
- person.to_hash # => {"name"=>"Bob"}
96
- person.to_hash.to_json # => "{\"name\":\"Bob\"}"
97
- person.to_hash.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<page-controls>\n <name>Bob</name>\n</page-controls>\n"
98
- dmp = Marshal.dump(person) # => "\x04\bo:\x1ASknUtils::PageControls\x06:\n@nameI\"\bBob\x06:\x06ET"
99
- person = Marshal.load(dmp) # => #<SknUtils::PageControls:0x007faede906d40 @name="Bob">
85
+ - enables serialization:
86
+ Internally supports #to_hash and #to_json
100
87
 
101
- ***GenericBean designed to automatically handles the setup for serialization and multi level without arrays
88
+ $ person = SknUtils::NestedResult.new({name: "Bob"})
89
+ $ person.to_hash # => {"name"=>"Bob"}
90
+ $ person.to_json # => "{\"name\":\"Bob\"}"
91
+ $ dmp = Marshal.dump(person) # => "\x04\bo:\x1ASknUtils::NestedResult\x06:\n@nameI\"\bBob\x06:\x06ET"
92
+ $ person2 = Marshal.load(dmp) # => #<SknUtils::NestedResult:0x007faede906d40 @name="Bob">
102
93
 
103
94
  - post create additions:
104
- 'obj = SknUtils::ResultBean.new({value1: "some value", value2: {one: 1, two: "two"}})
95
+
96
+ 'obj = SknUtils::NestedResult.new({value1: "some value", value2: {one: 1, two: "two"}})
105
97
  'x = obj.one' --causes NoMethodError
106
98
  'x = obj.one = 'some other value' --creates a new instance value with accessors
107
99
  'x = obj.one = {key1: 1, two: "two"}' --creates a new ***bean as the value of obj.one
@@ -109,130 +101,93 @@ into the input params key ':enable_serialization' set to true. It defaults to f
109
101
  'y = obj.one[:two] --returns "two"
110
102
  'y = obj.one['two'] --returns "two"
111
103
 
112
- - supports predicates <attr>? and clear_<attr>? method patterns:
113
- 'obj = SknUtils::PageControls.new({name: "Something", phone: "2609998888"})'
114
- 'obj.name?' # => true true or false, like obj.name.present?
115
- 'obj.clear_name' # => nil sets :name to nil
104
+ - supports predicates <attr>? method patterns: target must exist and have a non-empty/valid value
105
+
106
+ $ obj = SknUtils::NestedResult.new({name: "Something", active: false, phone: "2609998888"})'
107
+ $ obj.name?' # => true -- true or false, like obj.name.present?
108
+ $ obj.active? # => true -- your asking if method exist with a valid value, not what the value is!
109
+ $ obj.street? # => false
116
110
  ```
117
111
 
118
- The combination of this NestedResultBase(dot notation class) and AttributeHelpers(hash notation module), produces these effects given the same params hash:
119
112
 
120
- drb = SknUtils::ResultBean.new(params) Basic dot notation: effect of :depth
121
- ---------------------------------------------------- -----------------------------------------------------------------
113
+ ## Usage:
122
114
 
123
- (DOES NOT FOLLOW Values) :depth => :single
115
+ * The NestedResult produces these effects when given a params hash;
116
+ * Follow VALUES that are Hashes, Arrays of Hashes, and Arrays of Arrays of Hashes
124
117
  ```ruby
125
- * params = {one: 1, drb.one = 1
126
- two: { one: 1, drb.two = {one: 1, two: 'two}
127
- two: "two" drb.two.two = NoMethodError
128
- },
129
- three: [ {one: 'one', two: 2}, drb.three = [{one: 'one', two: 2},{three: 'three', four: 4}]
130
- {three: 'three', four: 4} drb.three[1] = {three: 'three', four: 4}
131
- ] drb.three[1].four = NoMethodError
132
- }
133
- ```
118
+ drb = SknUtils::NestedResult.new(params) Basic dot notation:
119
+ ---------------------------------------------------- -----------------------------------------------------------------
134
120
 
135
- (Follow VALUES that are Hashes only.) :depth => :multi
136
- ```ruby
137
121
  * params = {one: 1, drb.one = 1
138
- two: { one: 1, drb.two = <SknUtils::ResultBean>
139
- two: "two" drb.two.two = 'two'
140
- },
141
- three: [ {one: 'one', two: 2}, drb.three = [{one: 'one', two: 2},{three: 'three', four: 4}]
142
- {three: 'three', four: 4} drb.three[1] = {three: 'three', four: 4}
143
- ] drb.three[1].four = NoMethodError
144
- }
145
- ```
146
-
147
- (Follow VALUES that are Hashes and/or Arrays of Hashes) :depth => :multi_with_arrays
148
- ```ruby
149
- * params = {one: 1, drb.one = 1
150
- two: { one: 1, drb.two = <SknUtils::ResultBean>
151
- two: "two" drb.two.two = 'two'
152
- },
153
- three: [ {one: 'one', two: 2}, drb.three = [<SknUtils::ResultBean>,<SknUtils::ResultBean>]
154
- {three: 'three', four: 4} drb.three[1] = <SknUtils::ResultBean>
155
- ] drb.three[1].four = 4
122
+ two: { one: 1, two: "two"}, drb.two = <SknUtils::NestedResult>
123
+ drb.two.two = 'two'
124
+
125
+ three: [ {one: 'one', two: 2}, drb.three.first.one = 'one'
126
+ {three: 'three', four: 4} drb.three[1].four = 4
127
+ ], drb.three.last.three = 'three'
128
+
129
+ four: [
130
+ [ {one: 'one', two: 2}, drb.four.first.first.one = 'one'
131
+ {three: 'three', four: 4} ], drb.four.first.last.four = 4
132
+ [ { 5: 'five', 6: 'six'}, drb.four[1][0][5] = 'five' # number keys require hash notation :[]
133
+ {five: '5', six: 6} ] drb.four[1].last.six = 6
134
+ ],
135
+ 'five' => [1, 2, 3] drb.five = [1, 2, 3]
136
+ 6 => 'number key' drb[6] = 'number key'
156
137
  }
157
-
158
138
  ```
159
- # Usage:
160
139
 
161
- (DOES NOT FOLLOW Values)
140
+ * Expected usage
162
141
  ```ruby
163
- class SmallPackage < SknUtils::NestedResultBase
164
- def initialize(params={})
165
- super( params.merge({depth: :single}) ) # override default of :multi level
166
- end
142
+ result = SknUtils::NestedResult.new({
143
+ success: true,
144
+ message: "",
145
+ payload: {package: 'of key/value pairs from operations'}
146
+ })
147
+ ...
148
+
149
+ if result.success && result.payload.package?
150
+ # do something with result.payload
167
151
  end
168
152
  ```
169
153
 
170
- (Follow VALUES that are Hashes only.)
154
+
155
+ * Wrap additional methods around the core NestedResult feature set
171
156
  ```ruby
172
- class MyPackage < SknUtils::NestedResultBase
173
- # defaults to :multi level
174
- end
175
-
176
- -- or --
177
-
178
- class MyPackage < SknUtils::NestedResultBase
179
- def initialize(params={})
180
- # your other init stuff here
181
- super(params) # default taken
182
- end
183
- end
184
-
185
- -- or --
186
-
187
- class MyPackage < SknUtils::NestedResultBase
157
+ class MyPackage < SknUtils::NestedResult
188
158
  def initialize(params={})
189
- # your other init stuff here
190
- super( params.merge({depth: :multi}) ) # Specified
159
+ super
191
160
  end
192
- end
193
-
194
- ** - or -- enable serialization and default to multi
195
- class MyPackage < SknUtils::NestedResultBase
196
- def initialize(params={})
197
- super( params.merge({enable_serialization: true}) ) # Specified with Serialization Enabled
198
- end
199
- end
200
- ```
201
161
 
202
- (Follow VALUES that are Hashes and/or Arrays of Hashes, and enable Serializers)
203
- ```ruby
204
- class MyPackage < SknUtils::NestedResultBase
205
- def initialize(params={})
206
- super( params.merge({depth: :multi_with_arrays, enable_serialization: true}) ) # override defaults
162
+ def additional_method
163
+ # do something
207
164
  end
208
165
  end
209
166
  ```
210
167
 
211
168
 
212
- NOTE: Cannot be Marshalled/Serialized unless input params.merge({enable_serialization: true}) -- default is false
213
- Use GenericBean or PageControls if serialization is needed, they initialize with this value true.
214
-
215
169
  ## Installation
216
- ----------------
217
170
 
218
171
  runtime prereqs:
172
+ V3+ None
219
173
  V2+ None
220
174
  V1+ gem 'active_model', '~> 3.0'
221
175
 
222
- Add this line to your application's Gemfile:
223
176
 
177
+ Add this line to your application's Gemfile:
224
178
  ```ruby
225
179
  gem 'skn_utils'
226
180
  ```
227
181
 
228
- And then execute:
229
182
 
183
+ And then execute:
230
184
  $ bundle
231
185
 
232
- Or install it yourself as:
233
186
 
187
+ Or install it yourself as:
234
188
  $ gem install skn_utils
235
189
 
190
+
236
191
  ## Build
237
192
 
238
193
  1. $ git clone git@github.com:skoona/skn_utils.git
@@ -244,18 +199,22 @@ Or install it yourself as:
244
199
  7. $ gem install skn_utils
245
200
  * Done
246
201
 
202
+
247
203
  ## Console Workout
248
204
 
249
205
  Start with building gem first.
250
206
  ```bash
251
207
  $ cd skn_utils
252
- $ bundle exec pry
253
- [1] pry(main)> require 'skn_utils'
254
- [2] pry(main)> rb = SknUtils::ResultBean.new({sample: [{one: "one", two: "two"},{one: 1, two: 2}] })
255
- [3] pry(main)> pg = SknUtils::PageControls.new({sample: [{one: "one", two: "two"},{one: 1, two: 2}] })
256
- [4] pry(main)> pg.sample.first.one # Tip :multi_with_arrays
257
- [5] pry(main)> rb.sample.first.one # Tip :multi without arrays you will get a NoMethodError
258
- [6] pry(main)> rb.sample.first[:one]
208
+ $ bin/console
209
+
210
+ [1] pry(main)> rb = SknUtils::NestedResult.new({sample: [{one: "one", two: "two"},{one: 1, two: 2}] })
211
+ [2] pry(main)> pg = SknUtils::NestedResult.new({sample: [{three: 3, four: 4},{five: 'five', two: 'two'}] })
212
+ [3] pry(main)> pg.sample.first.three
213
+ [4] pry(main)> rb.sample.first.one
214
+ [5] pry(main)> rb.sample.first[:one]
215
+ [6] pry(main)> rb.hash_from(:sample)
216
+ [7] pry(main)> rb.sample?
217
+ [8] pry(main)> rb.sample[0].one?
259
218
 
260
219
  [n] pry(main)> exit
261
220
  * Done