structure 1.0.3 → 1.1.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
- SHA1:
3
- metadata.gz: 3fb544bc3e97d170691465e39a8d28ea680632c7
4
- data.tar.gz: 34683dd3f6c1693e6c8e7c379d0433153a4ed326
2
+ SHA256:
3
+ metadata.gz: 82ce9608139683b5c96c2b9168b1784d75d22bb4b0ddbb40b2d223858edbb436
4
+ data.tar.gz: 8efee424104293918c1ba7489278a870ec445cfa88b3e1bd740267950d6580f7
5
5
  SHA512:
6
- metadata.gz: 2df2844425af4ac85b2b7a011d570fedba08fa5aa60abcfa5e3f07b82c4f7f90a3ee1977838619e9c6b8ec667e69f38ac021fe774b79ecb01f59e591fcf5ce48
7
- data.tar.gz: 05aee48f02e8c9a0c6c63324ea81f1b8a6247b9eb8cde7b88ec847420c4e2cee60dbc183133fd0168066a2bfbaa3f1e5cbb81a6fba336114e313bb3a5ee64afc
6
+ metadata.gz: f89bdd35530daa965b36fd56a92d96e1beff21508080efbb4e3306738a0d77a4141e734a3e55fcf18fb26c34bef5893370f08d2fe33bc913002c7061a972ce8a
7
+ data.tar.gz: 97490884a508a5920ca3bda6583ace8c47da12178b56ff038afe34bac0e16b10abb5337ad1475cf9847d7d4218b9d56528341ce67fa1eb8162415601add2e25b
@@ -1,32 +1,17 @@
1
- # Structure
2
- module Structure
3
- class << self
4
- private
1
+ # frozen_string_literal: true
5
2
 
6
- def included(base)
7
- base.extend(ClassMethods).instance_variable_set(:@attribute_names, [])
8
- end
3
+ require 'structure/class_methods'
4
+ require 'structure/utils'
5
+
6
+ module Structure
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ base.__overwrite_initialize__
9
10
  end
10
11
 
11
12
  def attributes
12
- attribute_names.reduce({}) do |hash, key|
13
- value = send(key)
14
- hash.update(
15
- key =>
16
- if value.respond_to?(:attributes)
17
- value.attributes
18
- elsif value.is_a?(Array)
19
- value.map do |element|
20
- if element.respond_to?(:attributes)
21
- element.attributes
22
- else
23
- element
24
- end
25
- end
26
- else
27
- value
28
- end
29
- )
13
+ attribute_names.each_with_object({}) do |key, hash|
14
+ hash[key] = Utils.serialize(send(key))
30
15
  end
31
16
  end
32
17
 
@@ -36,24 +21,25 @@ module Structure
36
21
 
37
22
  def ==(other)
38
23
  return false unless other.respond_to?(:attributes)
24
+
39
25
  attributes == other.attributes
40
26
  end
41
27
 
42
28
  def inspect
43
- name = self.class.name || self.class.to_s.gsub(/[^\w:]/, "")
29
+ name = self.class.name || self.class.to_s.gsub(/[^\w:]/, '')
44
30
  values =
45
31
  attribute_names
46
32
  .map do |key|
47
33
  value = send(key)
48
- if value.is_a?(Array)
49
- description = value.take(3).map(&:inspect).join(", ")
50
- description += "..." if value.size > 3
34
+ if value.is_a?(::Array)
35
+ description = value.take(3).map(&:inspect).join(', ')
36
+ description += '...' if value.size > 3
51
37
  "#{key}=[#{description}]"
52
38
  else
53
39
  "#{key}=#{value.inspect}"
54
40
  end
55
41
  end
56
- .join(", ")
42
+ .join(', ')
57
43
 
58
44
  "#<#{name} #{values}>"
59
45
  end
@@ -61,44 +47,4 @@ module Structure
61
47
  alias to_h attributes
62
48
  alias eql? ==
63
49
  alias to_s inspect
64
-
65
- # ClassMethods
66
- module ClassMethods
67
- attr_reader :attribute_names
68
-
69
- def attribute(name)
70
- name = name.to_s
71
-
72
- if name.chomp!("?")
73
- module_eval(<<-EOS, __FILE__, __LINE__)
74
- def #{name}?
75
- #{name}
76
- end
77
- EOS
78
- end
79
-
80
- module_eval(<<-EOS, __FILE__, __LINE__)
81
- def #{name}
82
- return @#{name} if defined?(@#{name})
83
- @#{name} = _#{name}
84
- @#{name}.freeze unless @#{name}.is_a?(Structure)
85
-
86
- @#{name}
87
- end
88
- EOS
89
-
90
- define_method("_#{name}", Proc.new)
91
- private "_#{name}"
92
-
93
- @attribute_names << name
94
-
95
- name.to_sym
96
- end
97
-
98
- private
99
-
100
- def inherited(subclass)
101
- subclass.instance_variable_set(:@attribute_names, @attribute_names.dup)
102
- end
103
- end
104
50
  end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Structure
4
+ module ClassMethods
5
+ attr_reader :attribute_names
6
+
7
+ def self.extended(base)
8
+ base.instance_variable_set :@attribute_names, []
9
+ end
10
+
11
+ def attribute(name, &block)
12
+ name = name.to_s
13
+
14
+ if name.chomp!('?')
15
+ module_eval <<-CODE, __FILE__, __LINE__ + 1
16
+ def #{name}?
17
+ #{name}
18
+ end
19
+ CODE
20
+ end
21
+
22
+ module_eval <<-CODE, __FILE__, __LINE__ + 1
23
+ def #{name}
24
+ @__mutex__.synchronize {
25
+ return @#{name} if defined?(@#{name})
26
+
27
+ @#{name} = __#{name}__
28
+ @#{name}.freeze unless @#{name}.is_a?(Structure)
29
+
30
+ @#{name}
31
+ }
32
+ end
33
+ CODE
34
+
35
+ define_method "__#{name}__", block
36
+ private "__#{name}__"
37
+
38
+ @attribute_names << name
39
+
40
+ name.to_sym
41
+ end
42
+
43
+ def __overwrite_initialize__
44
+ class_eval do
45
+ unless method_defined?(:__custom_initialize__)
46
+ define_method :__custom_initialize__ do |*args|
47
+ @__mutex__ = ::Thread::Mutex.new
48
+ __original_initialize__(*args)
49
+ end
50
+ end
51
+
52
+ return if instance_method(:initialize) ==
53
+ instance_method(:__custom_initialize__)
54
+
55
+ alias_method :__original_initialize__, :initialize
56
+ alias_method :initialize, :__custom_initialize__
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def method_added(name)
63
+ return if name != :initialize
64
+
65
+ __overwrite_initialize__
66
+ end
67
+
68
+ def inherited(subclass)
69
+ subclass.instance_variable_set :@attribute_names, @attribute_names.dup
70
+ end
71
+ end
72
+ end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Structure
2
- # ClassMethods
3
4
  module ClassMethods
4
5
  def double
5
6
  klass = Class.new(self)
@@ -9,27 +10,29 @@ module Structure
9
10
  protected_instance_methods(false) -
10
11
  [:initialize]
11
12
  ).each do |name|
12
- klass.send(:undef_method, name)
13
+ klass.send :undef_method, name
13
14
  end
14
15
 
15
16
  klass.module_eval do
16
17
  def initialize(data = {})
17
18
  data.each do |key, value|
18
19
  unless value.is_a?(Structure) ||
19
- (
20
- defined?(RSpec::Mocks::Double) &&
21
- value.is_a?(RSpec::Mocks::Double)
22
- )
20
+ (defined?(::RSpec::Mocks::Double) &&
21
+ value.is_a?(::RSpec::Mocks::Double))
23
22
  value.freeze
24
23
  end
25
24
 
26
- instance_variable_set(:"@#{key}", value)
25
+ instance_variable_set :"@#{key}", value
27
26
  end
28
27
  end
29
28
 
30
29
  attribute_names.each do |name|
31
- module_eval "def _#{name}; @#{name}; end"
32
- private "_#{name}"
30
+ module_eval <<-CODE, __FILE__, __LINE__ + 1
31
+ def __#{name}__
32
+ @#{name}
33
+ end
34
+ CODE
35
+ private "__#{name}__"
33
36
  end
34
37
 
35
38
  module_eval(&Proc.new) if block_given?
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Structure
4
+ module Utils
5
+ def self.serialize(value)
6
+ if value.respond_to?(:attributes)
7
+ value.attributes
8
+ elsif value.is_a?(::Array)
9
+ value.map { |subvalue| serialize(subvalue) }
10
+ else
11
+ value
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Structure
4
+ VERSION = '1.1.0'
5
+ end
metadata CHANGED
@@ -1,65 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-25 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: minitest
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rubocop
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
11
+ date: 2019-05-24 00:00:00.000000000 Z
12
+ dependencies: []
55
13
  description:
56
- email: me@hakanensari.com
14
+ email:
57
15
  executables: []
58
16
  extensions: []
59
17
  extra_rdoc_files: []
60
18
  files:
61
19
  - lib/structure.rb
20
+ - lib/structure/class_methods.rb
62
21
  - lib/structure/double.rb
22
+ - lib/structure/utils.rb
23
+ - lib/structure/version.rb
63
24
  homepage:
64
25
  licenses:
65
26
  - MIT
@@ -79,8 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
40
  - !ruby/object:Gem::Version
80
41
  version: '0'
81
42
  requirements: []
82
- rubyforge_project:
83
- rubygems_version: 2.6.11
43
+ rubygems_version: 3.0.3
84
44
  signing_key:
85
45
  specification_version: 4
86
46
  summary: Parse data into value objects