options_model 0.0.3 → 0.0.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c137c194481be3add8c0ffdaefe2ccfb35e697ce
|
4
|
+
data.tar.gz: 3088d2da15e69fb0ab15c37a37669f1b93e59c38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c10190f8502e9b3f1ad1692d1ad510a5f7b952f25136df8658bf18b4ef907795465e2a114ea3df8feb13bee10731dc5354d5c77006957b9cf8d0208c0e3ba73
|
7
|
+
data.tar.gz: ccd5d5ad6cad61e317851886cbbb12f6af3c8f1a3b30253b7c932682c93b80ceaaf954f3852aee2c62a097e4a36c4255ae623169d4e4120e31a75954be2a871e
|
data/lib/options_model/base.rb
CHANGED
@@ -14,6 +14,18 @@ module OptionsModel
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
def ==(other)
|
18
|
+
other.instance_of?(self.class) &&
|
19
|
+
attributes == other.attributes &&
|
20
|
+
nested_attributes == other.nested_attributes &&
|
21
|
+
unused_attributes == other.unused_attributes
|
22
|
+
end
|
23
|
+
alias :eql? :==
|
24
|
+
|
25
|
+
def hash
|
26
|
+
[attributes, nested_attributes, unused_attributes].hash
|
27
|
+
end
|
28
|
+
|
17
29
|
def inspect
|
18
30
|
"#<#{self.class.name}:OptionsModel #{self.to_h}>"
|
19
31
|
end
|
@@ -57,23 +57,17 @@ module OptionsModel
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
def
|
60
|
+
def attributes
|
61
61
|
@attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
62
62
|
end
|
63
|
-
private :_attributes
|
64
|
-
alias_method :attributes, :_attributes
|
65
63
|
|
66
|
-
def
|
64
|
+
def nested_attributes
|
67
65
|
@nested_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
68
66
|
end
|
69
|
-
private :_nested_attributes
|
70
|
-
alias_method :nested_attributes, :_nested_attributes
|
71
67
|
|
72
|
-
def
|
68
|
+
def unused_attributes
|
73
69
|
@unused_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
74
70
|
end
|
75
|
-
private :_unused_attributes
|
76
|
-
alias_method :unused_attributes, :_unused_attributes
|
77
71
|
end
|
78
72
|
end
|
79
73
|
end
|
@@ -51,7 +51,8 @@ module OptionsModel
|
|
51
51
|
end
|
52
52
|
|
53
53
|
self.attribute_names_for_inlining << name
|
54
|
-
|
54
|
+
|
55
|
+
self
|
55
56
|
end
|
56
57
|
|
57
58
|
def enum_attribute(name, enum, default: nil, allow_nil: false)
|
@@ -74,6 +75,8 @@ module OptionsModel
|
|
74
75
|
|
75
76
|
validates name, inclusion: {in: enum}, allow_nil: allow_nil
|
76
77
|
end
|
78
|
+
|
79
|
+
self
|
77
80
|
end
|
78
81
|
|
79
82
|
def embeds_one(name, class_name: nil, anonymous_class: nil)
|
@@ -117,7 +120,8 @@ module OptionsModel
|
|
117
120
|
end
|
118
121
|
|
119
122
|
self.attribute_names_for_nesting << name
|
120
|
-
|
123
|
+
|
124
|
+
self
|
121
125
|
end
|
122
126
|
|
123
127
|
def attribute_defaults
|
@@ -6,22 +6,13 @@ module OptionsModel
|
|
6
6
|
def to_h
|
7
7
|
hash = {}
|
8
8
|
|
9
|
-
self.class.
|
10
|
-
|
11
|
-
|
12
|
-
hash[attribute_name] = attribute.to_h
|
13
|
-
else
|
14
|
-
hash[attribute_name] = attribute
|
15
|
-
end
|
16
|
-
end
|
9
|
+
hash.merge! unused_attributes if self.class.with_unused_attributes?
|
10
|
+
hash.merge! attributes
|
11
|
+
hash.merge! nested_attributes.reduce({}) { |h, (k, v)| h[k] = v.to_h; h }
|
17
12
|
|
18
13
|
hash
|
19
14
|
end
|
20
15
|
|
21
|
-
def to_h_with_unused
|
22
|
-
to_h.merge unused_attributes
|
23
|
-
end
|
24
|
-
|
25
16
|
module ClassMethods
|
26
17
|
def dump(obj)
|
27
18
|
return YAML.dump({}) unless obj
|
@@ -47,6 +38,14 @@ module OptionsModel
|
|
47
38
|
|
48
39
|
new hash
|
49
40
|
end
|
41
|
+
|
42
|
+
def with_unused_attributes!
|
43
|
+
@with_unused_attributes = true
|
44
|
+
end
|
45
|
+
|
46
|
+
def with_unused_attributes?
|
47
|
+
@with_unused_attributes
|
48
|
+
end
|
50
49
|
end
|
51
50
|
end
|
52
51
|
end
|