evt-set_attributes 1.0.0.0 → 1.2.0.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: 0c3ad53825245320c9828c7f47dd1dde09653967a7acb629324f5a07c74621c6
4
- data.tar.gz: 433fd1e2294f1c72fbc695273b8e61b17829aee319ad78740c3f48528aed4888
3
+ metadata.gz: 3a54bb013a3d48de2d9798ad8b266a7c6077179d1f50df8891e7613ce16f6384
4
+ data.tar.gz: be116962b9fd81a5f5a76cc0eb6bb907802e5881f60fc997b02ef9373f326560
5
5
  SHA512:
6
- metadata.gz: bedab1db347c6d6ed003475ac70816557f09e9d7c95cefdbe3e68353b519250faf302d713b8548312cebf81ba1ae7722070593589fdf31e7ac7057fe8c4b2f77
7
- data.tar.gz: ab17a73826d0f180b597abf74eefe80684c829855f8b5cb42a726e36bb010931a6ca2df8c3b7d325f9bed6d25f10b84d56efaf963c8fb11c76cca73e5c8e5711
6
+ metadata.gz: 4f9dd5bdbf33930f0bab8d7b6274012cfc7fb7a80810f70a57888166dcedaad9643e929eb937b246b75fd1b23f42cdbf815b205a412b86b8628e26d05715dfa8
7
+ data.tar.gz: 971a826f7730fa69a67edb9256d311e83ccfe1977d61a56f97f807f6a999e752369de9492605ffe8aefef465d84098468d018bc88a47514535038974d765ff1e
@@ -1,2 +1,6 @@
1
- require 'set_attributes/attribute'
1
+ require 'set_attributes/assign'
2
+ require 'set_attributes/map'
3
+ require 'set_attributes/data_source'
4
+ require 'set_attributes/data_source/hash'
5
+ require 'set_attributes/data_source/object'
2
6
  require 'set_attributes/set_attributes'
@@ -1,14 +1,14 @@
1
1
  class SetAttributes
2
- module Attribute
2
+ module Assign
3
3
  class Error < RuntimeError; end
4
4
 
5
- def self.set(receiver, attribute, value, strict: nil)
5
+ def self.call(receiver, attribute, value, strict: nil)
6
6
  strict = true if strict.nil?
7
7
 
8
8
  setter = :"#{attribute}="
9
9
 
10
- if receiver.respond_to? setter
11
- receiver.send setter, value
10
+ if receiver.respond_to?(setter)
11
+ receiver.send(setter, value)
12
12
  else
13
13
  if strict
14
14
  error_msg = "#{receiver} has no setter for #{attribute}"
@@ -1,2 +1,7 @@
1
+ require 'securerandom'
2
+
3
+ require 'set_attributes/controls/attribute'
1
4
  require 'set_attributes/controls/object'
2
5
  require 'set_attributes/controls/hash'
6
+ require 'set_attributes/controls/mapping'
7
+ require 'set_attributes/controls/data_source'
@@ -0,0 +1,23 @@
1
+ class SetAttributes
2
+ module Controls
3
+ module Attribute
4
+ def self.some_attribute
5
+ 'some value'
6
+ end
7
+
8
+ def self.some_other_attribute
9
+ 'some other value'
10
+ end
11
+
12
+ def self.yet_another_attribute
13
+ 'yet another value'
14
+ end
15
+
16
+ module Random
17
+ def self.example
18
+ "x#{SecureRandom.hex}".to_sym
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ class SetAttributes
2
+ module Controls
3
+ module DataSource
4
+ def self.example(exclude: nil)
5
+ mapping = Controls::Mapping.example
6
+
7
+ source = SetAttributes::Controls::Hash.example
8
+
9
+ data_source = Example.build(source, mapping, exclude: exclude)
10
+
11
+ data_source
12
+ end
13
+
14
+ class Example
15
+ include SetAttributes::DataSource
16
+ end
17
+ end
18
+ end
19
+ end
@@ -2,7 +2,11 @@ class SetAttributes
2
2
  module Controls
3
3
  module Hash
4
4
  def self.example
5
- Controls::Object.example.to_h
5
+ {
6
+ :some_attribute => Attribute.some_attribute,
7
+ :some_other_attribute => Attribute.some_other_attribute,
8
+ :yet_another_attribute => Attribute.yet_another_attribute
9
+ }
6
10
  end
7
11
 
8
12
  module MissingAttribute
@@ -13,11 +17,14 @@ class SetAttributes
13
17
  end
14
18
  end
15
19
 
16
- Example = Struct.new(
17
- :some_attribute,
18
- :some_other_attribute,
19
- :yet_another_attribute
20
- )
20
+ module Transform
21
+ def self.example
22
+ {
23
+ :an_attribute => Attribute.some_attribute,
24
+ :some_other_attribute => Attribute.some_other_attribute
25
+ }
26
+ end
27
+ end
21
28
  end
22
29
  end
23
30
  end
@@ -0,0 +1,44 @@
1
+ class SetAttributes
2
+ module Controls
3
+ module Mapping
4
+ def self.example
5
+ Unary.example
6
+ end
7
+
8
+ module Unary
9
+ def self.example
10
+ [
11
+ :some_attribute,
12
+ :some_other_attribute
13
+ ]
14
+ end
15
+ end
16
+
17
+ module Unbalanced
18
+ def self.example
19
+ [
20
+ :some_attribute,
21
+ {:some_other_attribute => :some_other_attribute}
22
+ ]
23
+ end
24
+ end
25
+
26
+ module Balanced
27
+ def self.example
28
+ [
29
+ {:some_attribute => :some_attribute},
30
+ {:some_other_attribute => :some_other_attribute}
31
+ ]
32
+ end
33
+ end
34
+
35
+ module Transform
36
+ def self.example
37
+ [
38
+ {:some_attribute => :some_other_attribute}
39
+ ]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -4,31 +4,19 @@ class SetAttributes
4
4
  def self.example
5
5
  example = Example.new
6
6
 
7
- example.some_attribute = some_attribute
8
- example.some_other_attribute = some_other_attribute
9
- example.yet_another_attribute = yet_another_attribute
7
+ example.some_attribute = Attribute.some_attribute
8
+ example.some_other_attribute = Attribute.some_other_attribute
9
+ example.yet_another_attribute = Attribute.yet_another_attribute
10
10
 
11
11
  example
12
12
  end
13
13
 
14
- def self.some_attribute
15
- 'some value'
14
+ class Example
15
+ attr_accessor :some_attribute
16
+ attr_accessor :some_other_attribute
17
+ attr_accessor :yet_another_attribute
16
18
  end
17
19
 
18
- def self.some_other_attribute
19
- 'some other value'
20
- end
21
-
22
- def self.yet_another_attribute
23
- 'yet another value'
24
- end
25
-
26
- Example = Struct.new(
27
- :some_attribute,
28
- :some_other_attribute,
29
- :yet_another_attribute
30
- )
31
-
32
20
  module New
33
21
  def self.example
34
22
  Object::Example.new
@@ -39,8 +27,8 @@ class SetAttributes
39
27
  def self.example
40
28
  example = Example.new
41
29
 
42
- example.some_attribute = Object.some_attribute
43
- example.some_other_attribute = Object.some_other_attribute
30
+ example.some_attribute = Attribute.some_attribute
31
+ example.some_other_attribute = Attribute.some_other_attribute
44
32
 
45
33
  example
46
34
  end
@@ -50,6 +38,22 @@ class SetAttributes
50
38
  :some_other_attribute,
51
39
  )
52
40
  end
41
+
42
+ module Transform
43
+ def self.example
44
+ example = Example.new
45
+
46
+ example.an_attribute = Attribute.some_attribute
47
+ example.some_other_attribute = Attribute.some_other_attribute
48
+
49
+ example
50
+ end
51
+
52
+ Example = Struct.new(
53
+ :an_attribute,
54
+ :some_other_attribute,
55
+ )
56
+ end
53
57
  end
54
58
  end
55
59
  end
@@ -0,0 +1,34 @@
1
+ class SetAttributes
2
+ module DataSource
3
+ def self.included(cls)
4
+ cls.class_exec do
5
+ extend Build
6
+ end
7
+ end
8
+
9
+ attr_reader :source
10
+
11
+ def initialize(source)
12
+ @source = source
13
+ end
14
+
15
+ module Build
16
+ def build(source)
17
+ new(source)
18
+ end
19
+ end
20
+
21
+ def self.build_data_source(source)
22
+ data_source_class = implementation(source)
23
+ data_source_class.build(source)
24
+ end
25
+
26
+ def self.implementation(source)
27
+ if source.is_a?(::Hash)
28
+ return DataSource::Hash
29
+ else
30
+ return DataSource::Object
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ class SetAttributes
2
+ module DataSource
3
+ class Hash
4
+ include DataSource
5
+
6
+ def self.verify_mapping(source, include)
7
+ if include.nil?
8
+ return source.keys
9
+ end
10
+
11
+ return include
12
+ end
13
+
14
+ def get_value(attribute)
15
+ source[attribute]
16
+ end
17
+ alias :[] :get_value
18
+
19
+ def attribute?(attribute)
20
+ source.keys.include?(attribute)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ class SetAttributes
2
+ module DataSource
3
+ class Object
4
+ include DataSource
5
+
6
+ Error = Class.new(RuntimeError)
7
+
8
+ def self.verify_mapping(source, include)
9
+ if include.nil?
10
+ raise Error, "Object source is missing the include mapping"
11
+ end
12
+
13
+ return include
14
+ end
15
+
16
+ def get_value(attribute)
17
+ return nil unless attribute?(attribute)
18
+ source.send(attribute)
19
+ end
20
+ alias :[] :get_value
21
+
22
+ def attribute?(attribute)
23
+ source.respond_to?(attribute)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,104 @@
1
+ class SetAttributes
2
+ class Map
3
+ include Enumerable
4
+
5
+ def mappings
6
+ @mappings ||= []
7
+ end
8
+
9
+ def self.build(mappings, exclude: nil)
10
+ mappings ||= []
11
+ mappings = Array(mappings)
12
+
13
+ exclude ||= []
14
+ exclude = Array(exclude)
15
+
16
+ instance = new
17
+
18
+ mappings.each do |mapping|
19
+ instance.add(mapping)
20
+ end
21
+
22
+ instance.exclude(exclude)
23
+
24
+ instance
25
+ end
26
+
27
+ def self.balance_mapping(mapping)
28
+ return mapping if mapping.is_a? Hash
29
+ { mapping => mapping }
30
+ end
31
+
32
+ def [](key)
33
+ mapping = mapping(key)
34
+ values = mapping &.values
35
+
36
+ if values.nil?
37
+ nil
38
+ else
39
+ values[0]
40
+ end
41
+ end
42
+
43
+ def mapping(attribute)
44
+ find do |mapping|
45
+ mapping.keys[0] == attribute
46
+ end
47
+ end
48
+
49
+ def include?(attribute)
50
+ mapping(attribute) != nil
51
+ end
52
+
53
+ def empty?
54
+ count == 0
55
+ end
56
+
57
+ def add(*mappings)
58
+ mappings = Array(mappings).flatten
59
+
60
+ added_mappings = []
61
+ mappings.each do |mapping|
62
+ balanced_mapping = self.class.balance_mapping(mapping)
63
+ self.mappings << balanced_mapping
64
+ added_mappings << balanced_mapping
65
+ end
66
+
67
+ added_mappings
68
+ end
69
+ alias :<< :add
70
+
71
+ def keys
72
+ map do |mapping|
73
+ mapping.keys[0]
74
+ end
75
+ end
76
+
77
+ def delete(*attributes)
78
+ attributes = Array(attributes).flatten
79
+
80
+ deleted_attributes = []
81
+ attributes.each do |attribute|
82
+ mapping = mapping(attribute)
83
+ deleted_attribute = mappings.delete(mapping)
84
+ deleted_attributes << deleted_attribute
85
+ end
86
+
87
+ deleted_attributes
88
+ end
89
+ alias :exclude :delete
90
+
91
+ def each(&action)
92
+ mappings.each(&action)
93
+ end
94
+
95
+ def each_mapping(&action)
96
+ each do |mapping|
97
+ source_attribute = mapping.keys[0]
98
+ receiver_attribute = mapping.values[0]
99
+
100
+ action.(source_attribute, receiver_attribute)
101
+ end
102
+ end
103
+ end
104
+ end
@@ -1,87 +1,52 @@
1
1
  class SetAttributes
2
2
  attr_reader :receiver
3
- attr_reader :data
4
- attr_writer :include
5
- attr_writer :exclude
6
- attr_writer :strict
7
-
8
- def include
9
- @include ||= []
10
- end
11
-
12
- def exclude
13
- @exclude ||= []
14
- end
3
+ attr_reader :data_source
4
+ attr_reader :attribute_map
15
5
 
16
6
  def strict
17
7
  @strict ||= false
18
8
  end
9
+ attr_writer :strict
19
10
 
20
- def initialize(receiver, data)
11
+ def initialize(receiver, data_source, attribute_map)
21
12
  @receiver = receiver
22
- @data = data
13
+ @data_source = data_source
14
+ @attribute_map = attribute_map
23
15
  end
24
16
 
25
- def self.build(receiver, data, copy: nil, include: nil, exclude: nil, strict: nil)
17
+ def self.build(receiver, source, copy: nil, include: nil, exclude: nil, strict: nil)
26
18
  strict ||= false
27
19
 
28
- unless data.respond_to? :to_h
29
- raise ArgumentError, "#{data} can't be used to set attributes. It can't be converted to Hash."
30
- end
31
-
32
- unless data.is_a? Hash
33
- data = data.to_h
34
- end
35
-
36
- exclude ||= []
37
- exclude = Array(exclude)
38
-
39
20
  unless copy.nil?
40
21
  include = copy
41
22
  end
42
23
 
43
- include ||= []
44
- include = Array(include)
45
- include = data.keys if include.empty?
24
+ data_source_implementation = DataSource.implementation(source)
25
+ include = data_source_implementation.verify_mapping(source, include)
26
+
27
+ attribute_map = SetAttributes::Map.build(include, exclude: exclude)
28
+
29
+ data_source = data_source_implementation.build(source)
46
30
 
47
- new(receiver, data).tap do |instance|
48
- instance.include = include
49
- instance.exclude = exclude
31
+ new(receiver, data_source, attribute_map).tap do |instance|
50
32
  instance.strict = strict
51
33
  end
52
34
  end
53
35
 
54
- def self.call(receiver, data, include: nil, copy: nil, exclude: nil, strict: nil)
55
- instance = build(receiver, data, copy: copy, include: include, exclude: exclude, strict: strict)
36
+ def self.call(receiver, source, include: nil, copy: nil, exclude: nil, strict: nil)
37
+ instance = build(receiver, source, copy: copy, include: include, exclude: exclude, strict: strict)
56
38
  instance.()
57
39
  end
58
40
 
59
41
  def call
60
- include_mapping = self.include_mapping
61
- attributes = (data.keys & include_mapping.keys) - exclude
62
-
63
42
  set_attributes = []
64
- attributes.each do |from_attribute|
65
- to_attribute = include_mapping[from_attribute]
66
-
67
- value = data[from_attribute]
68
43
 
69
- Attribute.set(receiver, to_attribute, value, strict: strict)
70
-
71
- set_attributes << to_attribute
44
+ attribute_map.each_mapping do |source_attribute, receiver_attribute|
45
+ value = data_source.get_value(source_attribute)
46
+ Assign.(receiver, receiver_attribute, value, strict: strict)
47
+ set_attributes << receiver_attribute
72
48
  end
73
- set_attributes
74
- end
75
49
 
76
- def include_mapping
77
- mapping = {}
78
- include.each do |item|
79
- if item.is_a? Hash
80
- mapping[item.keys.first] = item.values.first
81
- else
82
- mapping[item] = item
83
- end
84
- end
85
- mapping
50
+ set_attributes
86
51
  end
87
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt-set_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.0
4
+ version: 1.2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Eventide Project
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-17 00:00:00.000000000 Z
11
+ date: 2019-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test_bench
@@ -31,10 +31,17 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/set_attributes.rb
34
- - lib/set_attributes/attribute.rb
34
+ - lib/set_attributes/assign.rb
35
35
  - lib/set_attributes/controls.rb
36
+ - lib/set_attributes/controls/attribute.rb
37
+ - lib/set_attributes/controls/data_source.rb
36
38
  - lib/set_attributes/controls/hash.rb
39
+ - lib/set_attributes/controls/mapping.rb
37
40
  - lib/set_attributes/controls/object.rb
41
+ - lib/set_attributes/data_source.rb
42
+ - lib/set_attributes/data_source/hash.rb
43
+ - lib/set_attributes/data_source/object.rb
44
+ - lib/set_attributes/map.rb
38
45
  - lib/set_attributes/set_attributes.rb
39
46
  homepage: https://github.com/eventide-project/set-attributes
40
47
  licenses:
@@ -55,9 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
62
  - !ruby/object:Gem::Version
56
63
  version: '0'
57
64
  requirements: []
58
- rubyforge_project:
59
- rubygems_version: 2.7.3
65
+ rubygems_version: 3.0.1
60
66
  signing_key:
61
67
  specification_version: 4
62
- summary: Set an object's attributes from a hash or an object that implements to_h
68
+ summary: Set an object's attributes from a source object or hash
63
69
  test_files: []