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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c826e95643c9ec3ec3eb2b326ab2317a7d13c791
4
- data.tar.gz: 0c9b711fdf5c097b2fd7494cd16ab5f7a9bfb0a5
3
+ metadata.gz: c9d3cbd45c8bd9beb62542631fb22dd7ee23a0ea
4
+ data.tar.gz: bf57eeccaff8abaf0422d223d8fddd0625786c71
5
5
  SHA512:
6
- metadata.gz: 96826dbee1d0cc607cb5f2d523e985999f80ac79735e77b04666ab45583acb7eab3df059d64da5045625cf86bea2f6246250530ff55736f7ec5d1764d2183409
7
- data.tar.gz: e0c0032189ca07bd2401e9858c4ac0abdb6bd3d508c21111eebd87d865499088a26ae39b62448ddf61823b77c6beada47725de636e06b2eafba14337eb6b1c22
6
+ metadata.gz: 3dbca6039c04b91e1c6c98da262291f33d98afa6f502be08cab79a38f7570e8951d681e3691dc29ec60c7a2aaf4feacfbdbdf2917d61df852dcfd5d3d0362b08
7
+ data.tar.gz: 6535a3575c0994f6a19227b4cb69bb10c6b0acf2a7102a1a34b42de521992ef354e00b85edd6acc3410faf223e80ed98679d7c6135b3c440d530d46eda823750
data/.gitignore CHANGED
@@ -10,3 +10,5 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ *.gem
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 well_actually
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, :overwrite, :attributes
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:, attributes:)
8
- raise ArgumentError.new("overwrite must be a symbol") unless overwrite.is_a?(Symbol)
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
- @overwrite = overwrite
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(:@@well_actually_overwrite, overwrite)
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, :well_actually_overwrite) do
28
- self.class_variable_get(:@@well_actually_overwrite)
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, :well_actually_overwrite) do
32
- self.public_send(self.class.well_actually_overwrite)
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.well_actually_overwrite || {}).select do |key, value|
55
- self.well_actually_attributes.include?(key) && !value.nil? && (!value.is_a?(String) || !value.match(BLANK_RE))
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
@@ -1,3 +1,3 @@
1
1
  module WellActually
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  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.0
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-09 00:00:00.000000000 Z
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler