opt_struct 0.3.0 → 0.4.0

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: 13aeb90dc88ca1e4eabb9ab9f0a82b595cae5953
4
- data.tar.gz: bb6faaad48a7fe17a7963b85f7612d2321f1e973
3
+ metadata.gz: 20cc3d259d8f9a17a7804fa4022e01862483a8b8
4
+ data.tar.gz: 92344e5ac8ec8d8abd8a4512fed58584c67041ff
5
5
  SHA512:
6
- metadata.gz: 917067f619231d3575b0d2ddeb3cf921e749e5d12191fa7ab58844630e34059c8823d8b1dc5415c63b623d6a6da53c8f3376fdc2dfa2c88a262bf930455b2988
7
- data.tar.gz: 49a85c43f919def4a677898dd83a67f68944262314fae1a316106ef3a6a9c84fb3005c1f44254eb5cbc18cfacac676acf8384b694e3d85a75fa34391e0f9cf34
6
+ metadata.gz: 349c78d63e1f56eaa801de15ad3294de2e2877c5345997e68568b5a4c985ffcbaee11abf0b01584a5d5a81bc022aa48e8a87e20bf6ca343c1858f37ac6f3ff1e
7
+ data.tar.gz: d2d65670ac1c8a1f4e0a09a0ad1836a67190ad6d81d06b5a50613c4921b49431436cc24f1aac7eec69503bad4db5ac2497a203df4f9837342e131a6d402eb19f
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # The Opt Struct
2
2
 
3
- A struct around a hash
3
+ A struct around a hash. Great for encapsulating actions with complex configuration, like interactor/action classes.
4
4
 
5
5
  ```ruby
6
6
  gem "opt_struct"
@@ -19,6 +19,9 @@ MyClass.new
19
19
  MyClass.new "foo", "bar"
20
20
  # => #<MyClass>
21
21
 
22
+ MyClass.new foo: "foo", bar: "bar"
23
+ # => #<MyClass>
24
+
22
25
  i = MyClass.new "foo", "bar", yin: "yang"
23
26
  i.options
24
27
  # => {yin: "yang"}
@@ -85,3 +88,45 @@ i = MyClass.new("something", bar: "foo")
85
88
  [i.foo, i.bar]
86
89
  # => ["something", "foo"]
87
90
  ```
91
+
92
+ ## Example 5
93
+
94
+ Both `build` and `new` accept a block.
95
+
96
+ ```ruby
97
+ PersonClass = OptStruct.new do
98
+ required :first_name
99
+ option :last_name
100
+
101
+ def name
102
+ [first_name, last_name].compact.join(" ")
103
+ end
104
+ end
105
+
106
+ t = PersonClass.new(first_name: "Trish")
107
+ # => #<PersonClass>
108
+ t.name
109
+ # => "Trish"
110
+ t.last_name = "Smith"
111
+ t.name
112
+ # => "Trish Smith"
113
+
114
+ class CarClass
115
+ include OptStruct.build do
116
+ required :make, :model
117
+ options :year, transmission: "Automatic"
118
+
119
+ def name
120
+ [year, make, model].compact.join(" ")
121
+ end
122
+ end
123
+ end
124
+
125
+ c = CarClass.new(make: "Infiniti", model: "G37", year: 2012)
126
+ c.name
127
+ # => "2012 Infinit G37"
128
+
129
+ c = CarClass.new(model: "WRX", make: "Subaru")
130
+ c.name
131
+ # => "Subaru WRX"
132
+ ```
data/lib/opt_struct.rb CHANGED
@@ -16,28 +16,32 @@ module OptStruct
16
16
  inject_struct(klass)
17
17
  end
18
18
 
19
- def self.new(*args, **defaults)
19
+ def self.new(*args, **defaults, &callback)
20
20
  check_for_invalid_args(args)
21
21
  args.map!(&:to_sym)
22
- inject_struct(Class.new) do
22
+ klass = inject_struct(Class.new) do
23
23
  expect_arguments *args
24
24
  options defaults
25
25
  end
26
+ klass.class_exec(&callback) if callback
27
+ klass
26
28
  end
27
29
 
28
- def self.build(*args, **defaults)
30
+ def self.build(*args, **defaults, &callback)
29
31
  check_for_invalid_args(args)
30
32
  args.map!(&:to_sym)
31
33
  Module.new do
32
34
  @arguments = args
33
35
  @defaults = defaults
36
+ @callback = callback
34
37
 
35
38
  def self.included(klass)
36
- arguments, defaults = @arguments, @defaults
39
+ arguments, defaults, callback = @arguments, @defaults, @callback
37
40
  OptStruct.inject_struct(klass) do
38
41
  expect_arguments *arguments
39
42
  options defaults
40
43
  end
44
+ klass.class_exec(&callback) if callback
41
45
  end
42
46
  end
43
47
  end
@@ -67,16 +67,18 @@ module OptStruct
67
67
  def expect_arguments(*arguments)
68
68
  @expected_arguments = arguments
69
69
  attr_accessor *arguments
70
- lines = []
70
+ assignment_lines = String.new
71
71
  arguments.each_with_index do |arg, i|
72
- lines << "@#{arg} = @options.delete(:#{arg}) if @options.key?(:#{arg})"
73
- lines << "@#{arg} = values[#{i}] if values.length > #{i}"
74
- lines << %[raise ArgumentError, "missing required argument: #{arg}" unless defined?(@#{arg})]
72
+ assignment_lines << <<~RUBY
73
+ @#{arg} = @options.delete(:#{arg}) if @options.key?(:#{arg})
74
+ @#{arg} = values[#{i}] if values.length > #{i}
75
+ raise ArgumentError, "missing required argument: #{arg}" unless defined?(@#{arg})
76
+ RUBY
75
77
  end
76
78
  self.class_eval <<~RUBY
77
79
  def initialize(*values, **options)
78
80
  @options = self.class.defaults.merge(options)
79
- #{lines.join("\n ")}
81
+ #{assignment_lines}
80
82
  check_required_keys
81
83
  end
82
84
  RUBY
data/opt_struct.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "opt_struct"
3
- spec.version = "0.3.0"
3
+ spec.version = "0.4.0"
4
4
  spec.authors = ["Carl Zulauf"]
5
5
  spec.email = ["carl@linkleaf.com"]
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-28 00:00:00.000000000 Z
11
+ date: 2017-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler