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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a4de20033f56a487af8d2b4505604c19bc0c1262c73b81b8e0e6f7fcb3f0dce
4
- data.tar.gz: 1c84021684ebfac6da5c09cfe87e55e83f9e104ec4ac67fc4b9e58a4c01db108
3
+ metadata.gz: db3327d005f3ea2e87b22fa4db36cd4a1ebf6183018c4bb3e0f87635f6e1bd69
4
+ data.tar.gz: 53582ce20974c015108001d61c82f82119709208702c423061aa1591ecae678c
5
5
  SHA512:
6
- metadata.gz: cc2c157aca7127c5d6f8815ea9324dbce2341b5862b377b77bdff553276aa58275e46661c71a7b710d2119a5ebb9e13d160f8b2f68c7c6ee9e306fb03af259af
7
- data.tar.gz: 1d592b18cc8befb21003dea5d43970acfe8fa258d251076f593eb0f83c722f759e4b7b91abca893cad1b1096004a635d1d9b494a63cc07b78d0cc8aa4e53ca31
6
+ metadata.gz: 82bc591f3fba95fa0237c194819891ea8f77c4d4fdc15e1164e8faa00f7201073136e2cf99a2f94d95f384133bfaaf4b80506d6998aa8075faf71e9e1b360f7b
7
+ data.tar.gz: a200d6b5ac5c21ce92a937de325780663fcba50b5a1c1263c880aab63087e54c8d3d65b21579e844b2d5c831a6c2319fb0e4334314cf7f02aecb854e91053a90
@@ -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
- set_proc_to_const(proc, attr_name) if proc
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 proc_const_name(attr_name)
52
- "VALIDATION_PROC_#{attr_name.upcase}"
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
@@ -1,3 +1,3 @@
1
1
  module Methodist
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
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.1.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: 2019-12-15 00:00:00.000000000 Z
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.6
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.