well-actually 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +5 -1
- data/lib/well_actually/mounter.rb +24 -11
- data/lib/well_actually/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9d3cbd45c8bd9beb62542631fb22dd7ee23a0ea
|
4
|
+
data.tar.gz: bf57eeccaff8abaf0422d223d8fddd0625786c71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dbca6039c04b91e1c6c98da262291f33d98afa6f502be08cab79a38f7570e8951d681e3691dc29ec60c7a2aaf4feacfbdbdf2917d61df852dcfd5d3d0362b08
|
7
|
+
data.tar.gz: 6535a3575c0994f6a19227b4cb69bb10c6b0acf2a7102a1a34b42de521992ef354e00b85edd6acc3410faf223e80ed98679d7c6135b3c440d530d46eda823750
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
## Installation
|
2
2
|
|
3
|
+
Is tested with pure ruby classes and classes that inherit from ActiveRecord::Base, rails >= 4 supported.
|
4
|
+
|
3
5
|
Add this line to your application's Gemfile:
|
4
6
|
|
5
7
|
```ruby
|
@@ -12,12 +14,14 @@ And then execute:
|
|
12
14
|
|
13
15
|
Or install it yourself as:
|
14
16
|
|
15
|
-
$ gem install
|
17
|
+
$ gem install well-actually
|
16
18
|
|
17
19
|
## Usage
|
18
20
|
```ruby
|
19
21
|
Dog < ActiveRecord::Base
|
20
22
|
extend WellActually
|
23
|
+
# overwrite or overwrites option can be a symbol or an array of symbol, earlier takes precedence over later
|
24
|
+
# attributes must be an array of symbols
|
21
25
|
well_actually overwrite: :overwrite, attributes: [:name, :show, :birthday]
|
22
26
|
end
|
23
27
|
|
@@ -1,20 +1,21 @@
|
|
1
1
|
module WellActually
|
2
2
|
class Mounter
|
3
|
-
attr_reader :klass, :
|
3
|
+
attr_reader :klass, :overwrites, :attributes
|
4
4
|
|
5
5
|
BLANK_RE = defined?(String::BLANK_RE) ? String::BLANK_RE : /\A[[:space:]]*\z/
|
6
6
|
|
7
|
-
def initialize(klass:, overwrite
|
8
|
-
|
7
|
+
def initialize(klass:, overwrites: nil, overwrite: nil, attributes:)
|
8
|
+
overwrites = overwrite || overwrites
|
9
|
+
raise ArgumentError.new("overwrite must be a symbol or an array of symbols") unless overwrites.is_a?(Symbol) || (overwrites.is_a?(Array) && overwrites.all?{|a| a.is_a?(Symbol)})
|
9
10
|
raise ArgumentError.new("attributes must be an array of symbols") unless attributes.is_a?(Array) && attributes.all?{|a| a.is_a?(Symbol)}
|
10
11
|
@klass = klass
|
11
|
-
@
|
12
|
+
@overwrites = overwrites
|
12
13
|
@attributes = attributes.uniq
|
13
14
|
end
|
14
15
|
|
15
16
|
def mount
|
16
17
|
klass.class_variable_set(:@@well_actually_attributes, attributes.map{|_attr| _attr.to_s})
|
17
|
-
klass.class_variable_set(:@@
|
18
|
+
klass.class_variable_set(:@@well_actually_overwrites, [*overwrites])
|
18
19
|
|
19
20
|
klass.send(:define_singleton_method, :well_actually_attributes) do
|
20
21
|
self.class_variable_get(:@@well_actually_attributes)
|
@@ -24,12 +25,20 @@ module WellActually
|
|
24
25
|
self.class.well_actually_attributes
|
25
26
|
end
|
26
27
|
|
27
|
-
klass.send(:define_singleton_method, :
|
28
|
-
self.class_variable_get(:@@
|
28
|
+
klass.send(:define_singleton_method, :well_actually_overwrites) do
|
29
|
+
self.class_variable_get(:@@well_actually_overwrites)
|
29
30
|
end
|
30
31
|
|
31
|
-
klass.send(:define_method, :
|
32
|
-
self.
|
32
|
+
klass.send(:define_method, :well_actually_overwrites) do
|
33
|
+
self.class.well_actually_overwrites.map do |overwrite|
|
34
|
+
self.public_send(overwrite)
|
35
|
+
end.reduce({}) do |result, overwrite|
|
36
|
+
result.merge(overwrite || {}) do |key, v1, v2|
|
37
|
+
v1 = nil unless self.well_actually_overwrite_value_check(key, v1)
|
38
|
+
v2 = nil unless self.well_actually_overwrite_value_check(key, v2)
|
39
|
+
v1.nil? ? v2 : v1
|
40
|
+
end
|
41
|
+
end
|
33
42
|
end
|
34
43
|
|
35
44
|
klass.send(:define_method, :well_actually?) do
|
@@ -51,10 +60,14 @@ module WellActually
|
|
51
60
|
end
|
52
61
|
|
53
62
|
klass.send(:define_method, :well_actually_overwrite_safe) do
|
54
|
-
(self.
|
55
|
-
self.
|
63
|
+
(self.well_actually_overwrites || {}).select do |key, value|
|
64
|
+
self.well_actually_overwrite_value_check(key, value)
|
56
65
|
end
|
57
66
|
end
|
67
|
+
|
68
|
+
klass.send(:define_method, :well_actually_overwrite_value_check) do |key, value|
|
69
|
+
self.well_actually_attributes.include?(key) && !value.nil? && (!value.is_a?(String) || !value.match(BLANK_RE))
|
70
|
+
end
|
58
71
|
end
|
59
72
|
end
|
60
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: well-actually
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Rove
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|