options_model 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 658ee8fa02e09a0d6965e6331ecc3d281eec4ab7
4
- data.tar.gz: 35a3deec15574e4c47eec496b95fdfc6b92c7854
3
+ metadata.gz: c137c194481be3add8c0ffdaefe2ccfb35e697ce
4
+ data.tar.gz: 3088d2da15e69fb0ab15c37a37669f1b93e59c38
5
5
  SHA512:
6
- metadata.gz: fed38e4816859b310c9ae9a5b47c3f632bcf75a66b5a6414d7323c6694e9f6fbb64fd3cd91148afb5989c9a04f3c74ede3c421faa3c527844d456e2b07507e69
7
- data.tar.gz: aa7bc5057fb324506bb6e31ed58f526317a241b5ecb3136da78bcbd4746ff33e1f5d0bece4a9e8f4ae71772728d39bca6cb9c6f653b66a72ce4c4bb048db866b
6
+ metadata.gz: 2c10190f8502e9b3f1ad1692d1ad510a5f7b952f25136df8658bf18b4ef907795465e2a114ea3df8feb13bee10731dc5354d5c77006957b9cf8d0208c0e3ba73
7
+ data.tar.gz: ccd5d5ad6cad61e317851886cbbb12f6af3c8f1a3b30253b7c932682c93b80ceaaf954f3852aee2c62a097e4a36c4255ae623169d4e4120e31a75954be2a871e
@@ -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 _attributes
60
+ def attributes
61
61
  @attributes ||= ActiveSupport::HashWithIndifferentAccess.new
62
62
  end
63
- private :_attributes
64
- alias_method :attributes, :_attributes
65
63
 
66
- def _nested_attributes
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 _unused_attributes
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
- name
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
- name
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.attribute_names.each do |attribute_name|
10
- attribute = public_send(attribute_name)
11
- if attribute.is_a?(OptionsModel::Base)
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
@@ -1,3 +1,3 @@
1
1
  module OptionsModel
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: options_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - jasl