attribeauty 0.4.7 → 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 +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/lib/attribeauty/base.rb +6 -2
- data/lib/attribeauty/params.rb +3 -0
- data/lib/attribeauty/validator.rb +3 -3
- data/lib/attribeauty/version.rb +1 -1
- metadata +1 -1
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
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
@@ -71,6 +71,9 @@ module Attribeauty
|
|
71
71
|
# in Rails if you have a user model you can call
|
72
72
|
# Attribeauty::Params.with(params.to_unsafe_h).generate_for(User, :username, :name, :age, :email)
|
73
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.
|
74
77
|
def generate_for(model, *columns)
|
75
78
|
raise "Method requires Rails" unless defined?(Rails)
|
76
79
|
|
@@ -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