store_model 0.8.1 → 0.11.0
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/README.md +1 -1
- data/lib/store_model/combine_errors_strategies/merge_error_strategy.rb +2 -1
- data/lib/store_model/model.rb +44 -1
- data/lib/store_model/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f94bfdbb51a596617e73ce1df8cffc444906bbbb2f2815f603b487673ac4038
|
4
|
+
data.tar.gz: 85545a53223eee0ab626d6f2c21974270cb16505c239308c656bf84d24bda54c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b8dd9df6fd3f0c3d4307e647b95e7e568cf84a177b91d5e2c6fcb6a5ab8dfc8be3dfaa6f71b4bc352f0889b4f64c7f8bba46785d2882cbe83f6b299fd333385
|
7
|
+
data.tar.gz: b05e55818779210c5d1fe6458316f51b940adc09e51521c431eef470d09ba7cecfb6233dcf42b22238e3889fa43c2e9298f1e5895d5b3f1ba538d4b767a80ea3
|
data/README.md
CHANGED
@@ -44,7 +44,7 @@ This approach works fine when you don't have a lot of keys with logic around the
|
|
44
44
|
|
45
45
|
For instance, try to find a way to validate `:model` value to be required. Despite of the fact, that you'll have to write this validation by hand, it violates single-repsponsibility principle: why parent model (`Product`) should know about the logic related to a child (`Configuration`)?
|
46
46
|
|
47
|
-
> 📖 Read more about the motivation in the [Wrapping JSON-based ActiveRecord attributes with classes](https://
|
47
|
+
> 📖 Read more about the motivation in the [Wrapping JSON-based ActiveRecord attributes with classes](https://evilmartians.com/chronicles/wrapping-json-based-active-record-attributes-with-classes) post
|
48
48
|
|
49
49
|
## Getting started
|
50
50
|
|
@@ -13,7 +13,8 @@ module StoreModel
|
|
13
13
|
# attribute
|
14
14
|
def call(attribute, base_errors, store_model_errors)
|
15
15
|
if Rails::VERSION::MAJOR < 6 || Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR.zero?
|
16
|
-
base_errors.
|
16
|
+
base_errors.delete(attribute)
|
17
|
+
store_model_errors.each { |field, error| base_errors.add(field, error) }
|
17
18
|
else
|
18
19
|
store_model_errors.errors.each do |error|
|
19
20
|
base_errors.add(attribute, :invalid, message: error.full_message)
|
data/lib/store_model/model.rb
CHANGED
@@ -11,10 +11,13 @@ module StoreModel
|
|
11
11
|
def self.included(base) # :nodoc:
|
12
12
|
base.include ActiveModel::Model
|
13
13
|
base.include ActiveModel::Attributes
|
14
|
+
base.include ActiveModel::AttributeMethods
|
14
15
|
base.include StoreModel::NestedAttributes
|
15
16
|
|
16
17
|
base.extend StoreModel::Enum
|
17
18
|
base.extend StoreModel::TypeBuilders
|
19
|
+
|
20
|
+
base.attribute_method_suffix "?"
|
18
21
|
end
|
19
22
|
|
20
23
|
attr_accessor :parent
|
@@ -40,6 +43,13 @@ module StoreModel
|
|
40
43
|
attributes.all? { |name, value| value == other.attributes[name] }
|
41
44
|
end
|
42
45
|
|
46
|
+
# Returns hash for a StoreModel::Model instance based on attributes hash
|
47
|
+
#
|
48
|
+
# @return [Integer]
|
49
|
+
def hash
|
50
|
+
attributes.hash
|
51
|
+
end
|
52
|
+
|
43
53
|
# Allows to call :presence validation on the association itself.
|
44
54
|
#
|
45
55
|
# @return [Boolean]
|
@@ -70,13 +80,37 @@ module StoreModel
|
|
70
80
|
|
71
81
|
# Checks if the attribute with a given name is defined
|
72
82
|
#
|
83
|
+
# @example
|
84
|
+
# class Person
|
85
|
+
# include StoreModel::Model
|
86
|
+
# attribute :name, :string
|
87
|
+
# alias_attribute :new_name, :name
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# Person.has_attribute?('name') # => true
|
91
|
+
# Person.has_attribute?('new_name') # => true
|
92
|
+
# Person.has_attribute?(:age) # => true
|
93
|
+
# Person.has_attribute?(:nothing) # => false
|
94
|
+
#
|
73
95
|
# @param attr_name [String] name of the attribute
|
74
96
|
#
|
75
97
|
# @return [Boolean]
|
76
98
|
# rubocop:disable Naming/PredicateName
|
77
99
|
def has_attribute?(attr_name)
|
78
|
-
|
100
|
+
attr_name = attr_name.to_s
|
101
|
+
attr_name = self.class.attribute_aliases[attr_name] || attr_name
|
102
|
+
attribute_types.key?(attr_name)
|
79
103
|
end
|
104
|
+
|
105
|
+
# Legacy implementation of #has_attribute?
|
106
|
+
#
|
107
|
+
# @param attr_name [String] name of the attribute
|
108
|
+
#
|
109
|
+
# @return [Boolean]
|
110
|
+
def _has_attribute?(attr_name)
|
111
|
+
attribute_types.key?(attr_name)
|
112
|
+
end
|
113
|
+
|
80
114
|
# rubocop:enable Naming/PredicateName
|
81
115
|
|
82
116
|
# Contains a hash of attributes which are not defined but exist in the
|
@@ -86,5 +120,14 @@ module StoreModel
|
|
86
120
|
def unknown_attributes
|
87
121
|
@unknown_attributes ||= {}
|
88
122
|
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def attribute?(attribute)
|
127
|
+
case value = attributes[attribute]
|
128
|
+
when 0 then false
|
129
|
+
else value.present?
|
130
|
+
end
|
131
|
+
end
|
89
132
|
end
|
90
133
|
end
|
data/lib/store_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|