portrayal 0.4.0 → 0.5.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
  SHA256:
3
- metadata.gz: 44f125ec13de164e28a515a3a4cbdfa98047d645aae03bf818cc184983ecb679
4
- data.tar.gz: fab0eb7aa3fad16c833d547a2d405fdcbc34aa2160633e60ff4ad43420135c49
3
+ metadata.gz: 9ad16f90c209c12d143b4705a6e076cd62b7a5e691f464434489ab58d3efec9c
4
+ data.tar.gz: 575380d993804fac6bca6deb407f5acd92b890304b4729058cd9c9e80c89752a
5
5
  SHA512:
6
- metadata.gz: 46af6c3b3918144effcdf07bd6a15bb1dac898a99569183cc2129310e75bd38f035432f7ced725d2ad583d1a09b65f26ad304c08fcf29f46800a086b09aef44f
7
- data.tar.gz: b5564653b81c34921d10f62979c38c8dbc1f6e06ad4c396c20aaa0e3a1f09f28b738425ac508c1d9556a4b2b77e0c83dcb0a39b3bc91102ec59b87cd09bf420f
6
+ metadata.gz: f4c8a0bdb896dad477aff92b718d0401240497dd1ec0fd5d992a41206026ab213149206b1b9707c1943fb2dc313a0864bcaf285553e6fb6ed4d026cf18099d8d
7
+ data.tar.gz: 4f07e7614d06f50f2a053d8ba0b73f5a32061d5fc9e75c878d84441fc16a30a68747b229eb06c9cc0130d8a7c08d20268b438654823ced77ed9e3375f2644362
@@ -2,6 +2,10 @@ This project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.5.0 - 2020-05-28
6
+
7
+ * Add option `define` for overriding nested class name. [[commit]](https://github.com/scottscheapflights/portrayal/commit/665ad297fb71fcdf5f641c672a457ccbe29e4a49)
8
+
5
9
  ## 0.4.0 - 2020-05-16
6
10
 
7
11
  * Portrayal schema is deep-duped to subclasses. [[commit]](https://github.com/scottscheapflights/portrayal/commit/f346483a379ce9fbdece72cde8b0844f2d22b1cd)
data/README.md CHANGED
@@ -8,7 +8,7 @@ Inspired by:
8
8
  - Piotr Solnica's [virtus](https://github.com/solnic/virtus)
9
9
  - Everything [Michel Martens](https://github.com/soveran)
10
10
 
11
- Portrayal is a minimalist gem (~130 loc, no dependencies) for building struct-like classes. It provides a small yet powerful step up from plain ruby with its one and only `keyword` method.
11
+ Portrayal is a minimalist gem (~120 loc, no dependencies) for building struct-like classes. It provides a small yet powerful step up from plain ruby with its one and only `keyword` method.
12
12
 
13
13
  ```ruby
14
14
  class Person < MySuperClass
@@ -198,6 +198,36 @@ Any other value works as normal.
198
198
  keyword :foo, default: 4
199
199
  ```
200
200
 
201
+ ### Nested Classes
202
+
203
+ When you pass a block to a keyword, it creates a nested class named after camelized keyword name.
204
+
205
+ ```ruby
206
+ class Person
207
+ extend Portrayal
208
+
209
+ keyword :address do
210
+ keyword :street
211
+ end
212
+ end
213
+ ```
214
+
215
+ The above block created class `Person::Address`.
216
+
217
+ If you want to change the name of the created class, use the option `define`.
218
+
219
+ ```ruby
220
+ class Person
221
+ extend Portrayal
222
+
223
+ keyword :visited_countries, define: 'Country' do
224
+ keyword :name
225
+ end
226
+ end
227
+ ```
228
+
229
+ This defines `Person::Country`, while the accessor remains `visited_countries`.
230
+
201
231
  ### Schema
202
232
 
203
233
  Every class that has at least one keyword defined in it automatically receives a class method called `portrayal`. This method is a schema of your object with some additional helpers.
@@ -4,7 +4,7 @@ require 'portrayal/schema'
4
4
  module Portrayal
5
5
  NULL = :_portrayal_value_not_set
6
6
 
7
- def keyword(name, optional: NULL, default: NULL, &block)
7
+ def keyword(name, optional: NULL, default: NULL, define: nil, &block)
8
8
  unless respond_to?(:portrayal)
9
9
  class << self
10
10
  attr_reader :portrayal
@@ -24,9 +24,8 @@ module Portrayal
24
24
  class_eval(portrayal.definition_of_initialize)
25
25
 
26
26
  if block_given?
27
- keyword_class = Class.new(superclass) { extend Portrayal }
28
- keyword_class.class_eval(&block)
29
- const_set(portrayal.camelcase(name), keyword_class)
27
+ kw_class = Class.new(superclass) { extend Portrayal }
28
+ const_set(define || portrayal.camelize(name), kw_class).class_eval(&block)
30
29
  end
31
30
  end
32
31
  end
@@ -10,6 +10,8 @@ module Portrayal
10
10
  Hash[object.class.portrayal.keywords.map { |k| [k, object.send(k)] }]
11
11
  end
12
12
 
13
+ def camelize(string); string.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase } end
14
+
13
15
  def add_keyword(name, optional, default)
14
16
  optional, default =
15
17
  case [optional == NULL, default == NULL]
@@ -22,10 +24,6 @@ module Portrayal
22
24
  @schema[name.to_sym] = { optional: optional, default: default }
23
25
  end
24
26
 
25
- def camelcase(string)
26
- string.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase }
27
- end
28
-
29
27
  def get_default(name)
30
28
  action, value = @schema[name][:default]
31
29
  action == :call ? value.call : value
@@ -1,3 +1,3 @@
1
1
  module Portrayal
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: portrayal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Chernyak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-17 00:00:00.000000000 Z
11
+ date: 2020-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler