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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3e7fb42086d75e28fa5c80ff5718c7de734759a0bce85df5b3fd48a2fabbbaf
4
- data.tar.gz: 2a08c818e591cd8b4a780d275f2323c76842c7095a2de878bda7bfaab357ef0c
3
+ metadata.gz: 53aa2dd41b3c190851af0cac11f16ae9ccbebe82d8f83b45f6544fd438a2c612
4
+ data.tar.gz: fa8f0e2d4a8c7b642255938eb828efd98a264c199c3e63c9d9315c8a620352ef
5
5
  SHA512:
6
- metadata.gz: 970ede789c687fd334b84555b610940e3862672166269a2b39055c942f79a7466371f4a05d35bc658b5b455914281e5cde607e9a41da84c49e7554d58df935b7
7
- data.tar.gz: 2e7a482d54b2487169cb63380eb641a42a3bc03a1fecd25028ed917e2184110bdd658173924de55ff7440c7101c3a93b5f42bf182f2e58c90a6e874ef4406a86
6
+ metadata.gz: 7647ebd7f97f78f9d160436a5a2df37ece3dc5c52f9281f31dc8ec0c9d769ec470d78a2f04214723c2785ca38397110023210d01f9d519c5d73262f4b4eefcef
7
+ data.tar.gz: 877730b270379af03c9480f24db221790676f8da285156f718147be21295154a79a58f616e67ef0b337eac422749ec9ba224e3d4bd634e4b826fa4ee31f40b0b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
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
+
3
7
  ## [0.4.7] - 2024-07-01
4
8
 
5
9
  - generate for fix, and added strict option.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.4.7)
4
+ attribeauty (0.4.8)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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)`:
@@ -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); @#{name} = TypeCaster.run(value, #{type.inspect}); end
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
@@ -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, type, original_val, **args)
6
- new(name, type, original_val, **args).run
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, **args)
12
+ def initialize(name, original_val, type = nil, args = {})
13
13
  @name = name
14
14
  @type = type
15
15
  @original_val = original_val
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.4.7"
4
+ VERSION = "0.4.8"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribeauty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby