store_model 0.8.2 → 0.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/store_model/model.rb +44 -1
- data/lib/store_model/version.rb +1 -1
- data/lib/store_model.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: 066a53b97ac50d61d82c10376abe79d3dac452ff2ff33f3a5d650e7e119e356f
|
4
|
+
data.tar.gz: '0892deb0ac4827ad431ea76c0d1a121bacfbb0fe66204164fba1c6baec685cad'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0333e12118a8a445a22ed591e1e887dfc3912f6e02f41e0cc63d87d8dde76a001abd30e5860afba0bde49a4f6163b7092e4fc9c931865586344959c52ddf2cdc
|
7
|
+
data.tar.gz: 9eaffc2c1660ea778b5360410e1498ff3dd4add451cbcdc1cf6ee32a83fb65bd4286b76929c9cddf7d4c693811bee83cc5669391fa0ea84dcdd0254e5fc44a25
|
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
|
|
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
data/lib/store_model.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.1
|
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-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|