attribeauty 0.4.6 → 0.4.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/lib/attribeauty/base.rb +6 -2
- data/lib/attribeauty/params.rb +15 -1
- data/lib/attribeauty/validator.rb +3 -3
- data/lib/attribeauty/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: 53aa2dd41b3c190851af0cac11f16ae9ccbebe82d8f83b45f6544fd438a2c612
|
4
|
+
data.tar.gz: fa8f0e2d4a8c7b642255938eb828efd98a264c199c3e63c9d9315c8a620352ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7647ebd7f97f78f9d160436a5a2df37ece3dc5c52f9281f31dc8ec0c9d769ec470d78a2f04214723c2785ca38397110023210d01f9d519c5d73262f4b4eefcef
|
7
|
+
data.tar.gz: 877730b270379af03c9480f24db221790676f8da285156f718147be21295154a79a58f616e67ef0b337eac422749ec9ba224e3d4bd634e4b826fa4ee31f40b0b
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.4.8] - 2024-07-01
|
4
|
+
|
5
|
+
- allow args with base, so `required` and `default` can be used
|
6
|
+
|
7
|
+
## [0.4.7] - 2024-07-01
|
8
|
+
|
9
|
+
- generate for fix, and added strict option.
|
10
|
+
- handle stringifying keys with root
|
11
|
+
|
3
12
|
## [0.4.6] - 2024-06-28
|
4
13
|
|
5
14
|
- unofficial support for auto generating rails models params filtering
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -28,6 +28,7 @@ class MyClass < Attribeauty::Base
|
|
28
28
|
attribute :forth, :boolean
|
29
29
|
attribute :fifth, :time
|
30
30
|
attribute :sixth, :koala
|
31
|
+
attribute :seventh, :string, default: "Kangaroo"
|
31
32
|
end
|
32
33
|
|
33
34
|
instance = MyClass.new(first: 456)
|
@@ -36,6 +37,7 @@ instance.assign_attributes(second: "456")
|
|
36
37
|
instance.second # => 456
|
37
38
|
instance.first = 9000
|
38
39
|
instance.first # => "9000"
|
40
|
+
instance.seventh # => "Kangaroo"
|
39
41
|
```
|
40
42
|
|
41
43
|
To add your own types, simply have a class that handles `MyClassName.new.cast(value)`:
|
data/lib/attribeauty/base.rb
CHANGED
@@ -4,14 +4,18 @@ module Attribeauty
|
|
4
4
|
# base class to inherit from
|
5
5
|
# class MyClass < Attribeauty::Base
|
6
6
|
class Base
|
7
|
-
def self.attribute(name, type)
|
7
|
+
def self.attribute(name, type, **args)
|
8
8
|
@attributes ||= {}
|
9
9
|
return if @attributes[name]
|
10
10
|
|
11
11
|
@attributes[name] = type
|
12
12
|
|
13
13
|
class_eval(<<-CODE, __FILE__, __LINE__ + 1)
|
14
|
-
def #{name}=(value)
|
14
|
+
def #{name}=(value)
|
15
|
+
validator = Validator.run(#{name.inspect}, value, #{type.inspect}, #{args})
|
16
|
+
raise MissingAttributeError, validator.errors.join(', ') if validator.errors.any?
|
17
|
+
@#{name} = validator.value
|
18
|
+
end
|
15
19
|
|
16
20
|
def #{name};@#{name};end
|
17
21
|
CODE
|
data/lib/attribeauty/params.rb
CHANGED
@@ -44,7 +44,7 @@ module Attribeauty
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def root(name)
|
47
|
-
@request_params = request_params[name]
|
47
|
+
@request_params = request_params[name].transform_keys(&:to_sym)
|
48
48
|
|
49
49
|
yield
|
50
50
|
end
|
@@ -68,6 +68,12 @@ module Attribeauty
|
|
68
68
|
strict
|
69
69
|
end
|
70
70
|
|
71
|
+
# in Rails if you have a user model you can call
|
72
|
+
# Attribeauty::Params.with(params.to_unsafe_h).generate_for(User, :username, :name, :age, :email)
|
73
|
+
# It will grab the type, and add an exclude_if: for all with Null false
|
74
|
+
# Note, there are very few circumstances where you wouldn't want to just assign_attributes
|
75
|
+
# to the model, and use the types from there, but here you go.
|
76
|
+
# This api will never be tested or documented.
|
71
77
|
def generate_for(model, *columns)
|
72
78
|
raise "Method requires Rails" unless defined?(Rails)
|
73
79
|
|
@@ -80,6 +86,14 @@ module Attribeauty
|
|
80
86
|
attribute(*attrs)
|
81
87
|
end
|
82
88
|
end
|
89
|
+
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
def generate_for!(model, *columns)
|
94
|
+
@strict = true
|
95
|
+
|
96
|
+
generate_for(model, *columns)
|
83
97
|
end
|
84
98
|
|
85
99
|
private
|
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
module Attribeauty
|
4
4
|
class Validator
|
5
|
-
def self.run(name,
|
6
|
-
new(name,
|
5
|
+
def self.run(name, original_val, type = nil, args = {})
|
6
|
+
new(name, original_val, type, args).run
|
7
7
|
end
|
8
8
|
|
9
9
|
attr_reader :original_val, :errors, :name, :type, :required,
|
10
10
|
:default, :excludes, :value, :valid, :allows
|
11
11
|
|
12
|
-
def initialize(name, original_val, type = nil,
|
12
|
+
def initialize(name, original_val, type = nil, args = {})
|
13
13
|
@name = name
|
14
14
|
@type = type
|
15
15
|
@original_val = original_val
|
data/lib/attribeauty/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribeauty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toby
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: There are so many of these, I just needed this one.
|
14
14
|
email:
|