ns-options 1.0.0 → 1.0.1

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.
@@ -60,7 +60,8 @@ module NsOptions
60
60
  end
61
61
 
62
62
  def inspect(*args)
63
- "#<#{self.class}:#{'0x%x' % (self.object_id << 1)}:#{@__name__} #{to_hash.inspect}>"
63
+ inspect_details = to_hash.inspect rescue "error getting inspect details"
64
+ "#<#{self.class}:#{'0x%x' % (self.object_id << 1)}:#{@__name__} #{inspect_details}>"
64
65
  end
65
66
 
66
67
  end
@@ -52,7 +52,12 @@ module NsOptions
52
52
  end
53
53
 
54
54
  def reset
55
- save_value(self.rules[:default])
55
+ default_value = begin
56
+ self.rules[:default].clone
57
+ rescue TypeError
58
+ self.rules[:default]
59
+ end
60
+ save_value(default_value)
56
61
  end
57
62
 
58
63
  def is_set?
@@ -30,7 +30,11 @@ module NsOptions::Proxy
30
30
 
31
31
  # This hook copies the proxy definition to any subclasses
32
32
  def inherited(subclass)
33
- subclass.__proxy_options__.build_from(self.__proxy_options__)
33
+ subclass.build_from(self.__proxy_options__)
34
+ end
35
+
36
+ def inspect(*args, &block)
37
+ "#<#{super()}:#{__proxy_options__.inspect}>"
34
38
  end
35
39
 
36
40
  end
@@ -47,6 +51,11 @@ module NsOptions::Proxy
47
51
  __proxy_options__ == other_proxy_instance.__proxy_options__
48
52
  end
49
53
 
54
+ def inspect(*args, &block)
55
+ ref = "#{self.class.name}:#{'0x%x' % (self.object_id << 1)}"
56
+ "#<#{ref}:#{__proxy_options__.inspect}>"
57
+ end
58
+
50
59
  end
51
60
 
52
61
  module ModuleReceiverExtendMethods
@@ -56,6 +65,10 @@ module NsOptions::Proxy
56
65
  self.apply(configs || {})
57
66
  end
58
67
 
68
+ def inspect(*args, &block)
69
+ "#<#{super()}:#{__proxy_options__.inspect}>"
70
+ end
71
+
59
72
  end
60
73
 
61
74
  module ProxyMethods
@@ -81,17 +94,15 @@ module NsOptions::Proxy
81
94
 
82
95
  def has_option?(*args, &block); __proxy_options__.has_option?(*args, &block); end
83
96
  def has_namespace?(*args, &block); __proxy_options__.has_namespace?(*args, &block); end
84
- def required_set?(*args, &block); __proxy_options__.required_set?(*args, &block); end
85
- def valid?(*args, &block); __proxy_options__.valid?(*args, &block); end
86
-
87
- def apply(*args, &block); __proxy_options__.apply(*args, &block); end
88
- def to_hash(*args, &block); __proxy_options__.to_hash(*args, &block); end
89
- def each(*args, &block); __proxy_options__.each(*args, &block); end
90
- def define(*args, &block); __proxy_options__.define(*args, &block); end
91
-
92
- def inspect(*args, &block)
93
- "#<#{self.class}:#{'0x%x' % (self.object_id << 1)}:#{__proxy_options__.__name__} #{__proxy_options__.to_hash.inspect}>"
94
- end
97
+ def required_set?(*args, &block); __proxy_options__.required_set?(*args, &block); end
98
+ def valid?(*args, &block); __proxy_options__.valid?(*args, &block); end
99
+
100
+ def apply(*args, &block); __proxy_options__.apply(*args, &block); end
101
+ def to_hash(*args, &block); __proxy_options__.to_hash(*args, &block); end
102
+ def each(*args, &block); __proxy_options__.each(*args, &block); end
103
+ def define(*args, &block); __proxy_options__.define(*args, &block); end
104
+ def build_from(*args, &block); __proxy_options__.build_from(*args, &block); end
105
+ def reset(*args, &block); __proxy_options__.reset(*args, &block); end
95
106
 
96
107
  # for everything else, send to the proxied NAMESPACE handler
97
108
  # at this point it really just enables dynamic options writers
@@ -45,8 +45,8 @@ module NsOptions
45
45
 
46
46
  def not_recommended_meth_names
47
47
  [ :option, :opt, :namespace, :ns,
48
- :define, :inspect, :method_missing, :respond_to?,
49
- :apply, :to_hash, :each, :required_set?, :valid?,
48
+ :inspect, :method_missing, :respond_to?, :required_set?, :valid?,
49
+ :define, :build_from, :reset, :apply, :to_hash, :each
50
50
  ]
51
51
  end
52
52
 
@@ -1,3 +1,3 @@
1
1
  module NsOptions
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -10,8 +10,8 @@ module SomeProxy
10
10
  super(opts)
11
11
  end
12
12
 
13
- opt :value1
14
- opt :value2
13
+ opt :value1, String
14
+ opt :value2, Symbol
15
15
 
16
16
  ns :more do
17
17
  opt :other1
@@ -22,7 +22,7 @@ module SomeProxy
22
22
 
23
23
  class SomeOtherThing < SomeThing; end
24
24
 
25
- opt :some, SomeThing, :default => { :value1 => 1 }
25
+ opt :some, SomeThing, :default => { :value1 => '1' }
26
26
  opt :some_prime, SomeThing, :default => { :value1 => 'one' }
27
27
  opt :stuff, :default => []
28
28
 
@@ -13,7 +13,7 @@ class SomeProxyIntTests < Assert::Context
13
13
  subject { @proxy }
14
14
 
15
15
  should have_option :some, SomeProxy::SomeThing, {
16
- :default => {:value1 => 1}
16
+ :default => {:value1 => '1'}
17
17
  }
18
18
 
19
19
  should have_option :some_prime, SomeProxy::SomeThing, {
@@ -35,7 +35,7 @@ class SomeProxySomeThingTests < SomeProxyIntTests
35
35
  should have_options :value1, :value2
36
36
 
37
37
  should "have its :value1 defaulted" do
38
- assert_equal 1, subject.value1
38
+ assert_equal '1', subject.value1
39
39
  end
40
40
 
41
41
  end
@@ -50,4 +50,23 @@ class SomeProxySomeOtherThingTests < SomeProxyIntTests
50
50
  should have_namespace :more
51
51
  should have_options :value1, :value2
52
52
 
53
+ should "have the same definition as its superclass" do
54
+ # the superclass definition type casts :value1 to a String and :value2 to a Symbol
55
+ superprox = SomeProxy::SomeThing.new
56
+ assert_nil superprox.value1
57
+ assert_nil superprox.value2
58
+ superprox.value1 = :a_symbol
59
+ superprox.value2 = 'a_string'
60
+ assert_equal 'a_symbol', superprox.value1
61
+ assert_equal :a_string, superprox.value2
62
+
63
+ # the subject (a subclass of the superclass) should do the same
64
+ assert_nil subject.value1
65
+ assert_nil subject.value2
66
+ subject.value1 = :a_symbol
67
+ subject.value2 = 'a_string'
68
+ assert_equal 'a_symbol', subject.value1
69
+ assert_equal :a_string, subject.value2
70
+ end
71
+
53
72
  end
@@ -30,6 +30,14 @@ class NsOptions::Namespace
30
30
  assert_included subject.to_hash.inspect, subject.inspect
31
31
  end
32
32
 
33
+ should "still work if to_hash raises an exception" do
34
+ subject.option(:something, :default => proc{ raise 'test' })
35
+
36
+ output = nil
37
+ assert_nothing_raised{ output = subject.inspect }
38
+ assert_includes "error getting inspect details", output
39
+ end
40
+
33
41
  should "know its option type class" do
34
42
  assert_equal Object, subject.option_type_class
35
43
  end
@@ -77,11 +77,11 @@ class NsOptions::Option
77
77
  class DefaultRuleTests < BaseTests
78
78
  desc "using the :default rule"
79
79
  setup do
80
- @option = NsOptions::Option.new(:opt, Object, :default => "something")
80
+ @option = NsOptions::Option.new(:opt, Object, :default => {})
81
81
  end
82
82
 
83
83
  should "have defaulted value based on the rule" do
84
- assert_equal 'something', subject.value
84
+ assert_equal Hash.new, subject.value
85
85
  end
86
86
 
87
87
  should "allow overwriting the default value" do
@@ -95,10 +95,13 @@ class NsOptions::Option
95
95
  end
96
96
 
97
97
  should "return the value to its default when `reset` is called" do
98
- subject.value = "overwritten"
98
+ subject.value = {:hash => 'overwritten'}
99
99
  subject.reset
100
+ assert_equal Hash.new, subject.value
100
101
 
101
- assert_equal 'something', subject.value
102
+ subject.value[:hash] = 'overwritten'
103
+ subject.reset
104
+ assert_equal Hash.new, subject.value
102
105
  end
103
106
 
104
107
  end
@@ -23,6 +23,8 @@ module NsOptions::Proxy
23
23
  assert_respond_to :to_hash, subject
24
24
  assert_respond_to :each, subject
25
25
  assert_respond_to :define, subject
26
+ assert_respond_to :build_from, subject
27
+ assert_respond_to :reset, subject
26
28
  assert_respond_to :inspect, subject
27
29
  assert_respond_to :has_option?, subject
28
30
  assert_respond_to :has_namespace?, subject
@@ -186,6 +188,19 @@ module NsOptions::Proxy
186
188
  end
187
189
  end
188
190
 
191
+ should "should have its proxy namespace built on inheriting" do
192
+ super_ns = @a_super_class.instance_variable_get("@__proxy_options__")
193
+ assert_not_nil super_ns
194
+ assert_kind_of NsOptions::Namespace, super_ns
195
+
196
+ a_sub_class = Class.new(@a_super_class)
197
+ sub_ns = a_sub_class.instance_variable_get("@__proxy_options__")
198
+ assert_not_nil sub_ns
199
+ assert_kind_of NsOptions::Namespace, sub_ns
200
+
201
+ assert_not_same super_ns, sub_ns
202
+ end
203
+
189
204
  should "pass its definition to any subclass" do
190
205
  a_sub_class = Class.new(@a_super_class)
191
206
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ns-options
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Collin Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-12-04 00:00:00 Z
19
+ date: 2012-12-05 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: assert