portrayal 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/Gemfile.lock +1 -1
- data/README.md +18 -3
- data/lib/portrayal.rb +3 -1
- data/lib/portrayal/schema.rb +11 -8
- data/lib/portrayal/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eeb24e8ffff128d18963b1b9b35b542b260a2ec3b0e298064201c108a58c0c6c
|
4
|
+
data.tar.gz: 36adc49da54f8a4756d08dd0f33fbff9b6b5366115082682ff9b9fd46bd26687
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 992c3b99a75e3b0226d55cc6429072be5abac8248aab9e43903352a2aeb50b0ad29acdb3dd28993ae7ddabc46b3784440d5048afef958d1ffdae1bc9e6f4738c
|
7
|
+
data.tar.gz: 4bc0a1cec3d59ae6db9e22e60504accc420e036ab197313e1c0b9f71219e3aae341acf82de1faa2f88984e6af79d19f1c4f825ad1fc5594b12db3497dcdc168c
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
This project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
2
|
+
|
3
|
+
## [0.2.0] - 2019-07-03
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
|
7
|
+
* It's now possible to specify non-lambda default values, like `default: "foo". There is now also a distinction between a proc and a lambda default. Procs are `call`-ed, while lambdas or any other types are returned as-is. In the majority of cases defaults are static values, and there is no need for the performance overhead of making all defaults into anonymous functions.
|
8
|
+
|
9
|
+
## [0.1.0]
|
10
|
+
|
11
|
+
First version.
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -14,7 +14,7 @@ class Person < MySuperClass
|
|
14
14
|
|
15
15
|
keyword :name
|
16
16
|
keyword :age, optional: true
|
17
|
-
keyword :favorite_fruit, default:
|
17
|
+
keyword :favorite_fruit, default: 'feijoa'
|
18
18
|
|
19
19
|
keyword :address do
|
20
20
|
keyword :street
|
@@ -41,7 +41,7 @@ The code above produces almost exactly the following ruby. There's a lot of boil
|
|
41
41
|
class Person < MySuperClass
|
42
42
|
attr_reader :name, :age, :favorite_fruit, :address
|
43
43
|
|
44
|
-
def initialize(name:, age: nil, favorite_fruit:
|
44
|
+
def initialize(name:, age: nil, favorite_fruit: 'feijoa', address:)
|
45
45
|
@name = name
|
46
46
|
@age = age
|
47
47
|
@favorite_fruit = favorite_fruit
|
@@ -123,6 +123,21 @@ Possible use cases for these objects include, but are not limited to:
|
|
123
123
|
- Objects serializable for 3rd party APIs
|
124
124
|
- Objects serializable for React components
|
125
125
|
|
126
|
+
### Defaults
|
127
|
+
|
128
|
+
When specifying default, there's a difference between procs and lambda.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
keyword :foo, default: proc { 2 + 2 } # => Will call this proc and return 4
|
132
|
+
keyword :foo, default: -> { 2 + 2 } # => Will return this lambda itself
|
133
|
+
```
|
134
|
+
|
135
|
+
Any other value works as normal.
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
keyword :foo, default: 4
|
139
|
+
```
|
140
|
+
|
126
141
|
### Schema
|
127
142
|
|
128
143
|
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.
|
@@ -149,7 +164,7 @@ Address.portrayal.attributes(address) # => {street: '34th st', city: 'NYC', post
|
|
149
164
|
Get everything portrayal knows about your keywords in one hash.
|
150
165
|
|
151
166
|
```ruby
|
152
|
-
Address.portrayal.schema # => {:street=>{:optional=>false, :default=>nil}, :city=>{:optional=>false, :default=>nil}, :postcode=>{:optional=>false, :default=>nil}, :country=>{:optional=>true, :default
|
167
|
+
Address.portrayal.schema # => {:street=>{:optional=>false, :default=>nil}, :city=>{:optional=>false, :default=>nil}, :postcode=>{:optional=>false, :default=>nil}, :country=>{:optional=>true, :default=>[:return, nil]}}
|
153
168
|
```
|
154
169
|
|
155
170
|
## Philosophy
|
data/lib/portrayal.rb
CHANGED
@@ -2,7 +2,9 @@ require 'portrayal/version'
|
|
2
2
|
require 'portrayal/schema'
|
3
3
|
|
4
4
|
module Portrayal
|
5
|
-
|
5
|
+
NULL = :_portrayal_value_not_set
|
6
|
+
|
7
|
+
def keyword(name, optional: NULL, default: NULL, &block)
|
6
8
|
unless respond_to?(:portrayal)
|
7
9
|
class << self; attr_reader :portrayal end
|
8
10
|
@portrayal = Schema.new
|
data/lib/portrayal/schema.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module Portrayal
|
2
2
|
class Schema
|
3
|
-
NULL = :_portrayal_value_not_set
|
4
|
-
|
5
3
|
attr_reader :schema
|
6
4
|
|
7
5
|
def initialize
|
@@ -28,11 +26,11 @@ module Portrayal
|
|
28
26
|
if optional == NULL && default == NULL
|
29
27
|
[false, nil]
|
30
28
|
elsif optional != NULL && default == NULL
|
31
|
-
[optional, optional ?
|
29
|
+
[optional, optional ? [:return, nil] : nil]
|
32
30
|
elsif optional == NULL && default != NULL
|
33
|
-
[true, default]
|
31
|
+
[true, [default_strategy(default), default]]
|
34
32
|
else
|
35
|
-
[optional, optional ? default : nil]
|
33
|
+
[optional, optional ? [default_strategy(default), default] : nil]
|
36
34
|
end
|
37
35
|
|
38
36
|
@schema[name.to_sym] = { optional: optional, default: default }
|
@@ -42,8 +40,13 @@ module Portrayal
|
|
42
40
|
string.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase }
|
43
41
|
end
|
44
42
|
|
45
|
-
def
|
46
|
-
@schema[name][:default]
|
43
|
+
def get_default(name)
|
44
|
+
action, value = @schema[name][:default]
|
45
|
+
action == :call ? value.call : value
|
46
|
+
end
|
47
|
+
|
48
|
+
def default_strategy(value)
|
49
|
+
(value.is_a?(Proc) && !value.lambda?) ? :call : :return
|
47
50
|
end
|
48
51
|
|
49
52
|
def definition_of_initialize
|
@@ -51,7 +54,7 @@ module Portrayal
|
|
51
54
|
@schema
|
52
55
|
.map { |name, config|
|
53
56
|
if config[:optional]
|
54
|
-
"#{name}: self.class.portrayal.
|
57
|
+
"#{name}: self.class.portrayal.get_default(:#{name})"
|
55
58
|
else
|
56
59
|
"#{name}:"
|
57
60
|
end
|
data/lib/portrayal/version.rb
CHANGED
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
|
+
version: 0.2.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: 2019-
|
11
|
+
date: 2019-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- ".gitignore"
|
81
81
|
- ".rspec"
|
82
82
|
- ".travis.yml"
|
83
|
+
- CHANGELOG.md
|
83
84
|
- CODE_OF_CONDUCT.md
|
84
85
|
- Gemfile
|
85
86
|
- Gemfile.lock
|