attribute_struct 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.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v0.2.2
2
+ * Update block evaluation assignment to prevent value knockout
3
+ * Fix `#is_a?` behavior and include base class in check list
4
+ * Add `#respond_to?` method
5
+ * Add irb helper module
6
+
1
7
  ## v0.2.0
2
8
  * Add support for value setting into given context level
3
9
  * Add #build helper method
data/README.md CHANGED
@@ -51,6 +51,16 @@ which gives:
51
51
  "client"=>{"general"=>{"enabled"=>false}}}}
52
52
  ```
53
53
 
54
+ ### IRB
55
+
56
+ IRB expects some things to be around like `#inspect` and `#to_s`. Before
57
+ using `AttributeStruct` in IRB, enable compat mode:
58
+
59
+ ```ruby
60
+ > require 'attribute_struct'
61
+ > AttributeStruct.irb_compat!
62
+ ```
63
+
54
64
  ## Information
55
65
 
56
66
  * Repo: https://github.com/chrisroberts/attribute_struct
Binary file
@@ -1,3 +1,5 @@
1
+ require 'attribute_struct/irb_compat'
2
+
1
3
  class AttributeStruct < BasicObject
2
4
 
3
5
  class << self
@@ -50,6 +52,15 @@ class AttributeStruct < BasicObject
50
52
  new(&block)._dump
51
53
  end
52
54
 
55
+ # Enable IRB compatibility mode
56
+ #
57
+ # @return [TrueClass]
58
+ # @note this will add methods required for working within IRB
59
+ def irb_compat!
60
+ self.send(:include, IrbCompat)
61
+ true
62
+ end
63
+
53
64
  end
54
65
 
55
66
  # @return [Truthy, Falsey] current camelizing setting
@@ -64,7 +75,7 @@ class AttributeStruct < BasicObject
64
75
  def initialize(init_hash=nil, &block)
65
76
  _klass.load_the_hash
66
77
  @_camel_keys = _klass.camel_keys
67
- @_arg_state = self.class.hashish.new
78
+ @_arg_state = __hashish.new
68
79
  @table = __hashish.new
69
80
  if(init_hash)
70
81
  _load(init_hash)
@@ -153,10 +164,15 @@ class AttributeStruct < BasicObject
153
164
  sym = _process_key(sym)
154
165
  if(!args.empty? || block)
155
166
  if(args.empty? && block)
156
- if(_state(:value_collapse))
157
- orig = @table.fetch(sym, :__unset__)
167
+ base = @table.fetch(sym, :__unset__)
168
+ if(_state(:value_collapse) && !base.is_a?(self.class!))
169
+ orig = base
170
+ base = _klass_new
171
+ else
172
+ unless(base.is_a?(self.class!))
173
+ base = _klass_new
174
+ end
158
175
  end
159
- base = _klass_new
160
176
  if(block.arity == 0)
161
177
  base.instance_exec(&block)
162
178
  else
@@ -175,7 +191,6 @@ class AttributeStruct < BasicObject
175
191
  end
176
192
  elsif(!args.empty? && block)
177
193
  base = @table[sym]
178
- base = _klass_new unless base.is_a?(_klass)
179
194
  leaf = base
180
195
  key = sym
181
196
  args.each do |arg|
@@ -238,7 +253,7 @@ class AttributeStruct < BasicObject
238
253
  # @param klass [Class]
239
254
  # @return [TrueClass, FalseClass]
240
255
  def is_a?(klass)
241
- klass.ancestors.include?(_klass)
256
+ (_klass.ancestors + [::AttributeStruct]).include?(klass)
242
257
  end
243
258
  alias_method :kind_of?, :is_a?
244
259
 
@@ -404,7 +419,8 @@ class AttributeStruct < BasicObject
404
419
  def _klass
405
420
  ::AttributeStruct
406
421
  end
407
- alias_method :class, :_klass
422
+ alias_method :klass!, :_klass
423
+ alias_method :class!, :_klass
408
424
 
409
425
  # @return [AttributeStruct] new struct instance
410
426
  # @note will set self as parent and propogate camelizing status
@@ -467,4 +483,12 @@ class AttributeStruct < BasicObject
467
483
  end
468
484
  alias_method :array!, :_array
469
485
 
486
+ # Instance responds to method name
487
+ #
488
+ # @param name [Symbol, String]
489
+ # @return [TrueClass, FalseClass]
490
+ def respond_to?(name)
491
+ _klass.instance_methods.map(&:to_sym).include?(name.to_sym)
492
+ end
493
+
470
494
  end
@@ -0,0 +1,14 @@
1
+ # Helper methods for IRB interactions
2
+ module IrbCompat
3
+
4
+ # @return [String] object inspection
5
+ def inspect
6
+ "<[#{self._klass}:#{@table.object_id}] - table: #{@table.inspect}>"
7
+ end
8
+
9
+ # @return [String] string of instance
10
+ def to_s
11
+ "<#{self._klass}:#{@table.object_id}>"
12
+ end
13
+
14
+ end
@@ -5,5 +5,5 @@ class AttributeStruct
5
5
  class Version < ::Gem::Version
6
6
  end
7
7
  # Current library version
8
- VERSION = Version.new('0.2.0')
8
+ VERSION = Version.new('0.2.2')
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  prerelease:
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: 2014-05-27 00:00:00.000000000 Z
12
+ date: 2014-05-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie
@@ -36,8 +36,10 @@ files:
36
36
  - lib/attribute_struct.rb
37
37
  - lib/attribute_struct/attribute_struct.rb
38
38
  - lib/attribute_struct/version.rb
39
+ - lib/attribute_struct/irb_compat.rb
39
40
  - lib/attribute_struct/attribute_hash.rb
40
41
  - lib/attribute_struct/monkey_camels.rb
42
+ - attribute_struct-0.2.0.gem
41
43
  - test/spec.rb
42
44
  - test/specs/camel.rb
43
45
  - test/specs/merging.rb