flex_struct 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/.reek +6 -0
- data/Gemfile +2 -0
- data/Guardfile +2 -0
- data/README.md +31 -0
- data/lib/flex_struct/frozen.rb +9 -0
- data/lib/flex_struct/initializer.rb +10 -0
- data/lib/flex_struct/version.rb +1 -1
- data/lib/flex_struct.rb +8 -6
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be5088836ee8daf48c1348b406db1f7f57e20f2e
|
4
|
+
data.tar.gz: ff5c0e89a2ae853d337d4b2ba5fc945c65413f22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95b8a633396b72cd8da9825b008e66e06f1c528ba873255b8f9927b138d760452cd02a24d7af8bb0735a036a0976979fd52f06a1590e31c778f9d8f22985b9fe
|
7
|
+
data.tar.gz: 7f4bd82e56b4a7fda380634a9302517b4d83bc7bd14aedf537cfb4ba1f3ffa246249190e98e2da17b7da871a98f6e72f28b22caf69a3a8fcd7191f38004f799a
|
data/.reek
ADDED
data/Gemfile
CHANGED
data/Guardfile
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,8 @@ A drop-in replacement for Struct which adds a more flexible initialize method
|
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
+
### Basic
|
8
|
+
|
7
9
|
Create a `FlexStruct` very similarly to how you'd use any other `Struct`:
|
8
10
|
|
9
11
|
Pet = FlexStruct.new(:species, :name, :colour)
|
@@ -25,6 +27,35 @@ default `Struct`s:
|
|
25
27
|
pet.colour = :tortoiseshell
|
26
28
|
end
|
27
29
|
|
30
|
+
### Customisation
|
31
|
+
|
32
|
+
`FlexStruct` acts just like a `Struct` in other ways, you can customize its
|
33
|
+
behaviour and add functionality to the generated class by passing a block to the
|
34
|
+
`FlexStruct` constructor:
|
35
|
+
|
36
|
+
Person = FlexStruct.new(:forename, :surname) do
|
37
|
+
def fullname
|
38
|
+
[forename, surname].compact.join(" ")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Person.new(forename: "Finn", surname: "Mertens").fullname
|
43
|
+
# => "Finn Mertens"
|
44
|
+
|
45
|
+
It's possible to overwrite the `initialize` method this way - so be sure to use
|
46
|
+
`super` if you do this. Or better, don't do this, you probably don't need to.
|
47
|
+
|
48
|
+
### Immutability
|
49
|
+
|
50
|
+
`FlexStruct` comes with a convenience method for making immutable `Struct`s. This
|
51
|
+
will amend the generated struct to make it frozen after initialization:
|
52
|
+
|
53
|
+
Position = FlexStruct.new(:lat, :lng) { prepend FlexStruct::Frozen }
|
54
|
+
|
55
|
+
location = Position.new(lat: 44.681, lng: 169.101)
|
56
|
+
location.lat = -70.850
|
57
|
+
# => RuntimeError: can't modify frozen Position
|
58
|
+
|
28
59
|
## Why?
|
29
60
|
|
30
61
|
`Hash`es are great. You can use them to store arbitrary key/value pairs using
|
data/lib/flex_struct/version.rb
CHANGED
data/lib/flex_struct.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
# A drop-in replacement for Struct which adds a more flexible initialize method
|
2
2
|
class FlexStruct
|
3
3
|
autoload :VERSION, "flex_struct/version"
|
4
|
+
autoload :Frozen, "flex_struct/frozen"
|
5
|
+
autoload :Initializer, "flex_struct/initializer"
|
4
6
|
|
5
|
-
def self.new(*args)
|
7
|
+
def self.new(*args, &block)
|
6
8
|
Struct.new(*args) do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
module_eval(&block) if block
|
10
|
+
|
11
|
+
# Insert our `initialize` method into the ancestors chain so it can be
|
12
|
+
# overridden by `block` if necessary
|
13
|
+
include Initializer
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flex_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gareth Adams
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- ".reek"
|
77
78
|
- ".rspec"
|
78
79
|
- ".rubocop.yml"
|
79
80
|
- ".travis.yml"
|
@@ -87,6 +88,8 @@ files:
|
|
87
88
|
- bin/setup
|
88
89
|
- flex_struct.gemspec
|
89
90
|
- lib/flex_struct.rb
|
91
|
+
- lib/flex_struct/frozen.rb
|
92
|
+
- lib/flex_struct/initializer.rb
|
90
93
|
- lib/flex_struct/version.rb
|
91
94
|
homepage: https://github.com/bridgeu/flex_struct
|
92
95
|
licenses:
|