flex_struct 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: caf46503e9a8152fa8f0e263643c8be5900ac8db
4
- data.tar.gz: 1a5aefee1703e619e88c2fb4f0e618fcb832b98d
3
+ metadata.gz: be5088836ee8daf48c1348b406db1f7f57e20f2e
4
+ data.tar.gz: ff5c0e89a2ae853d337d4b2ba5fc945c65413f22
5
5
  SHA512:
6
- metadata.gz: 8b0662189bbffdf683097d768121da164e5cbea58ad73f6866e7678a17d4dc0897fa94ee5df8039d645e1f2d59bea633ee7b0c366b95251bd2b34a8e6302e494
7
- data.tar.gz: 1414dbe98a5a8e7fd03851ae85dbbc683e264bb0bf1757be5e3731877198f657850dcc5bf71ed04a3ab6e9a09dc94a0dc1327c7ed7f4b73ca15d886345af863d
6
+ metadata.gz: 95b8a633396b72cd8da9825b008e66e06f1c528ba873255b8f9927b138d760452cd02a24d7af8bb0735a036a0976979fd52f06a1590e31c778f9d8f22985b9fe
7
+ data.tar.gz: 7f4bd82e56b4a7fda380634a9302517b4d83bc7bd14aedf537cfb4ba1f3ffa246249190e98e2da17b7da871a98f6e72f28b22caf69a3a8fcd7191f38004f799a
data/.reek ADDED
@@ -0,0 +1,6 @@
1
+ ModuleInitialize:
2
+ exclude:
3
+ - FlexStruct::Frozen
4
+ - FlexStruct::Initializer
5
+
6
+ # vim: set ft=yaml:
data/Gemfile CHANGED
@@ -5,3 +5,5 @@ gemspec
5
5
  # Gems which aren't development dependencies, but are useful for development
6
6
  gem "guard-bundler"
7
7
  gem "guard-rspec"
8
+
9
+ gem "pry-byebug"
data/Guardfile CHANGED
@@ -24,3 +24,5 @@ guard :rspec, cmd: "bundle exec rspec" do
24
24
  ruby = dsl.ruby
25
25
  dsl.watch_spec_files_for(ruby.lib_files)
26
26
  end
27
+
28
+ # vim: set ft=ruby:
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
@@ -0,0 +1,9 @@
1
+ class FlexStruct
2
+ # Wraps a FlexStruct initializer to freeze the struct instance after creation.
3
+ module Frozen
4
+ def initialize(*)
5
+ super
6
+ freeze
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class FlexStruct
2
+ # FlexStruct's initializer adds two alternate ways to initialize a Struct
3
+ module Initializer
4
+ def initialize(*args, **kwargs)
5
+ super(*args)
6
+ kwargs.each { |key, val| self[key] = val }
7
+ yield self if block_given?
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  class FlexStruct
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
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
- def initialize(*args, **kwargs)
8
- super(*args)
9
- kwargs.each { |key, val| self[key] = val }
10
- yield self if block_given?
11
- end
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.0
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: