active_form_objects 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/active_form_objects.rb +97 -0
  3. metadata +72 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cd6a6062ef08b1c9b46388bd3f0a1274b1d4d9d442e39c91dc1d1c63d6415447
4
+ data.tar.gz: a3c513e5714d72da6f6f70a19d9c875f7e26c3e66d4e78e35c5b49f7e8bbc8c2
5
+ SHA512:
6
+ metadata.gz: 8509659b8deb555882893706d2c088462a3e2f6b42291380abf637071e228e14237eda6bc73386e257def994bf904cb29d6ada1440985f5dbec0326e8e7783f8
7
+ data.tar.gz: 35a5c17b12542a29db944667128218c96b82c10c9253dd8b674ce889e7700be22f91d43894acfdf109ada496cfec085cbea937c05e7789e3e5245bbbd474c103
@@ -0,0 +1,97 @@
1
+ require 'active_support'
2
+ require 'active_model'
3
+ require 'active_support/core_ext/object/instance_variables'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+ require_relative 'concerns/relations'
6
+ require_relative 'concerns/savings'
7
+
8
+ module ActiveFormObjects
9
+ class Base
10
+ include ActiveModel::Model
11
+ include Relations
12
+ include Savings
13
+
14
+ @@authorized_params = {}
15
+ @@overrided_params = {}
16
+ @@default_params = {}
17
+ @@preparers = {}
18
+ @@resource = {}
19
+
20
+ attr_reader :resource
21
+
22
+ def self.resource(resource = nil, &block)
23
+ bloc_to_call = block || proc { resource }
24
+ @@resource[name] = bloc_to_call
25
+ end
26
+
27
+ def self.prepare(*params)
28
+ method_to_call = params.last[:with]
29
+ @@preparers[name] = (@@preparers[name] || []) + [{
30
+ key: params.first,
31
+ method_to_call: method_to_call
32
+ }]
33
+ end
34
+
35
+ def self.attributes(*params)
36
+ @@authorized_params[name] = (@@authorized_params[name] || []) + params
37
+ send(:attr_accessor, *params)
38
+ end
39
+
40
+ def self.delegate(*params)
41
+ delegator = params.last
42
+ params.pop
43
+ send(:attributes, *params)
44
+ define_method("#{delegator[:to]}_params") do
45
+ attrs_only(params)
46
+ end
47
+ end
48
+
49
+ def self.set_default(params)
50
+ [params[:to]].flatten.each do |param|
51
+ @@default_params[name] ||= {}
52
+ @@default_params[name][param] = params[:value]
53
+ end
54
+ send(:attributes, *params[:to])
55
+ end
56
+
57
+ def self.ensure_value(param, value)
58
+ @@overrided_params[name] ||= {}
59
+ @@overrided_params[name][param] = value
60
+
61
+ send(:attributes, param)
62
+ end
63
+
64
+ def initialize(params, resource = nil)
65
+ @resource = resource || @@resource[self.class.name].try(:call, params).try(:new)
66
+ set_associated_forms
67
+ params = affect_relation_values(params)
68
+ resource_params(params || {})
69
+
70
+ params_with_resource_attrs = @resource.respond_to?(:attributes) ? @resource.attributes.symbolize_keys.slice(*@authorized_params).merge(@params) : @params
71
+ params_with_preprared_value = affect_prepared_values(params_with_resource_attrs)
72
+ super(params_with_preprared_value)
73
+ end
74
+
75
+ def affect_prepared_values(params)
76
+ (@@preparers[self.class.name] || []).each do |preparer|
77
+ params[preparer[:key]] = params[preparer[:key]].try(preparer[:method_to_call])
78
+ end
79
+ params
80
+ end
81
+
82
+ def resource_params(params)
83
+ @default_params = @@default_params[self.class.name] || {}
84
+ @overrided_params = @@overrided_params[self.class.name] || {}
85
+ @authorized_params = @@authorized_params[self.class.name] || {}
86
+ @params = @default_params.merge(params.to_h.symbolize_keys).merge(@overrided_params).slice(*@authorized_params)
87
+ end
88
+
89
+ def attrs_only(params)
90
+ attributes.slice(*params)
91
+ end
92
+
93
+ def attributes
94
+ instance_values.with_indifferent_access.symbolize_keys
95
+ end
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_form_objects
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michaël Villeneuve
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ description: Clean up your controller, slim up your models, handle more use cases
42
+ email: contact@michaelvilleneuve.fr
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/active_form_objects.rb
48
+ homepage: https://github.com/fidme/active_form_objects
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.7.3
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A step toward an hexagonal Rails architecture
72
+ test_files: []