structure 0.29.0 → 1.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
  SHA1:
3
- metadata.gz: 6ed08697fcb78fd184a4051eaa84f027cbcd7a8d
4
- data.tar.gz: c3cadb48c7d20fb8fe36688bb4436fd38c753689
3
+ metadata.gz: 98a53ce023b7dca2e8b5e8928ef06a72ed33778a
4
+ data.tar.gz: f58c93016c291624411a1f3c2d9136e7c405ebb5
5
5
  SHA512:
6
- metadata.gz: 8207ae458aa21cb2a51873d29dfe24555adf5fecaf807cfec2cb3ab2604c0f85f83e4f134efdb4f166b8ec291bb93f947eb568c453d93e047ee738ec5725461d
7
- data.tar.gz: 00eb4ba174d39851d1a80f7bf379010e0a682ce6721300f8dff8d05d59bd4e372fa8fdca397971f0d1e2bb8f8c3d706f13a2a66e3db7aa14819bd5d2a5690000
6
+ metadata.gz: b6dd7f521583805cff070284c79c2bcaa878d40579341abb3f1e24f0b6c30da0932336bc1c3b27140e2d3afeefcda5b611273a1d4dfff10b9f6449336e67e6d9
7
+ data.tar.gz: 2d2b7285060950e11664ac47a8ec1b65bd5cd4f1edd89aa5a81245c9a008417801e406a3da50012eb31237dcf54633c68f2a90040666c4c03ab7ca0144d83b86
data/lib/structure.rb ADDED
@@ -0,0 +1,100 @@
1
+ module Structure
2
+ class << self
3
+ private
4
+
5
+ def included(base)
6
+ base.extend(ClassMethods).instance_variable_set(:@attribute_names, [])
7
+ end
8
+ end
9
+
10
+ def attributes
11
+ attribute_names.reduce({}) { |hash, key|
12
+ value = send(key)
13
+ hash.update(key =>
14
+ if value.respond_to?(:attributes)
15
+ value.attributes
16
+ elsif value.is_a?(Array)
17
+ value.map { |element|
18
+ if element.respond_to?(:attributes)
19
+ element.attributes
20
+ else
21
+ element
22
+ end
23
+ }
24
+ else
25
+ value
26
+ end
27
+ )
28
+ }
29
+ end
30
+
31
+ def attribute_names
32
+ self.class.attribute_names
33
+ end
34
+
35
+ def ==(other)
36
+ return false unless other.respond_to?(:attributes)
37
+ attributes == other.attributes
38
+ end
39
+
40
+ def inspect
41
+ name = self.class.name || self.class.to_s.gsub(/[^\w:]/, "")
42
+ values = attribute_names
43
+ .map { |key|
44
+ value = send(key)
45
+ if (value).is_a?(Array)
46
+ description = value.take(3).map(&:inspect).join(", ")
47
+ description += "..." if value.size > 3
48
+ "#{key}=[#{description}]"
49
+ else
50
+ "#{key}=#{value.inspect}"
51
+ end
52
+ }
53
+ .join(", ")
54
+
55
+ "#<#{name} #{values}>"
56
+ end
57
+
58
+ alias_method :to_h, :attributes
59
+ alias_method :eql?, :==
60
+ alias_method :to_s, :inspect
61
+
62
+ module ClassMethods
63
+ attr_reader :attribute_names
64
+
65
+ def attribute(name)
66
+ name = name.to_s
67
+
68
+ if name.chomp!("?")
69
+ module_eval(<<-EOS, __FILE__, __LINE__)
70
+ def #{name}?
71
+ #{name}
72
+ end
73
+ EOS
74
+ end
75
+
76
+ module_eval(<<-EOS, __FILE__, __LINE__)
77
+ def #{name}
78
+ return @#{name} if defined?(@#{name})
79
+ @#{name} = _#{name}
80
+ @#{name}.freeze unless @#{name}.is_a?(Structure)
81
+
82
+ @#{name}
83
+ end
84
+ EOS
85
+
86
+ define_method("_#{name}", Proc.new)
87
+ private "_#{name}"
88
+
89
+ @attribute_names << name
90
+
91
+ name.to_sym
92
+ end
93
+
94
+ private
95
+
96
+ def inherited(subclass)
97
+ subclass.instance_variable_set(:@attribute_names, @attribute_names.dup)
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,32 @@
1
+ module Structure
2
+ module ClassMethods
3
+ def double
4
+ klass = Class.new(self)
5
+
6
+ (
7
+ private_instance_methods(false) +
8
+ protected_instance_methods(false) -
9
+ [:initialize]
10
+ ).each do |name|
11
+ klass.send(:undef_method, name)
12
+ end
13
+
14
+ klass.module_eval do
15
+ def initialize(data = {})
16
+ data.each { |key, value|
17
+ instance_variable_set(:"@#{key}", value)
18
+ }
19
+ end
20
+
21
+ attribute_names.each do |name|
22
+ module_eval "def _#{name}; @#{name}; end"
23
+ private "_#{name}"
24
+ end
25
+
26
+ module_eval(&Proc.new) if block_given?
27
+ end
28
+
29
+ klass
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,22 +1,51 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.29.0
4
+ version: 1.0.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: 2014-08-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2015-03-20 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'
13
41
  description:
14
42
  email: me@hakanensari.com
15
43
  executables: []
16
44
  extensions: []
17
45
  extra_rdoc_files: []
18
46
  files:
19
- - structure.rb
47
+ - lib/structure.rb
48
+ - lib/structure/double.rb
20
49
  homepage:
21
50
  licenses:
22
51
  - MIT
@@ -24,7 +53,7 @@ metadata: {}
24
53
  post_install_message:
25
54
  rdoc_options: []
26
55
  require_paths:
27
- - "."
56
+ - lib
28
57
  required_ruby_version: !ruby/object:Gem::Requirement
29
58
  requirements:
30
59
  - - ">="
@@ -37,9 +66,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
66
  version: '0'
38
67
  requirements: []
39
68
  rubyforge_project:
40
- rubygems_version: 2.3.0
69
+ rubygems_version: 2.4.5
41
70
  signing_key:
42
71
  specification_version: 4
43
- summary: Parses data into value objects
72
+ summary: Parse data into value objects
44
73
  test_files: []
45
74
  has_rdoc:
data/structure.rb DELETED
@@ -1,105 +0,0 @@
1
- module Structure
2
- def self.included(base)
3
- base.extend(ClassMethods).instance_variable_set(:@attribute_names, [])
4
- end
5
-
6
- def attributes
7
- attribute_names.reduce({}) { |ret, key|
8
- val = send(key)
9
-
10
- ret.update(key =>
11
- if val.respond_to?(:attributes)
12
- val.attributes
13
- elsif val.is_a?(Array)
14
- val.map { |el| el.respond_to?(:attributes) ? el.attributes : el }
15
- else
16
- val
17
- end
18
- )
19
- }
20
- end
21
-
22
- def attribute_names
23
- self.class.attribute_names
24
- end
25
-
26
- def ==(other)
27
- return false unless other.respond_to?(:attributes)
28
- attributes == other.attributes
29
- end
30
-
31
- def inspect
32
- class_name = self.class.name || self.class.to_s.gsub(/[^\w:]/, '')
33
-
34
- "#<#{class_name} #{
35
- attribute_names
36
- .map { |key|
37
- val = send(key)
38
- if val.is_a?(Array)
39
- "#{key}=[#{val.take(3).map(&:inspect).join(', ')}" + (val.size > 3 ? '...' : '') + ']'
40
- else
41
- "#{key}=#{val.inspect}"
42
- end
43
- }
44
- .join(', ')
45
- }>"
46
- end
47
-
48
- alias_method :to_h, :attributes
49
- alias_method :eql?, :==
50
- alias_method :to_s, :inspect
51
-
52
- module ClassMethods
53
- attr_reader :attribute_names
54
-
55
- def double(&blk)
56
- klass = Class.new(self)
57
-
58
- (private_instance_methods(false) + protected_instance_methods(false) - [:initialize])
59
- .each do |mth|
60
- klass.send(:undef_method, mth)
61
- end
62
-
63
- klass.module_eval do
64
- def initialize(data = {})
65
- data.each { |key, val| instance_variable_set(:"@#{key}", val) }
66
- end
67
-
68
- attribute_names.each do |name|
69
- module_eval "def _#{name}; @#{name}; end"
70
- private "_#{name}"
71
- end
72
-
73
- module_eval(&blk) if block_given?
74
- end
75
-
76
- klass
77
- end
78
-
79
- def inherited(subclass)
80
- subclass.instance_variable_set(:@attribute_names, @attribute_names.dup)
81
- end
82
-
83
- private
84
-
85
- def attribute(name, &blk)
86
- name = name.to_s
87
- module_eval "def #{name}?; #{name}; end" if name.chomp!('?')
88
- module_eval <<-DEF
89
- def #{name}
90
- return @#{name} if defined?(@#{name})
91
- @#{name} = _#{name}
92
- @#{name}.freeze unless @#{name}.is_a?(Structure)
93
-
94
- @#{name}
95
- end
96
- DEF
97
- define_method("_#{name}", blk)
98
- private "_#{name}"
99
-
100
- @attribute_names << name
101
-
102
- name.to_sym
103
- end
104
- end
105
- end