yuba 0.0.2 → 0.0.3

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: 290adbd946ac8cad632dd7d5cb975a4d5c68d831e9cbbeb7e3f6e42b89fd1b26
4
+ data.tar.gz: 583c456070ce6dd43c470de3db139a4e8fc2c5753dc9a7dcd74021b33b4383c4
5
5
  SHA512:
6
- metadata.gz: b5418fb02dd94930ed5b273bbbaf77cf4dad46d89c0bafb84bf4a2257d76266fb1987a19c8671aeed71bae4f78b068c991942a24797318ff59773a4880554fbf
7
- data.tar.gz: 104a17016bbc0a247f03ff21a4e87eab9fcc7c2a4f86c0d10618cc8a32fb2b8d10d572805a8a938ce5932e44b51834e5834c8ac27f32f6f9ce9a9f3f72cb4f3c
6
+ metadata.gz: ba927cfa71760e4b4e59e2959678783eaea2c77b9cd9eb746cc78b091d85ad23300f99da6770c36cc6f4f3fe7c07cc5d7f118cbaee1bae3fbd863d3dbbda404c
7
+ data.tar.gz: bf0e9248993e20e124a6de98eeb7b5bce9fa629ac16d7d32702b3f167cfcc5f042477ea2bf789c2a70ca9e43a51280c196bd1c56191e80379d33460f78462801
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Yuba
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
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.3
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: 2018-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -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
@@ -181,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
180
  version: '0'
182
181
  requirements: []
183
182
  rubyforge_project:
184
- rubygems_version: 2.6.13
183
+ rubygems_version: 2.7.6
185
184
  signing_key:
186
185
  specification_version: 4
187
186
  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