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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d9235eae25e3e88f554e43a68d163d24e7213bfb8830b256ffd6a12968ca7e2
4
- data.tar.gz: 930a8eeaa04ce976cf1b387abf9b3c6775af9abf90b7b59ec8ff74af68fe7ff2
3
+ metadata.gz: eeb24e8ffff128d18963b1b9b35b542b260a2ec3b0e298064201c108a58c0c6c
4
+ data.tar.gz: 36adc49da54f8a4756d08dd0f33fbff9b6b5366115082682ff9b9fd46bd26687
5
5
  SHA512:
6
- metadata.gz: e5ef351e415ba810039a9f4cb2a77f29dd088a8bd98322123fc3f534d16004faf2c9fbefd51fe8799949c91045fd3494c7dccf64c0e90f4b9cf617fb1e9e2f90
7
- data.tar.gz: 36583d990b87b84d5fc8b2f7f149f047a7f1c2b1e11c9b651a91ca240266a9af11c3c36331d9eaebd9a4f2acbd0800f77a478d729275d62b585c09cd3211eca7
6
+ metadata.gz: 992c3b99a75e3b0226d55cc6429072be5abac8248aab9e43903352a2aeb50b0ad29acdb3dd28993ae7ddabc46b3784440d5048afef958d1ffdae1bc9e6f4738c
7
+ data.tar.gz: 4bc0a1cec3d59ae6db9e22e60504accc420e036ab197313e1c0b9f71219e3aae341acf82de1faa2f88984e6af79d19f1c4f825ad1fc5594b12db3497dcdc168c
@@ -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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- portrayal (0.1.0)
4
+ portrayal (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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: -> { 'feijoa' }
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: -> { 'feijoa' }.call, address:)
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=>#<Proc>}}
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
@@ -2,7 +2,9 @@ require 'portrayal/version'
2
2
  require 'portrayal/schema'
3
3
 
4
4
  module Portrayal
5
- def keyword(name, optional: Schema::NULL, default: Schema::NULL, &block)
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
@@ -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 ? -> { nil } : nil]
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 call_default(name)
46
- @schema[name][:default].call
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.call_default(:#{name})"
57
+ "#{name}: self.class.portrayal.get_default(:#{name})"
55
58
  else
56
59
  "#{name}:"
57
60
  end
@@ -1,3 +1,3 @@
1
1
  module Portrayal
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.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.1.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-06-08 00:00:00.000000000 Z
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