u-attributes 0.12.0 → 0.13.0

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
  SHA256:
3
- metadata.gz: '0981953778c6a7130c83e54de1e8c5a992def3ee434b673cfc80c5cb2fb65298'
4
- data.tar.gz: 6219cc89e480e32fd28091999b882566aa6374f7e2c9b27a1dffab1881f8ad79
3
+ metadata.gz: 386780594f0b0289f62e38984d59f847901e5257002fa4d1124a570304cf792e
4
+ data.tar.gz: 1d66e4474bbdd5a027ba91898ff3990ed72d76b2278f7c8e4ab303dacbf5b1a8
5
5
  SHA512:
6
- metadata.gz: e09afedf86b00b99c76bacc9ace249177259b7ed62e3452bef4b7418f08be6ba75cc1f5c5487015d102b4677f2c9cd23bba9221547dde5f0f600ad47a4a98a45
7
- data.tar.gz: b284987930d5092a3158a112d2c6e17f11b6cfb873acdc73023560592c5fcc03e29b331097be49b171c3ac1524895510db14cef1bd7c0d6546801b2f6c0e1350
6
+ metadata.gz: abe5248e77a398a3dbb14047c0ecfdd1fec83e5aa59cde4c2a2c99704c2a503f059f484682654e20d04f6e3cf2ae3c3038cf0103ab51922948375d1e58495917
7
+ data.tar.gz: 91152ebf4dafe6ce4c5a563068bee973b3c334b6600c8c16a81f62e98a120fc07116426c8d163a75b072561da25a50fe71c097fdcdf65c05d120c1ae6ec1bdc5
data/Gemfile CHANGED
@@ -16,8 +16,8 @@ activemodel = case activemodel_version
16
16
  end
17
17
 
18
18
  if activemodel_version < '6.1'
19
- gem 'activemodel', activemodel
20
- gem 'activesupport', activemodel
19
+ gem 'activemodel', activemodel, require: false
20
+ gem 'activesupport', activemodel, require: false
21
21
  end
22
22
 
23
23
  minitest = activemodel_version < '4.1' ? '~> 4.2' : '~> 5.0'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-attributes (0.12.0)
4
+ u-attributes (0.13.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -400,7 +400,7 @@ p job_changes.changed?(:state, from: 'sleeping', to: 'running') # true
400
400
  #----------------#
401
401
  # #differences() #
402
402
  #----------------#
403
- p job_changes.differences # {"state"=>"running"}
403
+ p job_changes.differences # {"state"=> {"from" => "sleeping", "to" => "running"}}
404
404
  ```
405
405
 
406
406
  ## Development
@@ -3,7 +3,7 @@
3
3
  module Micro
4
4
  module Attributes
5
5
  module AttributesUtils
6
- ARGUMENT_ERROR_MSG = 'argument must be a Hash'
6
+ ARGUMENT_ERROR_MSG = 'argument must be a Hash'.freeze
7
7
 
8
8
  def self.hash_argument!(arg)
9
9
  return arg if arg.is_a?(Hash)
@@ -3,8 +3,10 @@
3
3
  module Micro::Attributes
4
4
  module Features
5
5
  module ActiveModelValidations
6
- @@__active_model_load_error = false
7
6
  @@__active_model_required = false
7
+ @@__active_model_load_error = false
8
+
9
+ V32 = '3.2'
8
10
 
9
11
  def self.included(base)
10
12
  if !@@__active_model_load_error && !@@__active_model_required
@@ -19,16 +21,18 @@ module Micro::Attributes
19
21
  unless @@__active_model_load_error
20
22
  base.send(:include, ::ActiveModel::Validations)
21
23
 
22
- if ::ActiveModel::VERSION::STRING >= '3.2'
24
+ if ::ActiveModel::VERSION::STRING >= V32
23
25
  base.class_eval(<<-RUBY)
24
- def initialize(options)
25
- self.attributes=(options)
26
+ def initialize(arg)
27
+ self.attributes=arg
26
28
  run_validations!
27
29
  end
28
30
  RUBY
29
31
  end
30
32
  end
31
33
  end
34
+
35
+ private_constant :V32
32
36
  end
33
37
  end
34
38
  end
@@ -4,6 +4,10 @@ module Micro::Attributes
4
4
  module Features
5
5
  module Diff
6
6
  class Changes
7
+ TO = 'to'.freeze
8
+ FROM = 'from'.freeze
9
+ FROM_TO_ERROR = 'pass the attribute name with the :from and :to values'.freeze
10
+
7
11
  attr_reader :from, :to, :differences
8
12
 
9
13
  def initialize(from:, to:)
@@ -24,32 +28,34 @@ module Micro::Attributes
24
28
  def changed?(name = nil, from: nil, to: nil)
25
29
  if name.nil?
26
30
  return present? if from.nil? && to.nil?
27
- raise ArgumentError, 'pass the attribute name with the :from and :to values'
31
+ raise ArgumentError, FROM_TO_ERROR
28
32
  elsif from.nil? && to.nil?
29
33
  differences.has_key?(name.to_s)
30
34
  else
31
- key = name.to_s
32
- @from_attributes[key] == from && @to_attributes[key] == to
35
+ result = @differences[name.to_s]
36
+ result ? result[FROM] == from && result[TO] == to : false
33
37
  end
34
38
  end
35
39
 
36
40
  private
37
41
 
38
42
  def diff(from_attributes, to_attributes)
39
- @to_attributes = to_attributes
40
- @from_attributes = from_attributes
43
+ @from_attributes, @to_attributes = from_attributes, to_attributes
41
44
  @from_attributes.each_with_object({}) do |(from_key, from_val), acc|
42
45
  to_value = @to_attributes[from_key]
43
- acc[from_key] = to_value if from_val != to_value
46
+ acc[from_key] = {FROM => from_val, TO => to_value}.freeze if from_val != to_value
44
47
  end
45
48
  end
49
+
50
+ private_constant :TO, :FROM, :FROM_TO_ERROR
46
51
  end
47
- private_constant :Changes
48
52
 
49
53
  def diff_attributes(to)
50
54
  return Changes.new(from: self, to: to) if to.is_a?(::Micro::Attributes)
51
55
  raise ArgumentError, "#{to.inspect} must implement Micro::Attributes"
52
56
  end
57
+
58
+ private_constant :Changes
53
59
  end
54
60
  end
55
61
  end
@@ -4,11 +4,11 @@ module Micro::Attributes
4
4
  module Features
5
5
  module Initialize
6
6
  def self.included(base)
7
- base.send(:include, ::Micro::Attributes)
8
- end
9
-
10
- def initialize(arg)
11
- self.attributes = arg
7
+ base.class_eval(<<-RUBY)
8
+ def initialize(arg)
9
+ self.attributes = arg
10
+ end
11
+ RUBY
12
12
  end
13
13
 
14
14
  def with_attribute(key, val)
@@ -1,63 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "micro/attributes/features/diff"
4
- require "micro/attributes/features/initialize"
5
- require "micro/attributes/features/activemodel_validations"
3
+ require "micro/attributes/with"
6
4
 
7
5
  module Micro
8
6
  module Attributes
9
7
  module Features
10
- module InitializeAndDiff
11
- def self.included(base)
12
- base.send(:include, ::Micro::Attributes::Features::Initialize)
13
- base.send(:include, ::Micro::Attributes::Features::Diff)
14
- end
15
- end
16
-
17
- module ActiveModelValidationsAndDiff
18
- def self.included(base)
19
- base.send(:include, ::Micro::Attributes::Features::Diff)
20
- base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
21
- end
22
- end
23
-
24
- module ActiveModelValidationsAndInitialize
25
- def self.included(base)
26
- base.send(:include, ::Micro::Attributes::Features::Initialize)
27
- base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
28
- end
29
- end
30
-
31
- module ActiveModelValidationsAndDiffAndInitialize
32
- def self.included(base)
33
- base.send(:include, ::Micro::Attributes::Features::Initialize)
34
- base.send(:include, ::Micro::Attributes::Features::Diff)
35
- base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
36
- end
37
- end
8
+ INVALID_FEATURES = 'Invalid feature name! Available options: :initialize, :diff, :activemodel_validations'.freeze
38
9
 
39
10
  OPTIONS = {
40
11
  # Features
41
- 'diff' => Diff,
42
- 'initialize' => Initialize,
43
- 'activemodel_validations' => ActiveModelValidations,
12
+ 'diff' => With::Diff,
13
+ 'initialize' => With::Initialize,
14
+ 'activemodel_validations' => With::ActiveModelValidations,
44
15
  # Combinations
45
- 'diff:initialize' => InitializeAndDiff,
46
- 'activemodel_validations:diff' => ActiveModelValidationsAndDiff,
47
- 'activemodel_validations:initialize' => ActiveModelValidationsAndInitialize,
48
- 'activemodel_validations:diff:initialize' => ActiveModelValidationsAndDiffAndInitialize
16
+ 'diff:initialize' => With::DiffAndInitialize,
17
+ 'activemodel_validations:diff' => With::ActiveModelValidationsAndDiff,
18
+ 'activemodel_validations:initialize' => With::ActiveModelValidationsAndInitialize,
19
+ 'activemodel_validations:diff:initialize' => With::ActiveModelValidationsAndDiffAndInitialize
49
20
  }.freeze
50
21
 
51
22
  private_constant :OPTIONS
52
23
 
53
24
  def self.all
54
- ActiveModelValidationsAndDiffAndInitialize
25
+ With::ActiveModelValidationsAndDiffAndInitialize
55
26
  end
56
27
 
57
28
  def self.fetch(names)
58
29
  option = OPTIONS[names.map { |name| name.to_s.downcase }.sort.join(':')]
59
30
  return option if option
60
- raise ArgumentError, 'Invalid feature name! Available options: diff, initialize, activemodel_validations'
31
+ raise ArgumentError, INVALID_FEATURES
61
32
  end
62
33
  end
63
34
  end
@@ -50,14 +50,18 @@ module Micro
50
50
  end
51
51
 
52
52
  module ForSubclasses
53
+ WRONG_NUMBER_OF_ARGS = 'wrong number of arguments (given 0, expected 1 or more)'.freeze
54
+
53
55
  def attribute!(name, value=nil)
54
56
  __attribute_set(name, value, true)
55
57
  end
56
58
 
57
59
  def attributes!(*args)
58
60
  return __attributes_set(args, can_overwrite: true) unless args.empty?
59
- raise ArgumentError, 'wrong number of arguments (given 0, expected 1 or more)'
61
+ raise ArgumentError, WRONG_NUMBER_OF_ARGS
60
62
  end
63
+
64
+ private_constant :WRONG_NUMBER_OF_ARGS
61
65
  end
62
66
  private_constant :ForSubclasses
63
67
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- VERSION = "0.12.0"
5
+ VERSION = '0.13.0'.freeze
6
6
  end
7
7
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'micro/attributes/features/diff'
4
+ require 'micro/attributes/features/initialize'
5
+ require 'micro/attributes/features/activemodel_validations'
6
+
7
+ module Micro
8
+ module Attributes
9
+ module With
10
+ #
11
+ # Features
12
+ #
13
+ module Diff
14
+ def self.included(base)
15
+ base.send(:include, ::Micro::Attributes)
16
+ base.send(:include, ::Micro::Attributes::Features::Diff)
17
+ end
18
+ end
19
+
20
+ module Initialize
21
+ def self.included(base)
22
+ base.send(:include, ::Micro::Attributes)
23
+ base.send(:include, ::Micro::Attributes::Features::Initialize)
24
+ end
25
+ end
26
+
27
+ module ActiveModelValidations
28
+ def self.included(base)
29
+ base.send(:include, ::Micro::Attributes)
30
+ base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
31
+ end
32
+ end
33
+
34
+ #
35
+ # Combinations
36
+ #
37
+ module DiffAndInitialize
38
+ def self.included(base)
39
+ base.send(:include, ::Micro::Attributes)
40
+ base.send(:include, ::Micro::Attributes::Features::Initialize)
41
+ base.send(:include, ::Micro::Attributes::Features::Diff)
42
+ end
43
+ end
44
+
45
+ module ActiveModelValidationsAndDiff
46
+ def self.included(base)
47
+ base.send(:include, ::Micro::Attributes)
48
+ base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
49
+ base.send(:include, ::Micro::Attributes::Features::Diff)
50
+ end
51
+ end
52
+
53
+ module ActiveModelValidationsAndInitialize
54
+ def self.included(base)
55
+ base.send(:include, ::Micro::Attributes)
56
+ base.send(:include, ::Micro::Attributes::Features::Initialize)
57
+ base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
58
+ end
59
+ end
60
+
61
+ module ActiveModelValidationsAndDiffAndInitialize
62
+ def self.included(base)
63
+ base.send(:include, ::Micro::Attributes)
64
+ base.send(:include, ::Micro::Attributes::Features::Initialize)
65
+ base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
66
+ base.send(:include, ::Micro::Attributes::Features::Diff)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -18,7 +18,7 @@ module Micro
18
18
 
19
19
  def base.inherited(subclass)
20
20
  subclass.attributes(self.attributes_data({}))
21
- subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses')
21
+ subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses'.freeze)
22
22
  end
23
23
  end
24
24
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-10 00:00:00.000000000 Z
11
+ date: 2019-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -51,6 +51,7 @@ files:
51
51
  - lib/micro/attributes/features/initialize.rb
52
52
  - lib/micro/attributes/macros.rb
53
53
  - lib/micro/attributes/version.rb
54
+ - lib/micro/attributes/with.rb
54
55
  - lib/u-attributes.rb
55
56
  - run-tests.sh
56
57
  - u-attributes.gemspec