yuba 0.0.1 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +16 -4
- data/lib/yuba.rb +1 -2
- data/lib/yuba/service.rb +31 -1
- data/lib/yuba/version.rb +1 -1
- data/lib/yuba/view_model.rb +12 -7
- data/lib/yuba/view_model/rendering.rb +38 -0
- metadata +11 -13
- data/lib/tasks/crepe_tasks.rake +0 -4
- data/lib/yuba/rendering.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 017f6300c8cb0d9867119b0e186af6d285b385abec945a23f7745648e140d709
|
4
|
+
data.tar.gz: 5c67a6522565be8935e10413298640755037ed516d30105a53bb1d1bfbf30da7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34d21d868256f97cf550ff18a6adb20c6ed2a46c923979ef933966c13b79503835b9db16c4df86c7cb327506dcf240c72e437bc9541e9f8548d086e6dfa58e09
|
7
|
+
data.tar.gz: 14579a01e26e0916eb2a199ed0fc182d6902130b9ad5e9cacf35700bfd1f085d0ab4d8652bbf71bedfdd108ac97d13a43f2c71ac315c07c5acd82fb9eb466e5b
|
data/README.md
CHANGED
@@ -35,8 +35,8 @@ $ bundle install
|
|
35
35
|
|
36
36
|
## Support
|
37
37
|
|
38
|
-
- Rails
|
39
|
-
- Ruby 2.
|
38
|
+
- Rails 5.2+
|
39
|
+
- Ruby 2.5+
|
40
40
|
|
41
41
|
## ViewModel
|
42
42
|
|
@@ -80,7 +80,7 @@ Property is default to private. This means you can use it in internal the instan
|
|
80
80
|
You can use ViewModel in a controller like following
|
81
81
|
|
82
82
|
```ruby
|
83
|
-
class PostsController
|
83
|
+
class PostsController < ApplicationController
|
84
84
|
def show
|
85
85
|
@view_model = PostViewModel.new(post: post, author: 'willnet')
|
86
86
|
end
|
@@ -90,7 +90,7 @@ end
|
|
90
90
|
In view template, if you want to access post and author, you have to use `@view_model` instance variable like `@view_model.post.title`. if it feels troublesome, you can write like following
|
91
91
|
|
92
92
|
```ruby
|
93
|
-
class PostsController
|
93
|
+
class PostsController < ApplicationController
|
94
94
|
def show
|
95
95
|
view_model = PostViewModel.new(post: post, author: 'willnet')
|
96
96
|
render view_model: view_model
|
@@ -154,6 +154,17 @@ end
|
|
154
154
|
- `.call` invokes `#call` after assigning arguments as properties.
|
155
155
|
- `#success?` returns `true` if you don't invoke `#fail!`
|
156
156
|
|
157
|
+
You have inspection methods for properties.
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
service = CreatePostService.new(user: someuser)
|
161
|
+
service.has_property?(:user) #=> true
|
162
|
+
service.has_public_property?(:user) #=> true
|
163
|
+
service.has_private_property?(:user) #=> false
|
164
|
+
service.has_required_property?(:user) #=> true
|
165
|
+
service.has_optional_property?(:user) #=> false
|
166
|
+
```
|
167
|
+
|
157
168
|
## Form
|
158
169
|
|
159
170
|
Form is just wrapper of [reform-rails](https://github.com/trailblazer/reform-rails) for now.
|
@@ -178,6 +189,7 @@ class ArtistsController < ApplicationController
|
|
178
189
|
render :new
|
179
190
|
end
|
180
191
|
end
|
192
|
+
end
|
181
193
|
```
|
182
194
|
|
183
195
|
```ruby
|
data/lib/yuba.rb
CHANGED
@@ -5,9 +5,8 @@ module Yuba
|
|
5
5
|
autoload :Form, 'yuba/form'
|
6
6
|
autoload :Service, 'yuba/service'
|
7
7
|
autoload :ViewModel, 'yuba/view_model'
|
8
|
-
autoload :Rendering, 'yuba/rendering'
|
9
8
|
end
|
10
9
|
|
11
10
|
ActiveSupport.on_load(:action_controller) do
|
12
|
-
ActionController::Base.include(Yuba::Rendering)
|
11
|
+
ActionController::Base.include(Yuba::ViewModel::Rendering)
|
13
12
|
end
|
data/lib/yuba/service.rb
CHANGED
@@ -11,7 +11,7 @@ module Yuba
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def property(name, options = {})
|
14
|
-
_properties
|
14
|
+
self._properties = _properties.merge(name.to_sym => options)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -37,6 +37,26 @@ module Yuba
|
|
37
37
|
!@_success
|
38
38
|
end
|
39
39
|
|
40
|
+
def has_property?(property)
|
41
|
+
_properties.has_key?(property.to_sym)
|
42
|
+
end
|
43
|
+
|
44
|
+
def has_required_property?(property)
|
45
|
+
has_property?(property) && !_properties.dig(property.to_sym, :optional)
|
46
|
+
end
|
47
|
+
|
48
|
+
def has_optional_property?(property)
|
49
|
+
has_property?(property) && !has_required_property?(property)
|
50
|
+
end
|
51
|
+
|
52
|
+
def has_public_property?(property)
|
53
|
+
has_property?(property) && !has_private_property?(property)
|
54
|
+
end
|
55
|
+
|
56
|
+
def has_private_property?(property)
|
57
|
+
has_property?(property) && !_properties.dig(property.to_sym, :public)
|
58
|
+
end
|
59
|
+
|
40
60
|
private
|
41
61
|
|
42
62
|
def validate_arguments(args)
|
@@ -45,6 +65,12 @@ module Yuba
|
|
45
65
|
raise ArgumentError, "missing 'property :#{key}' in #{self.class.name} class"
|
46
66
|
end
|
47
67
|
end
|
68
|
+
|
69
|
+
required_keys = _required_properties.keys
|
70
|
+
missing_keys = required_keys - args.keys
|
71
|
+
unless missing_keys.empty?
|
72
|
+
raise ArgumentError, "missing required arguments: #{missing_keys.join(',')}"
|
73
|
+
end
|
48
74
|
end
|
49
75
|
|
50
76
|
def define_accessors(args)
|
@@ -56,5 +82,9 @@ module Yuba
|
|
56
82
|
self.singleton_class.class_eval { private key.to_sym } unless public_method
|
57
83
|
end
|
58
84
|
end
|
85
|
+
|
86
|
+
def _required_properties
|
87
|
+
_properties.reject { |_, value| value[:optional] }
|
88
|
+
end
|
59
89
|
end
|
60
90
|
end
|
data/lib/yuba/version.rb
CHANGED
data/lib/yuba/view_model.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Yuba
|
2
2
|
class ViewModel
|
3
|
+
autoload :Rendering, 'yuba/view_model/rendering'
|
4
|
+
|
3
5
|
class_attribute :_properties
|
4
6
|
self._properties = {}
|
5
7
|
|
@@ -13,27 +15,30 @@ module Yuba
|
|
13
15
|
# property :name, public: true
|
14
16
|
# property :email, optional: true
|
15
17
|
def property(name, options = {})
|
16
|
-
_properties
|
18
|
+
self._properties = _properties.merge(name.to_sym => options)
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
22
|
def initialize(**args)
|
21
|
-
|
22
|
-
|
23
|
+
@_args = args
|
24
|
+
validate_arguments
|
25
|
+
define_accessors
|
23
26
|
end
|
24
27
|
|
25
28
|
private
|
26
29
|
|
27
|
-
|
28
|
-
|
30
|
+
attr_reader :_args
|
31
|
+
|
32
|
+
def validate_arguments
|
33
|
+
_args.each_key do |key|
|
29
34
|
if !_properties.has_key?(key.to_sym) && !_properties.dig(key.to_sym, :optional)
|
30
35
|
raise ArgumentError, "missing 'property :#{key}' in #{self.class.name} class"
|
31
36
|
end
|
32
37
|
end
|
33
38
|
end
|
34
39
|
|
35
|
-
def define_accessors
|
36
|
-
|
40
|
+
def define_accessors
|
41
|
+
_args.each do |key, value|
|
37
42
|
public_method = _properties[key.to_sym][:public]
|
38
43
|
define_singleton_method key do
|
39
44
|
value
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Yuba
|
2
|
+
class ViewModel
|
3
|
+
module Rendering
|
4
|
+
def render(*args)
|
5
|
+
view_model_hash = args.find { |arg| arg.is_a?(Hash) && arg[:view_model] }
|
6
|
+
@_view_model = view_model_hash[:view_model] if view_model_hash && view_model_hash[:view_model]
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def view_assigns
|
11
|
+
super.merge(view_model_assigns)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def _protected_ivars
|
17
|
+
super.merge(:@_view_model)
|
18
|
+
end
|
19
|
+
|
20
|
+
def view_model_assigns
|
21
|
+
return {} unless defined?(@_view_model)
|
22
|
+
methods = @_view_model.public_methods(false)
|
23
|
+
methods.reject! do |method_name|
|
24
|
+
%i[initialize].include?(method_name) ||
|
25
|
+
!valid_variable_name?(method_name)
|
26
|
+
end
|
27
|
+
methods.inject({}) do |hash, method_name|
|
28
|
+
hash[method_name] = @_view_model.public_send(method_name)
|
29
|
+
hash
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def valid_variable_name?(name)
|
34
|
+
name.match?(/\A[_a-z]\w*\z/)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yuba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- willnet
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.2.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: reform-rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: dry-types
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 0.12.2
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 0.12.2
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: sqlite3
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -123,7 +123,7 @@ dependencies:
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
126
|
+
name: webdrivers
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
129
|
- - ">="
|
@@ -152,15 +152,14 @@ files:
|
|
152
152
|
- lib/generators/yuba/service/templates/service.tt
|
153
153
|
- lib/generators/yuba/view_model/templates/view_model.tt
|
154
154
|
- lib/generators/yuba/view_model/view_model_generator.rb
|
155
|
-
- lib/tasks/crepe_tasks.rake
|
156
155
|
- lib/yuba.rb
|
157
156
|
- lib/yuba/form.rb
|
158
157
|
- lib/yuba/form/coercion.rb
|
159
158
|
- lib/yuba/form/multi_parameter_attributes.rb
|
160
|
-
- lib/yuba/rendering.rb
|
161
159
|
- lib/yuba/service.rb
|
162
160
|
- lib/yuba/version.rb
|
163
161
|
- lib/yuba/view_model.rb
|
162
|
+
- lib/yuba/view_model/rendering.rb
|
164
163
|
homepage: https://github.com/willnet/yuba
|
165
164
|
licenses:
|
166
165
|
- MIT
|
@@ -180,8 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
179
|
- !ruby/object:Gem::Version
|
181
180
|
version: '0'
|
182
181
|
requirements: []
|
183
|
-
|
184
|
-
rubygems_version: 2.6.13
|
182
|
+
rubygems_version: 3.1.6
|
185
183
|
signing_key:
|
186
184
|
specification_version: 4
|
187
185
|
summary: Add New Layers to Rails
|
data/lib/tasks/crepe_tasks.rake
DELETED
data/lib/yuba/rendering.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module Yuba
|
2
|
-
module Rendering
|
3
|
-
def render(*args)
|
4
|
-
view_model_hash = args.find { |arg| arg.is_a?(Hash) && arg[:view_model] }
|
5
|
-
@_view_model = view_model_hash[:view_model] if view_model_hash[:view_model]
|
6
|
-
super
|
7
|
-
end
|
8
|
-
|
9
|
-
def view_assigns
|
10
|
-
super.merge(view_model_assigns)
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def _protected_ivars
|
16
|
-
super.merge(:@_view_model)
|
17
|
-
end
|
18
|
-
|
19
|
-
def view_model_assigns
|
20
|
-
return {} unless @_view_model
|
21
|
-
# TODO: get all public methods between self and Yuba::ViewModel
|
22
|
-
# now get only in self
|
23
|
-
methods = @_view_model.public_methods(false)
|
24
|
-
methods.reject! do |method_name|
|
25
|
-
%i[call initialize].include?(method_name)
|
26
|
-
end
|
27
|
-
methods.inject({}) do |hash, method_name|
|
28
|
-
hash[method_name] = @_view_model.public_send(method_name)
|
29
|
-
hash
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|