methodist 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/methodist/builder.rb +30 -4
- data/lib/methodist/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db3327d005f3ea2e87b22fa4db36cd4a1ebf6183018c4bb3e0f87635f6e1bd69
|
4
|
+
data.tar.gz: 53582ce20974c015108001d61c82f82119709208702c423061aa1591ecae678c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82bc591f3fba95fa0237c194819891ea8f77c4d4fdc15e1164e8faa00f7201073136e2cf99a2f94d95f384133bfaaf4b80506d6998aa8075faf71e9e1b360f7b
|
7
|
+
data.tar.gz: a200d6b5ac5c21ce92a937de325780663fcba50b5a1c1263c880aab63087e54c8d3d65b21579e844b2d5c831a6c2319fb0e4334314cf7f02aecb854e91053a90
|
data/lib/methodist/builder.rb
CHANGED
@@ -18,9 +18,10 @@ class Methodist::Builder < Methodist::Pattern
|
|
18
18
|
# will returns true then #valid_attr? will returns true.
|
19
19
|
#
|
20
20
|
##
|
21
|
-
def field(attr_name, proc = nil)
|
21
|
+
def field(attr_name, proc = nil, **options)
|
22
22
|
attr_accessor(attr_name)
|
23
|
-
|
23
|
+
define_read_method_with_default(attr_name, options[:default])
|
24
|
+
define_write_method(attr_name, proc, options)
|
24
25
|
end
|
25
26
|
|
26
27
|
##
|
@@ -44,12 +45,35 @@ class Methodist::Builder < Methodist::Pattern
|
|
44
45
|
|
45
46
|
alias_method :attrs, :attributes
|
46
47
|
|
48
|
+
def proc_const_name(attr_name)
|
49
|
+
"VALIDATION_PROC_#{attr_name.upcase}"
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
47
54
|
def set_proc_to_const(proc, attr_name)
|
48
55
|
const_set(proc_const_name(attr_name), proc)
|
49
56
|
end
|
50
57
|
|
51
|
-
def
|
52
|
-
|
58
|
+
def define_read_method_with_default(attr_name, default_val)
|
59
|
+
define_method(attr_name) do
|
60
|
+
res = instance_variable_get("@#{attr_name}".to_sym)
|
61
|
+
res.nil? ? default_val : res
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def define_write_method(attr_name, proc, options)
|
66
|
+
set_proc_to_const(proc, attr_name) if proc
|
67
|
+
|
68
|
+
define_method("#{attr_name}=") do |val|
|
69
|
+
instance_variable_set("@#{attr_name}".to_sym, val)
|
70
|
+
return val if valid_attr?(attr_name)
|
71
|
+
instance_variable_set("@#{attr_name}".to_sym, nil)
|
72
|
+
|
73
|
+
raise InvalidValueError, "Value #{val} is not valid for your validation." if options[:raise_invalid]
|
74
|
+
|
75
|
+
nil
|
76
|
+
end
|
53
77
|
end
|
54
78
|
end
|
55
79
|
|
@@ -97,4 +121,6 @@ class Methodist::Builder < Methodist::Pattern
|
|
97
121
|
def get_proc(attr_name)
|
98
122
|
self.class.const_get(self.class.proc_const_name(attr_name)) # rescue nil
|
99
123
|
end
|
124
|
+
|
125
|
+
class InvalidValueError < StandardError; end
|
100
126
|
end
|
data/lib/methodist/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: methodist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nesterov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -170,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
170
170
|
- !ruby/object:Gem::Version
|
171
171
|
version: '0'
|
172
172
|
requirements: []
|
173
|
-
rubygems_version: 3.0.
|
173
|
+
rubygems_version: 3.0.8
|
174
174
|
signing_key:
|
175
175
|
specification_version: 4
|
176
176
|
summary: Gem for Ruby on Rails created to stop chaos in your buisness logic.
|