ph_model 0.0.1 → 1.0.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/.codeclimate.yml +5 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +8 -0
- data/README.md +2 -1
- data/lib/ph_model/concerns/attribute_nested_validation.rb +33 -0
- data/lib/ph_model/concerns/attribute_of_array_type_initialization.rb +19 -0
- data/lib/ph_model/concerns/attribute_required_validation.rb +19 -0
- data/lib/ph_model/concerns/attribute_type_validation.rb +56 -0
- data/lib/ph_model/concerns/initialize_callback.rb +19 -0
- data/lib/ph_model/concerns.rb +5 -0
- data/lib/ph_model/version.rb +1 -1
- data/lib/ph_model.rb +22 -3
- metadata +8 -4
- data/.hound.yml +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04cf28ca1fecccaf0c306c6259efe62e31cebe87
|
4
|
+
data.tar.gz: 8b15fce44d2f140b262a851515fa37d122024b7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc1b55bcc96ba908e6c2b7f04fe523e1c027200b58431196843efb19d03ef83d7fba4952a484918b6a04247d8f1dfba2656ead3698f99d6d57b6385a356180fc
|
7
|
+
data.tar.gz: 4f4050c4dc0a13105a3684d3d48c825e21729c9a4fe1827405529881184a9e12ea1a383afcc58c41f768216f09635d51c1c3dc0f8df67b4f005c27fd8c1e1297
|
data/.codeclimate.yml
CHANGED
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## [v0.0.1](https://github.com/payrollhero/ph_model/tree/v0.0.1) (2016-02-03)
|
4
|
+
- Initial extract from ph\_utility [\#1](https://github.com/payrollhero/ph_model/pull/1) ([piotrb](https://github.com/piotrb))
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
|
data/README.md
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
module PhModel
|
2
|
+
module Concerns
|
3
|
+
module AttributeNestedValidation
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do |other|
|
7
|
+
other.validate :validate_nested_attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate_nested_attributes
|
11
|
+
self.class.attributes.each do |attribute_name, info|
|
12
|
+
value = send(attribute_name)
|
13
|
+
if info[:type].is_a? Array
|
14
|
+
if value.respond_to? :each_with_index
|
15
|
+
value.each_with_index do |item_value, index|
|
16
|
+
check_one(item_value, "#{attribute_name}[#{index}]")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
check_one(value, attribute_name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def check_one(value, attribute_name)
|
26
|
+
return if !value.respond_to?(:valid?) || value.valid?
|
27
|
+
value.errors.full_messages.each do |message|
|
28
|
+
errors.add(:base, "#{attribute_name}.#{message}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module PhModel
|
2
|
+
module Concerns
|
3
|
+
module AttributeOfArrayTypeInitialization
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do |other|
|
7
|
+
other.after_initialize :initialize_array_type_attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize_array_type_attributes
|
11
|
+
self.class.attributes.each do |attribute_name, info|
|
12
|
+
if info[:type].kind_of?(Array)
|
13
|
+
send("#{attribute_name}=", []) unless send(attribute_name).present?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module PhModel
|
2
|
+
module Concerns
|
3
|
+
module AttributeRequiredValidation
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do |other|
|
7
|
+
other.validate :validate_required_attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate_required_attributes
|
11
|
+
self.class.attributes.each do |attribute_name, info|
|
12
|
+
if info[:required] && send(attribute_name).blank?
|
13
|
+
errors.add(attribute_name, :empty)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module PhModel
|
2
|
+
module Concerns
|
3
|
+
module AttributeTypeValidation
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do |other|
|
7
|
+
other.validate :ensure_typed_attributes_class
|
8
|
+
end
|
9
|
+
|
10
|
+
def ensure_typed_attributes_class
|
11
|
+
self.class.attributes.each do |attribute_name, info|
|
12
|
+
if info[:type] && !type_match?(attribute_name)
|
13
|
+
errors.add(attribute_name, "must be #{info[:type].inspect}, was #{type_summary(attribute_name)}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def type_summary(attribute_name)
|
19
|
+
value = send(attribute_name)
|
20
|
+
case value
|
21
|
+
when Array
|
22
|
+
value.map { |val| val.respond_to?(:model_name) ? val.model_name.name : val.class.name }.uniq
|
23
|
+
else
|
24
|
+
value.class
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def type_match?(attribute_name)
|
29
|
+
value = send(attribute_name)
|
30
|
+
info = self.class.attributes[attribute_name]
|
31
|
+
type = info[:type]
|
32
|
+
|
33
|
+
case type
|
34
|
+
when nil
|
35
|
+
true
|
36
|
+
when Array
|
37
|
+
if type.count != 1
|
38
|
+
raise ArgumentError, "don't know how to handle type: #{type.inspect}"
|
39
|
+
else
|
40
|
+
if value.kind_of? Array
|
41
|
+
value.all? { |item| item.is_a?(type.first) }
|
42
|
+
else
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
else
|
47
|
+
if !info[:required] && value.nil?
|
48
|
+
true
|
49
|
+
else
|
50
|
+
value.is_a?(type)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module PhModel
|
2
|
+
module Concerns
|
3
|
+
|
4
|
+
# Adds support for defining callbacks around initialize
|
5
|
+
module InitializeCallback
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do |other|
|
9
|
+
other.define_model_callbacks :initialize
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(attributes = nil, options = {})
|
13
|
+
run_callbacks :initialize do
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/ph_model/concerns.rb
CHANGED
@@ -2,6 +2,11 @@ module PhModel
|
|
2
2
|
module Concerns
|
3
3
|
extend ActiveSupport::Autoload
|
4
4
|
|
5
|
+
autoload :AttributeNestedValidation
|
6
|
+
autoload :AttributeOfArrayTypeInitialization
|
7
|
+
autoload :AttributeRequiredValidation
|
8
|
+
autoload :AttributeTypeValidation
|
9
|
+
autoload :InitializeCallback
|
5
10
|
autoload :ValidatedFactory
|
6
11
|
end
|
7
12
|
end
|
data/lib/ph_model/version.rb
CHANGED
data/lib/ph_model.rb
CHANGED
@@ -17,11 +17,33 @@ module PhModel
|
|
17
17
|
extend ActiveSupport::Concern
|
18
18
|
extend ActiveSupport::Autoload
|
19
19
|
|
20
|
+
autoload :Concerns
|
21
|
+
autoload :ValidationFailed
|
22
|
+
|
20
23
|
include ActiveAttr::BasicModel
|
21
24
|
include ActiveAttr::AttributeDefaults
|
22
25
|
include ActiveAttr::QueryAttributes
|
23
26
|
include ActiveAttr::MassAssignment
|
24
27
|
|
28
|
+
include Concerns::InitializeCallback
|
29
|
+
include Concerns::AttributeRequiredValidation
|
30
|
+
include Concerns::AttributeTypeValidation
|
31
|
+
include Concerns::AttributeNestedValidation
|
32
|
+
include Concerns::AttributeOfArrayTypeInitialization
|
33
|
+
|
34
|
+
def as_json
|
35
|
+
{}.tap do |hash|
|
36
|
+
self.class.attributes.each do |attribute_name, _info|
|
37
|
+
hash[attribute_name] = send(attribute_name).as_json
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def inspect
|
43
|
+
attr_info = self.class.attributes.map { |attr_name, info| "#{attr_name}: #{self.send(attr_name).inspect}" }.join(", ")
|
44
|
+
"#<#{self.model_name} #{attr_info}>"
|
45
|
+
end
|
46
|
+
|
25
47
|
# Monkey patch #assign_attributes inside ActiveAttr::MassAssignment
|
26
48
|
# so that it doesn't blindly ignore attempting to assign attributes which do not
|
27
49
|
# exist
|
@@ -38,8 +60,5 @@ module PhModel
|
|
38
60
|
end if sanitized_new_attributes
|
39
61
|
end
|
40
62
|
|
41
|
-
autoload :Concerns
|
42
|
-
autoload :ValidationFailed
|
43
|
-
|
44
63
|
include Concerns::ValidatedFactory
|
45
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ph_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Banasik
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -186,7 +186,6 @@ extra_rdoc_files: []
|
|
186
186
|
files:
|
187
187
|
- ".codeclimate.yml"
|
188
188
|
- ".gitignore"
|
189
|
-
- ".hound.yml"
|
190
189
|
- ".reek"
|
191
190
|
- ".rubocop.hound.yml"
|
192
191
|
- ".rubocop.yml"
|
@@ -204,6 +203,11 @@ files:
|
|
204
203
|
- lib/active_model/validations/type_validator.rb
|
205
204
|
- lib/ph_model.rb
|
206
205
|
- lib/ph_model/concerns.rb
|
206
|
+
- lib/ph_model/concerns/attribute_nested_validation.rb
|
207
|
+
- lib/ph_model/concerns/attribute_of_array_type_initialization.rb
|
208
|
+
- lib/ph_model/concerns/attribute_required_validation.rb
|
209
|
+
- lib/ph_model/concerns/attribute_type_validation.rb
|
210
|
+
- lib/ph_model/concerns/initialize_callback.rb
|
207
211
|
- lib/ph_model/concerns/validated_factory.rb
|
208
212
|
- lib/ph_model/validation_failed.rb
|
209
213
|
- lib/ph_model/version.rb
|
@@ -228,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
228
232
|
version: '0'
|
229
233
|
requirements: []
|
230
234
|
rubyforge_project:
|
231
|
-
rubygems_version: 2.
|
235
|
+
rubygems_version: 2.5.2
|
232
236
|
signing_key:
|
233
237
|
specification_version: 4
|
234
238
|
summary: ph-model -- active_model, active_attr brought together at last
|
data/.hound.yml
DELETED