attribute_struct 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +32 -0
- data/lib/attribute_struct/attribute_hash.rb +4 -1
- data/lib/attribute_struct/attribute_struct.rb +109 -21
- data/lib/attribute_struct/augmented.rb +26 -0
- data/lib/attribute_struct/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62a1d8f5b662295b8d042e63ab3db10675590f8c
|
4
|
+
data.tar.gz: c6aa3dff93e00ac5af116d7ad1f181fb92277894
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17a711517062fe06ab4af95af64dcb4b45bc9d8050a204d42767f63e712e39552c07a2ec7915bb465e9ff9c20b9ce50447e7a870d01ad6c0bc9ab34d51977044
|
7
|
+
data.tar.gz: 2367c34c291b7c8a7f0e79f75782f08ce80b4d065aa0319e81c798c60ca656aa0e86d6fd719beda54b90af8df932646592c77bf299f0f39b3522cb1b169f00e3
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -244,6 +244,38 @@ end.dump!
|
|
244
244
|
# => {"This"=>{"Is"=>{"A"=>{"Deeply"=>{"NestedCamel"=>{"Hash"=>true}}}}},"horse" => true}
|
245
245
|
```
|
246
246
|
|
247
|
+
### Augmented AttributeStruct
|
248
|
+
|
249
|
+
An augmented version of the AttributeStruct is available that includes
|
250
|
+
the `Kernel` module to provide many common methods. This can reduce the
|
251
|
+
usability of the struct by defining methods that may collide with desired
|
252
|
+
key entries. It is important to be aware of this when using the augmented
|
253
|
+
version. To create an augmented version:
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
require 'attribute_struct'
|
257
|
+
|
258
|
+
AttributeStruct::Augmented.new do
|
259
|
+
rand_value rand
|
260
|
+
end.dump!
|
261
|
+
|
262
|
+
# => {"rand_value"=>0.7983473046826768}
|
263
|
+
```
|
264
|
+
|
265
|
+
Kernel methods can also be injected into raw AttributeStruct instance. Again,
|
266
|
+
caution must be used to prevent unintended collisions:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
require 'attribute_struct'
|
270
|
+
|
271
|
+
AttributeStruct.new do
|
272
|
+
kernelify!
|
273
|
+
rand_value rand
|
274
|
+
end.dump!
|
275
|
+
|
276
|
+
# => {"rand_value"=>0.24500702285393017}
|
277
|
+
```
|
278
|
+
|
247
279
|
## In the wild
|
248
280
|
|
249
281
|
Libraries utilizing AttributeStruct:
|
@@ -185,7 +185,10 @@ class AttributeStruct
|
|
185
185
|
# @return [Hash] The mash as a Hash with symbolized keys.
|
186
186
|
def symbolize_keys
|
187
187
|
h = Hash.new(default)
|
188
|
-
each
|
188
|
+
each do |key, val|
|
189
|
+
key = key.to_sym if key.is_a?(String) || key.is_a?(Symbol)
|
190
|
+
h[key] = val
|
191
|
+
end
|
189
192
|
h
|
190
193
|
end
|
191
194
|
|
@@ -2,6 +2,7 @@ require 'attribute_struct'
|
|
2
2
|
|
3
3
|
class AttributeStruct < BasicObject
|
4
4
|
|
5
|
+
autoload :Augmented, 'attribute_struct/augmented'
|
5
6
|
autoload :Mash, 'attribute_struct/attribute_hash'
|
6
7
|
|
7
8
|
class << self
|
@@ -105,6 +106,7 @@ class AttributeStruct < BasicObject
|
|
105
106
|
def initialize(init_hash=nil, &block)
|
106
107
|
@_camel_keys = _klass.camel_keys
|
107
108
|
@_arg_state = __hashish.new
|
109
|
+
@_objectified = false
|
108
110
|
@table = __hashish.new
|
109
111
|
if(init_hash)
|
110
112
|
_load(init_hash)
|
@@ -165,6 +167,20 @@ class AttributeStruct < BasicObject
|
|
165
167
|
@_camel_style = ::AttributeStruct.validate_camel_style(val)
|
166
168
|
end
|
167
169
|
|
170
|
+
# Enable/disable root constant lookups
|
171
|
+
#
|
172
|
+
# @param enable [TrueClass, FalseClass]
|
173
|
+
# @return [TrueClass, FalseClass]
|
174
|
+
def _objectify(enable=true)
|
175
|
+
@_objectified = !!enable
|
176
|
+
end
|
177
|
+
alias_method :objectify!, :_objectify
|
178
|
+
|
179
|
+
# @return [TrueClass, FalseClass]
|
180
|
+
def objectified?
|
181
|
+
@_objectified
|
182
|
+
end
|
183
|
+
|
168
184
|
# Direct data access
|
169
185
|
#
|
170
186
|
# @param key [String, Symbol]
|
@@ -197,11 +213,17 @@ class AttributeStruct < BasicObject
|
|
197
213
|
# @return [Object] existing value or newly set value
|
198
214
|
# @note Dragons and unicorns all over in here
|
199
215
|
def method_missing(_sym, *_args, &_block)
|
200
|
-
if(
|
201
|
-
|
202
|
-
|
216
|
+
if(objectified? && _args.empty? && _block.nil?)
|
217
|
+
_o_lookup = _objectified_constant_lookup(_sym)
|
218
|
+
return _o_lookup if _o_lookup
|
219
|
+
end
|
220
|
+
if(_sym.is_a?(::String) || _sym.is_a?(::Symbol))
|
221
|
+
if((_s = _sym.to_s).end_with?('='))
|
222
|
+
_s.slice!(-1, _s.length)
|
223
|
+
_sym = _s
|
224
|
+
end
|
225
|
+
_sym = _process_key(_sym)
|
203
226
|
end
|
204
|
-
_sym = _process_key(_sym)
|
205
227
|
if(!_args.empty? || _block)
|
206
228
|
if(_args.empty? && _block)
|
207
229
|
_base = @table.fetch(_sym, UNSET_VALUE)
|
@@ -472,23 +494,27 @@ class AttributeStruct < BasicObject
|
|
472
494
|
# @param args [Object] argument list (:force will force processing)
|
473
495
|
# @return [String, Symbol]
|
474
496
|
def _process_key(key, *args)
|
475
|
-
key
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
497
|
+
if(key.is_a?(::String) || key.is_a?(::Symbol))
|
498
|
+
key = ::CamelString.new(key.to_s)
|
499
|
+
if(_camel_keys && _camel_keys_action && !key._hump_format_requested?)
|
500
|
+
case _camel_keys_action
|
501
|
+
when :auto_disable
|
502
|
+
key._no_hump
|
503
|
+
when :auto_enable
|
504
|
+
key._hump
|
505
|
+
end
|
482
506
|
end
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
507
|
+
if(_camel_keys && (key._camel? || args.include?(:force)))
|
508
|
+
camel_args = [key]
|
509
|
+
if(key._hump_style || _camel_style == :no_leading)
|
510
|
+
unless(key._hump_style == :leading_hump)
|
511
|
+
camel_args << false
|
512
|
+
end
|
489
513
|
end
|
514
|
+
::Bogo::Utility.camel(*camel_args)
|
515
|
+
else
|
516
|
+
key
|
490
517
|
end
|
491
|
-
::Bogo::Utility.camel(*camel_args)
|
492
518
|
else
|
493
519
|
key
|
494
520
|
end
|
@@ -499,9 +525,13 @@ class AttributeStruct < BasicObject
|
|
499
525
|
def _klass
|
500
526
|
::AttributeStruct
|
501
527
|
end
|
502
|
-
|
503
|
-
|
504
|
-
|
528
|
+
|
529
|
+
# @return [Class] this clas
|
530
|
+
def klass!
|
531
|
+
_klass
|
532
|
+
end
|
533
|
+
alias_method :class!, :klass!
|
534
|
+
alias_method :class, :klass!
|
505
535
|
|
506
536
|
# @return [AttributeStruct] new struct instance
|
507
537
|
# @note will set self as parent and propogate camelizing status
|
@@ -512,6 +542,8 @@ class AttributeStruct < BasicObject
|
|
512
542
|
end
|
513
543
|
n._camel_keys = _camel_keys
|
514
544
|
n._camel_style = _camel_style if _camel_style
|
545
|
+
n._objectify if objectified?
|
546
|
+
n._kernelify if kernelified?
|
515
547
|
n._parent(self)
|
516
548
|
n
|
517
549
|
end
|
@@ -576,6 +608,62 @@ class AttributeStruct < BasicObject
|
|
576
608
|
_klass.instance_methods.map(&:to_sym).include?(name.to_sym)
|
577
609
|
end
|
578
610
|
|
611
|
+
# Lookup constant in root namespace
|
612
|
+
#
|
613
|
+
# @param konst [Symbol, String]
|
614
|
+
# @return [Object, NilClass]
|
615
|
+
def _objectified_constant_lookup(konst)
|
616
|
+
if(konst.to_s[0].match(/[A-Z]/) && ::Object.const_defined?(konst))
|
617
|
+
::Object.const_get(konst)
|
618
|
+
end
|
619
|
+
end
|
620
|
+
|
621
|
+
# Inject Kernel methods
|
622
|
+
#
|
623
|
+
# @return [TrueClass]
|
624
|
+
def _kernelify
|
625
|
+
unless(kernelified?)
|
626
|
+
@_kernelified = true
|
627
|
+
(::Kernel.public_instance_methods + ::Kernel.private_instance_methods).each do |m_name|
|
628
|
+
self.instance_eval("def #{m_name}(*a, &b); ::Kernel.instance_method(:#{m_name}).bind(self).curry.call(*a, &b); end")
|
629
|
+
end
|
630
|
+
end
|
631
|
+
true
|
632
|
+
end
|
633
|
+
alias_method :kernelify!, :_kernelify
|
634
|
+
|
635
|
+
# @return [TrueClass, FalseClass] Kernel methods have been injected
|
636
|
+
def kernelified?
|
637
|
+
!!@_kernelified
|
638
|
+
end
|
639
|
+
|
640
|
+
# @return [Numeric]
|
641
|
+
def hash
|
642
|
+
::Kernel.instance_method(:hash).bind(self).curry.call
|
643
|
+
end
|
644
|
+
|
645
|
+
# @return [AttributeStruct] clone of current instance
|
646
|
+
def _clone(_new_parent=nil)
|
647
|
+
_cloned_inst = _klass_new
|
648
|
+
_cloned_inst.__table = __hashish[
|
649
|
+
@table.map{ |_key, _value|
|
650
|
+
if(_key.is_a?(::AttributeStruct))
|
651
|
+
_key = _key._clone
|
652
|
+
else
|
653
|
+
_key = _do_dup(_key)
|
654
|
+
end
|
655
|
+
if(_value.is_a?(::AttributeStruct))
|
656
|
+
_value = _value._clone
|
657
|
+
else
|
658
|
+
_value = _do_dup(_value)
|
659
|
+
end
|
660
|
+
[_key, _value]
|
661
|
+
}
|
662
|
+
]
|
663
|
+
_cloned_inst._parent(_new_parent) if _new_parent
|
664
|
+
_cloned_inst
|
665
|
+
end
|
666
|
+
alias_method :clone!, :_clone
|
579
667
|
end
|
580
668
|
|
581
669
|
require 'attribute_struct/attribute_hash'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'attribute_struct'
|
2
|
+
|
3
|
+
class AttributeStruct
|
4
|
+
# AttributeStruct expanded class that include the Kernel module
|
5
|
+
# and automatically objectifies the instance
|
6
|
+
class Augmented < ::AttributeStruct
|
7
|
+
|
8
|
+
include ::Kernel
|
9
|
+
|
10
|
+
# Create a new Augmented AttributeStruct instance. Passes arguments
|
11
|
+
# and block directly to parent for initialization. Automatically
|
12
|
+
# objectifies the instance
|
13
|
+
#
|
14
|
+
# @return [self]
|
15
|
+
def initialize(*args, &block)
|
16
|
+
super(*args, &block)
|
17
|
+
@_objectified = true
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Class]
|
21
|
+
def _klass
|
22
|
+
::AttributeStruct::Augmented
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bogo
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- lib/attribute_struct.rb
|
59
59
|
- lib/attribute_struct/attribute_hash.rb
|
60
60
|
- lib/attribute_struct/attribute_struct.rb
|
61
|
+
- lib/attribute_struct/augmented.rb
|
61
62
|
- lib/attribute_struct/irb_compat.rb
|
62
63
|
- lib/attribute_struct/monkey_camels.rb
|
63
64
|
- lib/attribute_struct/version.rb
|
@@ -86,4 +87,3 @@ signing_key:
|
|
86
87
|
specification_version: 4
|
87
88
|
summary: Attribute structures
|
88
89
|
test_files: []
|
89
|
-
has_rdoc:
|