structure 0.27.5 → 0.27.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/structure.rb +23 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7923734fab4cbf99ced503dd98c06fd8c6216c23
|
4
|
+
data.tar.gz: 8d8b1dda5952d0fb8a9ff5ca9ea790c34d489304
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fbdcbf61a7d398c76d5296832ae31e9c1e9808c3ce4d2c27b8227faf14092ca35cc1a2fed2ffea092060355107bfee4e91b0087ebddf5449f1aab5fe7b11785
|
7
|
+
data.tar.gz: 446a6f29b1944961e814f2b8fcdf5fe567be66c83f5dc5f604d822686fb667b6f9c6397fbe5550f885854be7d870296ead5da8fa662b76adb9045dde81d5bf0a
|
data/structure.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
module Structure
|
2
2
|
def self.included(base)
|
3
|
-
base
|
4
|
-
.extend(ClassMethods)
|
5
|
-
.instance_variable_set(:@attribute_names, [])
|
3
|
+
base.extend(ClassMethods).instance_variable_set(:@attribute_names, [])
|
6
4
|
end
|
7
5
|
|
8
6
|
def attributes
|
9
|
-
|
7
|
+
attribute_names.reduce({}) { |ret, name|
|
10
8
|
ret.update(name => self.send(name))
|
11
9
|
}
|
12
10
|
end
|
13
11
|
|
12
|
+
def attribute_names
|
13
|
+
self.class.attribute_names
|
14
|
+
end
|
15
|
+
|
14
16
|
def ==(other)
|
15
17
|
attributes == other.attributes
|
16
18
|
end
|
@@ -37,13 +39,25 @@ module Structure
|
|
37
39
|
attr_reader :attribute_names
|
38
40
|
|
39
41
|
def to_struct
|
40
|
-
|
42
|
+
class_name = name || to_s.gsub(/\W/, '')
|
41
43
|
|
42
|
-
Struct.
|
44
|
+
if Struct.const_defined?(class_name, false)
|
45
|
+
return Struct.const_get(class_name, false)
|
46
|
+
end
|
47
|
+
|
48
|
+
klass = Struct.new(class_name, *attribute_names) do
|
43
49
|
def initialize(data = {})
|
44
50
|
data.each { |key, val| self.send("#{key}=", val) }
|
45
51
|
end
|
46
52
|
end
|
53
|
+
|
54
|
+
attribute_names.each do |name|
|
55
|
+
if instance_methods(false).include?(:"#{name}?")
|
56
|
+
klass.module_eval "def #{name}?; #{name} end"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
klass
|
47
61
|
end
|
48
62
|
|
49
63
|
def inherited(subclass)
|
@@ -51,15 +65,12 @@ module Structure
|
|
51
65
|
end
|
52
66
|
|
53
67
|
def attribute(name, &blk)
|
68
|
+
name = name.to_s
|
69
|
+
module_eval "def #{name}?; #{name}; end" if name.chomp!('?')
|
70
|
+
module_eval "def #{name}; @#{name} ||= _#{name}; end"
|
54
71
|
define_method("_#{name}", blk)
|
55
72
|
private "_#{name}"
|
56
73
|
|
57
|
-
module_eval <<-END
|
58
|
-
def #{name}
|
59
|
-
@#{name} ||= _#{name}
|
60
|
-
end
|
61
|
-
END
|
62
|
-
|
63
74
|
@attribute_names << name
|
64
75
|
end
|
65
76
|
end
|