ns-options 1.0.1 → 1.1.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.
- data/README.md +54 -0
- data/lib/ns-options/struct.rb +13 -0
- data/lib/ns-options/version.rb +1 -1
- data/lib/ns-options.rb +1 -0
- data/test/unit/struct_tests.rb +66 -0
- metadata +7 -4
data/README.md
CHANGED
@@ -393,6 +393,60 @@ t = Thing.new(:one => 1, :two => 2, :three => 3)
|
|
393
393
|
t.to_hash # => {:one => 1, :two => 2, :three => 3}
|
394
394
|
```
|
395
395
|
|
396
|
+
## NsOptions::Struct
|
397
|
+
|
398
|
+
Much like a traditional ruby `Struct`, `NsOptions::Struct` is a class that creates other classes. However, `NsOptions::Struct` creates _proxy_ classes. There are a number of ways to do this:
|
399
|
+
|
400
|
+
```ruby
|
401
|
+
NsOptions::Struct.new #=> #<#<Class:0x1076166d0>:#<NsOptions::
|
402
|
+
Thing = NsOptions::Struct.new #=> #<Thing:#<NsOptions::
|
403
|
+
Thing = Class.new(NsOptions::Struct.new) #=> #<Thing:#<NsOptions::
|
404
|
+
class Thing < NsOptions::Struct.new; end #=> #<Thing:#<NsOptions::
|
405
|
+
```
|
406
|
+
|
407
|
+
`NsOptions::Struct` objects, being proxies, can be created with a hash of values and support dynamic writers (much like an `OpenStruct`).
|
408
|
+
|
409
|
+
```ruby
|
410
|
+
Thing = NsOptions::Struct.new
|
411
|
+
|
412
|
+
thing = Thing.new(:something => 1)
|
413
|
+
thing.something #=> 1
|
414
|
+
thing.otherthing #=> NoMethodError
|
415
|
+
thing.otherthing = 2
|
416
|
+
thing.otherthing #=> 2
|
417
|
+
```
|
418
|
+
|
419
|
+
You can pre-define the structure, including default values and type-casting info.
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
Thing = NsOptions::Struct.new do
|
423
|
+
option :something, Integer, :default => 1
|
424
|
+
option :otherthing, String
|
425
|
+
end
|
426
|
+
|
427
|
+
thing = Thing.new(:yet_another => 12.5)
|
428
|
+
thing.something #=> 1
|
429
|
+
thing.otherthing #=> nil
|
430
|
+
thing.otherthing = 2
|
431
|
+
thing.otherthing #=> '2'
|
432
|
+
thing.yet_another #=> 12.5
|
433
|
+
```
|
434
|
+
|
435
|
+
And since struct classes are proxies, you don't have to create instances of them if you don't need to.
|
436
|
+
|
437
|
+
```ruby
|
438
|
+
thing = NsOptions::Struct.new(:yet_another => 12.5) do
|
439
|
+
option :something, Integer, :default => 1
|
440
|
+
option :otherthing, String
|
441
|
+
end
|
442
|
+
|
443
|
+
thing.something #=> 1
|
444
|
+
thing.otherthing #=> nil
|
445
|
+
thing.otherthing = 2
|
446
|
+
thing.otherthing #=> '2'
|
447
|
+
thing.yet_another #=> 12.5
|
448
|
+
```
|
449
|
+
|
396
450
|
## Installation
|
397
451
|
|
398
452
|
Add this line to your application's Gemfile:
|
data/lib/ns-options/version.rb
CHANGED
data/lib/ns-options.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ns-options/proxy'
|
3
|
+
require 'ns-options/struct'
|
4
|
+
|
5
|
+
module NsOptions::Struct
|
6
|
+
|
7
|
+
class BaseTests < Assert::Context
|
8
|
+
desc "NsOptions::Struct"
|
9
|
+
setup do
|
10
|
+
@struct = NsOptions::Struct.new
|
11
|
+
end
|
12
|
+
subject { @struct }
|
13
|
+
|
14
|
+
should "be a proxy class" do
|
15
|
+
assert_kind_of ::Class, subject
|
16
|
+
assert subject.ancestors.include?(::NsOptions::Proxy)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
class StructGlobalTests < BaseTests
|
22
|
+
desc "assigned to a global"
|
23
|
+
setup do
|
24
|
+
Thing = NsOptions::Struct.new
|
25
|
+
end
|
26
|
+
subject { Thing }
|
27
|
+
|
28
|
+
should "be a proxy class" do
|
29
|
+
assert_kind_of ::Class, subject
|
30
|
+
assert subject.ancestors.include?(::NsOptions::Proxy)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
class StructClassTests < BaseTests
|
36
|
+
desc "used as the superclass to another class"
|
37
|
+
setup do
|
38
|
+
Thing = Class.new(NsOptions::Struct.new)
|
39
|
+
end
|
40
|
+
subject { Thing }
|
41
|
+
|
42
|
+
should "be a proxy class" do
|
43
|
+
assert_kind_of ::Class, subject
|
44
|
+
assert subject.ancestors.include?(::NsOptions::Proxy)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
class StructureTests < BaseTests
|
50
|
+
desc "created with a structure and values"
|
51
|
+
setup do
|
52
|
+
@struct = NsOptions::Struct.new(:a => 'aye') do
|
53
|
+
option :b, Symbol, :default => :bee
|
54
|
+
option :c
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
should "should define the structure and apply the values" do
|
59
|
+
assert_equal 'aye', subject.a
|
60
|
+
assert_equal :bee, subject.b
|
61
|
+
assert_nil subject.c
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
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:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
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-
|
19
|
+
date: 2012-12-10 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: assert
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- lib/ns-options/proxy.rb
|
62
62
|
- lib/ns-options/proxy_method.rb
|
63
63
|
- lib/ns-options/root_methods.rb
|
64
|
+
- lib/ns-options/struct.rb
|
64
65
|
- lib/ns-options/version.rb
|
65
66
|
- log/.gitkeep
|
66
67
|
- ns-options.gemspec
|
@@ -85,6 +86,7 @@ files:
|
|
85
86
|
- test/unit/proxy_method_tests.rb
|
86
87
|
- test/unit/proxy_tests.rb
|
87
88
|
- test/unit/root_methods_tests.rb
|
89
|
+
- test/unit/struct_tests.rb
|
88
90
|
homepage: https://github.com/redding/ns-options
|
89
91
|
licenses: []
|
90
92
|
|
@@ -140,3 +142,4 @@ test_files:
|
|
140
142
|
- test/unit/proxy_method_tests.rb
|
141
143
|
- test/unit/proxy_tests.rb
|
142
144
|
- test/unit/root_methods_tests.rb
|
145
|
+
- test/unit/struct_tests.rb
|