eac_ruby_utils 0.39.0 → 0.40.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: f2a4b0d92b253248df0a2e7ec093f1056a822602cfcce7376d2b91e28b9f2d22
4
- data.tar.gz: b0590f577360628d7f399981270a2a46914e3398ffb8af511a36ac40f67a9494
3
+ metadata.gz: 60224aa50261e8212946f7fb3cbe3efd1bef3ee6853bf827d78ca18810d81fc9
4
+ data.tar.gz: 2da01ff57939fc85b13fab89241c711526726681e547d0194565cad04b9ce811
5
5
  SHA512:
6
- metadata.gz: c42cfcbfb7bcc6a0ed2d6df02959de8496e131cbe1d2448e1ebeffa8b2dab284dcb4cdc535605eeaff9efdae0134aa473458b570ab511db4e1e41b6fe9d6af5b
7
- data.tar.gz: d989ccc189773585f71ad08f42f41400d3bddd8caf4914f452abffb28e0640f7bc64359c8374dfe20af115ecbbc48317ce623d3533ca1be947344d5b4c43db03
6
+ metadata.gz: 35bd1179d9dfdaf7f741f2cf054a379ed37018318955397996bdc8acd484d53b602efc9d383d2c9ee866db00dc306843c48e5c282812f56d08439a1f286c5b78
7
+ data.tar.gz: 4a937089189a97671ccfa8e30499fa0ddcc69843bdfd6a96232594e48c2019f20bcc02e653c597e2f086c6990437cb5c9d13b58287c4dc87817b0ea5d2a875e9
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/filesystem_cache'
4
+ require 'tmpdir'
4
5
 
5
6
  module EacRubyUtils
6
7
  class << self
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/module/common_concern'
4
+ require 'eac_ruby_utils/require_sub'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ ::EacRubyUtils.require_sub __FILE__
9
+
10
+ common_concern do
11
+ include ::EacRubyUtils::Listable
12
+ lists.add_symbol :type, :array, :boolean, :common, :hash
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/inflector'
4
+ require 'eac_ruby_utils/immutable/base_accessor'
5
+ require 'eac_ruby_utils/patches/class/common_constructor'
6
+
7
+ module EacRubyUtils
8
+ module Immutable
9
+ class ArrayAccessor < ::EacRubyUtils::Immutable::BaseAccessor
10
+ def apply(klass)
11
+ accessor = self
12
+ klass.send(:define_method, name) do |value|
13
+ accessor.immutable_value_set(self, value)
14
+ end
15
+
16
+ klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do
17
+ accessor.immutable_value_get(self)
18
+ end
19
+ end
20
+
21
+ def immutable_value_set(object, value)
22
+ duplicate_object(object) do |old_value|
23
+ (old_value || []) + [value]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ class BaseAccessor
6
+ common_constructor :name do
7
+ self.name = name.to_sym
8
+ end
9
+
10
+ def duplicate_object(object)
11
+ accessor_new_value = yield(immutable_value_get(object))
12
+ new_values = object.send(:immutable_values_get).merge(name => accessor_new_value)
13
+ r = object.class.new(*object.immutable_constructor_args)
14
+ r.send(:immutable_values_set, new_values)
15
+ r
16
+ end
17
+
18
+ def immutable_value_get(object)
19
+ object.send(:immutable_values_get)[name]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/common_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class BooleanAccessor < ::EacRubyUtils::Immutable::CommonAccessor
9
+ def apply(klass)
10
+ super
11
+ accessor = self
12
+ klass.send(:define_method, "#{name}?") { send(accessor.name) }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ module ClassMethods
6
+ def immutable_accessor(*accessors)
7
+ options = accessors.extract_options!
8
+ options[:type] ||= const_get('TYPE_COMMON')
9
+ accessors.each do |name|
10
+ class_name = options.fetch(:type).to_s.camelize + 'Accessor'
11
+ ::EacRubyUtils::Immutable.const_get(class_name).new(name).apply(self)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def imutable_single_accessor(name, options); end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/base_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class CommonAccessor < ::EacRubyUtils::Immutable::BaseAccessor
9
+ def apply(klass)
10
+ accessor = self
11
+ klass.send(:define_method, name) do |*args|
12
+ case args.count
13
+ when 0 then next accessor.immutable_value_get(self)
14
+ when 1 then next accessor.immutable_value_set(self, args.first)
15
+ else
16
+ raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 0..1)"
17
+ end
18
+ end
19
+ end
20
+
21
+ def immutable_value_get(object)
22
+ object.send(:immutable_values_get)[name]
23
+ end
24
+
25
+ def immutable_value_set(object, value)
26
+ duplicate_object(object) { |_old_value| value }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/base_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class HashAccessor < ::EacRubyUtils::Immutable::BaseAccessor
9
+ def apply(klass)
10
+ apply_get(klass)
11
+ apply_set(klass)
12
+ end
13
+
14
+ def immutable_value_set(object, key, value)
15
+ duplicate_object(object) do |old_value|
16
+ (old_value || {}).merge(key => value)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def apply_get(klass)
23
+ accessor = self
24
+ klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do
25
+ accessor.immutable_value_get(self)
26
+ end
27
+ end
28
+
29
+ def apply_set(klass)
30
+ accessor = self
31
+ klass.send(:define_method, name) do |*args|
32
+ case args.count
33
+ when 1 then next accessor.immutable_value_get(self, args[0])
34
+ when 2 then next accessor.immutable_value_set(self, *args[0..1])
35
+ else
36
+ raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 1..2)"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ module InstanceMethods
6
+ def immutable_constructor_args
7
+ []
8
+ end
9
+
10
+ private
11
+
12
+ def immutable_values_get
13
+ @immutable_values || {}
14
+ end
15
+
16
+ def immutable_values_set(new_values)
17
+ @immutable_values = new_values
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patch'
4
+ require 'eac_ruby_utils/immutable'
5
+
6
+ class Module
7
+ def enable_immutable
8
+ ::EacRubyUtils.patch(self, ::EacRubyUtils::Immutable)
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.39.0'
4
+ VERSION = '0.40.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_ruby_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.39.0
4
+ version: 0.40.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-06 00:00:00.000000000 Z
11
+ date: 2020-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -148,6 +148,14 @@ files:
148
148
  - lib/eac_ruby_utils/fs/traversable.rb
149
149
  - lib/eac_ruby_utils/fs/traverser.rb
150
150
  - lib/eac_ruby_utils/fs_cache.rb
151
+ - lib/eac_ruby_utils/immutable.rb
152
+ - lib/eac_ruby_utils/immutable/array_accessor.rb
153
+ - lib/eac_ruby_utils/immutable/base_accessor.rb
154
+ - lib/eac_ruby_utils/immutable/boolean_accessor.rb
155
+ - lib/eac_ruby_utils/immutable/class_methods.rb
156
+ - lib/eac_ruby_utils/immutable/common_accessor.rb
157
+ - lib/eac_ruby_utils/immutable/hash_accessor.rb
158
+ - lib/eac_ruby_utils/immutable/instance_methods.rb
151
159
  - lib/eac_ruby_utils/listable.rb
152
160
  - lib/eac_ruby_utils/listable/class_methods.rb
153
161
  - lib/eac_ruby_utils/listable/instance_methods.rb
@@ -171,6 +179,7 @@ files:
171
179
  - lib/eac_ruby_utils/patches/module.rb
172
180
  - lib/eac_ruby_utils/patches/module/common_concern.rb
173
181
  - lib/eac_ruby_utils/patches/module/console_speaker.rb
182
+ - lib/eac_ruby_utils/patches/module/immutable.rb
174
183
  - lib/eac_ruby_utils/patches/module/listable.rb
175
184
  - lib/eac_ruby_utils/patches/module/patch.rb
176
185
  - lib/eac_ruby_utils/patches/module/require_sub.rb