plain_params 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +1 -0
- data/lib/plain_params.rb +70 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8745c3a01ae4ce59b0780fea7dfb57a5a3d67666e1669266d9f7ad85cac81123
|
4
|
+
data.tar.gz: 2b7562a29b959aa4f7515a6ccd9128a9d3ad2209020d4e8febaef59094023a41
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ce5002eb5d8e62039765720973bd89e085bf9fc6f81ef2d18b875622e10b019822c73c00a4b598adc4ad301f10f0af917170fe34c3f66db51507e8a3ed398fa5
|
7
|
+
data.tar.gz: 67ffbd821da90e49875b020c57bf691655bbd27256ecf74a1231f4e647ca5eaf99a9c2d9ed0a9375fe1c6fca5994344c1037d82f1b65b5daba7efd40123d24d0
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# plain_params
|
data/lib/plain_params.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class PlainParams
|
4
|
+
extend ActiveModel::Naming
|
5
|
+
include ActiveModel::Conversion
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
def initialize(params_attributes = {})
|
9
|
+
raise 'No @real_fields or @virtual_fields provided in the class definition' if (self.class.real_fields.nil? and self.class.virtual_fields.nil?)
|
10
|
+
|
11
|
+
self.class.real_fields ||= []
|
12
|
+
self.class.virtual_fields ||= []
|
13
|
+
|
14
|
+
self.attributes = params_attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.real_fields = @real_fields
|
18
|
+
def self.virtual_fields = @virtual_fields
|
19
|
+
|
20
|
+
def values
|
21
|
+
fields_values = {}
|
22
|
+
|
23
|
+
fields_values[:real] = self.class.real_fields.map { |afield| [afield, send(afield)] }.to_h
|
24
|
+
fields_values[:virtual] = self.class.virtual_fields.map { |afield| [afield, send(afield)] }.to_h
|
25
|
+
|
26
|
+
fields_values
|
27
|
+
end
|
28
|
+
|
29
|
+
def real_values = values[:real]
|
30
|
+
def virtual_values = values[:virtual]
|
31
|
+
|
32
|
+
#def json_values = Oj.dump(values, mode: :compat)
|
33
|
+
#def json_values = Oj.dump(values, mode: :compat)
|
34
|
+
def persisted? = false
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def attributes=(attributes)
|
39
|
+
duplicated_fields = self.class.real_fields.intersection(self.class.virtual_fields)
|
40
|
+
raise "Duplicated field(s) '#{duplicated_fields.join(', ')}' in @real_fields and @virtual_fields" if duplicated_fields.any?
|
41
|
+
|
42
|
+
symbolized_attributes = attributes.symbolize_keys
|
43
|
+
|
44
|
+
(symbolized_attributes.keys - (self.class.real_fields + self.class.virtual_fields)).each do |invalid_field|
|
45
|
+
raise "field '#{invalid_field.to_s}' is not in @real_fields or @virtual_fields"
|
46
|
+
end
|
47
|
+
|
48
|
+
# initialized @real_fields first
|
49
|
+
self.class.real_fields.each do |available_field|
|
50
|
+
(instance_variable_set"@#{available_field.to_s}", symbolized_attributes[available_field])
|
51
|
+
self.class.send(:attr_accessor, available_field)
|
52
|
+
self.class.send(:validates_presence_of, available_field)
|
53
|
+
end
|
54
|
+
|
55
|
+
# @virtual_fields comes sencondly for use already initialized @available fields
|
56
|
+
self.class.virtual_fields.each do |virtual_field|
|
57
|
+
unless symbolized_attributes[virtual_field].nil?
|
58
|
+
self.send("#{virtual_field.to_s}=", symbolized_attributes[virtual_field])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.real_fields=(value)
|
64
|
+
@real_fields = value
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.virtual_fields=(value)
|
68
|
+
@virtual_fields = value
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: plain_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gedean Dias
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-04-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.7'
|
41
|
+
description: No ActiveRecord or Dry-Validation, just plain parameters.
|
42
|
+
email: gedean.dias@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- README.md
|
48
|
+
- lib/plain_params.rb
|
49
|
+
homepage: https://github.com/gedean/plain_params
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message: Please check readme file for use instructions.
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubygems_version: 3.5.9
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: No ActiveRecord or Dry-Validation, just plain parameters.
|
72
|
+
test_files: []
|