yuba 0.0.2 → 0.0.7

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
- SHA1:
3
- metadata.gz: 338f5b0c508bb1e8df93486999e6d06025f0efdd
4
- data.tar.gz: 69e9080eeddb78312be8475bd71f078aaae6947c
2
+ SHA256:
3
+ metadata.gz: 5909f5f5026e2ec31ccdab4e1689d32ed7c92145bbf1e0b23eb3204efdd2ef05
4
+ data.tar.gz: e1768f1cc2076c724d3776c3044c95f8abd4a7b3af8b5a41e130a42ceb938ec8
5
5
  SHA512:
6
- metadata.gz: b5418fb02dd94930ed5b273bbbaf77cf4dad46d89c0bafb84bf4a2257d76266fb1987a19c8671aeed71bae4f78b068c991942a24797318ff59773a4880554fbf
7
- data.tar.gz: 104a17016bbc0a247f03ff21a4e87eab9fcc7c2a4f86c0d10618cc8a32fb2b8d10d572805a8a938ce5932e44b51834e5834c8ac27f32f6f9ce9a9f3f72cb4f3c
6
+ metadata.gz: 471fcd997de8b4794bf2d99e9e0830e4a92803c535026aef299556c6eeaa55e438dac8e9c338a4f3971e0eb4d59cc7ce0195fccd15488d261ac8e222e19db0e5
7
+ data.tar.gz: 14cd89a01fb65e3ca3f7270f7b3ddf1aebc5f6dedc566e13af5c0ce93721f8b2d97e137fd42782e90797fea346521140f680b3416e3ec1d8b4bf34cf803589e8
data/README.md CHANGED
@@ -35,8 +35,8 @@ $ bundle install
35
35
 
36
36
  ## Support
37
37
 
38
- - Rails 4.2+
39
- - Ruby 2.2+
38
+ - Rails 5.2+
39
+ - Ruby 2.5+
40
40
 
41
41
  ## ViewModel
42
42
 
@@ -154,6 +154,18 @@ 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_value?(:user) #=> true
163
+ service.has_public_property?(:user) #=> true
164
+ service.has_private_property?(:user) #=> false
165
+ service.has_required_property?(:user) #=> true
166
+ service.has_optional_property?(:user) #=> false
167
+ ```
168
+
157
169
  ## Form
158
170
 
159
171
  Form is just wrapper of [reform-rails](https://github.com/trailblazer/reform-rails) for now.
@@ -178,6 +190,7 @@ class ArtistsController < ApplicationController
178
190
  render :new
179
191
  end
180
192
  end
193
+ end
181
194
  ```
182
195
 
183
196
  ```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[name.to_sym] = options
14
+ self._properties = _properties.merge(name.to_sym => options)
15
15
  end
16
16
  end
17
17
 
@@ -37,6 +37,30 @@ 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
+
60
+ def has_value?(property)
61
+ has_property?(property) && respond_to?(property, true) && !send(property).nil?
62
+ end
63
+
40
64
  private
41
65
 
42
66
  def validate_arguments(args)
@@ -45,6 +69,12 @@ module Yuba
45
69
  raise ArgumentError, "missing 'property :#{key}' in #{self.class.name} class"
46
70
  end
47
71
  end
72
+
73
+ required_keys = _required_properties.keys
74
+ missing_keys = required_keys - args.keys
75
+ unless missing_keys.empty?
76
+ raise ArgumentError, "missing required arguments: #{missing_keys.join(',')}"
77
+ end
48
78
  end
49
79
 
50
80
  def define_accessors(args)
@@ -56,5 +86,9 @@ module Yuba
56
86
  self.singleton_class.class_eval { private key.to_sym } unless public_method
57
87
  end
58
88
  end
89
+
90
+ def _required_properties
91
+ _properties.reject { |_, value| value[:optional] }
92
+ end
59
93
  end
60
94
  end
data/lib/yuba/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yuba
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -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[name.to_sym] = options
18
+ self._properties = _properties.merge(name.to_sym => options)
17
19
  end
18
20
  end
19
21
 
20
22
  def initialize(**args)
21
- validate_arguments(args)
22
- define_accessors(args)
23
+ @_args = args
24
+ validate_arguments
25
+ define_accessors
23
26
  end
24
27
 
25
28
  private
26
29
 
27
- def validate_arguments(args)
28
- args.each_key do |key|
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(args)
36
- args.each do |key, value|
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.2
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - willnet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-10 00:00:00.000000000 Z
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: 4.2.0
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: 4.2.0
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: '0'
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: '0'
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: chromedriver-helper
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
- rubyforge_project:
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
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :yuba do
3
- # # Task goes here
4
- # end
@@ -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_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