attributary 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec_status +12 -12
- data/README.md +32 -138
- data/lib/attributary.rb +0 -8
- data/lib/attributary/dsl/accessors.rb +5 -1
- data/lib/attributary/dsl/validations.rb +3 -1
- data/lib/attributary/type.rb +9 -0
- data/lib/attributary/types/fixnum_type.rb +9 -0
- data/lib/attributary/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1190641b6ffb55723ee8b6dd0f609d118401ce65
|
4
|
+
data.tar.gz: 457ed2b996317eadcc8fb6f69a0b3afa6f39621b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43e610ac006d26a6f4925fd73d7fea45bcfaf3a4ed07f268e6656f3be91206515bc27d0cf750e2bbdd229674fa71b602dab30909a64c152d14be1fada0829929
|
7
|
+
data.tar.gz: 5e7a9183f0184160c99ff4762ae2a95161339308a866735f8f7581e17a8141c65d3eb64618d1ad3f0800984ea9b9cd3224f445427a5e3e49464aa6edf2ef1a5b
|
data/.gitignore
CHANGED
data/.rspec_status
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
example_id | status | run_time |
|
2
2
|
------------------------------------------------ | ------ | --------------- |
|
3
|
-
./spec/unit/attributary/attribute_spec.rb[1:1:1] | passed | 0.
|
4
|
-
./spec/unit/attributary/attribute_spec.rb[1:1:2] | passed | 0.
|
5
|
-
./spec/unit/attributary/attribute_spec.rb[1:1:3] | passed | 0.
|
6
|
-
./spec/unit/attributary/attribute_spec.rb[1:1:4] | passed | 0.
|
7
|
-
./spec/unit/attributary/attribute_spec.rb[1:2:1] | passed | 0.
|
8
|
-
./spec/unit/attributary/attribute_spec.rb[1:2:2] | passed | 0.
|
9
|
-
./spec/unit/attributary/attribute_spec.rb[1:3:1] | passed | 0.
|
3
|
+
./spec/unit/attributary/attribute_spec.rb[1:1:1] | passed | 0.00045 seconds |
|
4
|
+
./spec/unit/attributary/attribute_spec.rb[1:1:2] | passed | 0.00012 seconds |
|
5
|
+
./spec/unit/attributary/attribute_spec.rb[1:1:3] | passed | 0.00016 seconds |
|
6
|
+
./spec/unit/attributary/attribute_spec.rb[1:1:4] | passed | 0.0001 seconds |
|
7
|
+
./spec/unit/attributary/attribute_spec.rb[1:2:1] | passed | 0.00012 seconds |
|
8
|
+
./spec/unit/attributary/attribute_spec.rb[1:2:2] | passed | 0.0001 seconds |
|
9
|
+
./spec/unit/attributary/attribute_spec.rb[1:3:1] | passed | 0.00103 seconds |
|
10
10
|
./spec/unit/attributary/attribute_spec.rb[1:3:2] | passed | 0.00012 seconds |
|
11
|
-
./spec/unit/attributary/attribute_spec.rb[1:4:1] | passed | 0.
|
12
|
-
./spec/unit/attributary/attribute_spec.rb[1:4:2] | passed | 0.
|
13
|
-
./spec/unit/attributary/attribute_spec.rb[1:5:1] | passed | 0.
|
14
|
-
./spec/unit/attributary/attribute_spec.rb[1:6:1] | passed | 0.
|
15
|
-
./spec/unit/attributary/attribute_spec.rb[1:7:1] | passed |
|
11
|
+
./spec/unit/attributary/attribute_spec.rb[1:4:1] | passed | 0.00014 seconds |
|
12
|
+
./spec/unit/attributary/attribute_spec.rb[1:4:2] | passed | 0.00013 seconds |
|
13
|
+
./spec/unit/attributary/attribute_spec.rb[1:5:1] | passed | 0.00015 seconds |
|
14
|
+
./spec/unit/attributary/attribute_spec.rb[1:6:1] | passed | 0.00013 seconds |
|
15
|
+
./spec/unit/attributary/attribute_spec.rb[1:7:1] | passed | 1.03 seconds |
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Attributary
|
2
2
|
|
3
|
-
Like `ActiveModel::Attributes` but not. No dependencies.
|
3
|
+
Like `ActiveModel::Attributes` or `Virtus` but not. No dependencies.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,144 +20,38 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
### Initializing
|
54
|
-
|
55
|
-
Want to initialize? Cool.
|
56
|
-
|
57
|
-
class Character
|
58
|
-
include Attributary::Initializer
|
59
|
-
include Attributary::DSL
|
60
|
-
|
61
|
-
donkey :gender, :symbol, default: :male
|
62
|
-
end
|
63
|
-
|
64
|
-
character = Character.new(gender: :male)
|
65
|
-
puts character.gender # :male
|
66
|
-
|
67
|
-
Have to initialize other stuff? Send a hash to attributary_initialize
|
68
|
-
|
69
|
-
class Character
|
70
|
-
include Attributary::Initializer
|
71
|
-
include Attributary::DSL
|
72
|
-
|
73
|
-
attributary do |c|
|
74
|
-
c.raise_errors = true
|
75
|
-
end
|
76
|
-
|
77
|
-
donkey :gender, :symbol, default: :male
|
78
|
-
|
79
|
-
def initialize(name, attributary_attributes = {})
|
80
|
-
@name = name
|
81
|
-
attributary_initialize(attributary_attributes)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
character = Character.new("Charlie", gender: :male)
|
86
|
-
puts character.gender # :male
|
87
|
-
|
88
|
-
### Validations
|
89
|
-
|
90
|
-
There are two types of validations. You can pass a `:collection` option, and/or you can pass a `:validates` option.
|
91
|
-
|
92
|
-
#### Using `:collection`
|
93
|
-
|
94
|
-
Simply pass an array of the possible options for the attribute.
|
95
|
-
|
96
|
-
class Character
|
97
|
-
include Attributary::DSL
|
98
|
-
|
99
|
-
attributary do |c|
|
100
|
-
c.raise_errors = true
|
101
|
-
end
|
102
|
-
|
103
|
-
donkey :gender, :symbol, default: :male, collection: [:male, :female]
|
104
|
-
end
|
105
|
-
|
106
|
-
character = Character.new("Charlie", gender: :male)
|
107
|
-
puts character.gender # :male
|
108
|
-
character.gender = :left # raises CollectionValidationError
|
109
|
-
character.gender = :female
|
110
|
-
puts character.gender # :female
|
111
|
-
character.gender = 'male' # gets casted correctly, and doesn't raise an error. Turn this off with strict_mode
|
112
|
-
|
113
|
-
#### Using `:validates`
|
114
|
-
|
115
|
-
Pass a proc or a method name.
|
116
|
-
|
117
|
-
class Character
|
118
|
-
include Attributary::DSL
|
119
|
-
|
120
|
-
attributary do |c|
|
121
|
-
c.raise_errors = true
|
122
|
-
end
|
123
|
-
|
124
|
-
donkey :gender, :symbol, default: :male, validates: proc { |value| [:male, :female].include?(value) }
|
125
|
-
end
|
126
|
-
|
127
|
-
character = Character.new("Charlie", gender: :male)
|
128
|
-
puts character.gender # :male
|
129
|
-
character.gender = :left # raises CollectionValidationError
|
130
|
-
character.gender = 'female' # gets casted correctly, and doesn't raise an error
|
131
|
-
|
132
|
-
### Types
|
133
|
-
|
134
|
-
Supports some really naive typecasting. [See the supported types](https://github.com/joshmn/attributary/tree/master/lib/attributary/types). Create your own by inheriting from `Attributary::Types` and naming it like `ClassNameType`
|
135
|
-
|
136
|
-
module Attributary
|
137
|
-
module Types
|
138
|
-
class FriendType < Type
|
139
|
-
def self.cast_to(value)
|
140
|
-
Foe.new(value)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
## Error handling
|
147
|
-
|
148
|
-
If the configuration `raise_errors` is false, the object will have their accessible errors on the `attributary_errors` instance method, which is a hash of `{attribute_name: error object}`
|
23
|
+
```Ruby
|
24
|
+
class Character
|
25
|
+
include Attributary::DSL
|
26
|
+
include Attributary::Initializer
|
27
|
+
|
28
|
+
attribute :age, :integer
|
29
|
+
attribute :description, :string, :validate => :check_description
|
30
|
+
attribute :gender, :string, :collection => ['male', 'female', 'other']
|
31
|
+
|
32
|
+
# if you don't need to initialize anything yourself, you can omit this
|
33
|
+
def initialize(name, options = {})
|
34
|
+
@name = name
|
35
|
+
attributary_attributes(options)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def check_description
|
41
|
+
description.length <= 1024
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
character = Character.new("Tommy", :age => 16, :description => "Hi I am Tommy", :gender => 'male')
|
46
|
+
character.age # 16
|
47
|
+
character.description # Hi I am Tommy
|
48
|
+
character.gender # male
|
49
|
+
|
50
|
+
character.age = 18
|
51
|
+
character.age # 18
|
52
|
+
```
|
149
53
|
|
150
|
-
|
151
|
-
include Attributary::DSL
|
152
|
-
|
153
|
-
donkey :gender, :symbol, default: :male, :collection => [:male, :female]
|
154
|
-
end
|
155
|
-
|
156
|
-
character = Character.new
|
157
|
-
character.attributary_errors # {}
|
158
|
-
character.gender = :left # raises CollectionValidationError
|
159
|
-
character.gender = 'female' # gets casted correctly, and doesn't raise an error
|
160
|
-
|
54
|
+
Lots more info on the [Wiki](https://github.com/joshmn/attributary/wiki)
|
161
55
|
|
162
56
|
## Contributing
|
163
57
|
|
data/lib/attributary.rb
CHANGED
@@ -2,14 +2,6 @@ require 'attributary/errors'
|
|
2
2
|
require 'attributary/config'
|
3
3
|
require 'attributary/core_ext'
|
4
4
|
require 'attributary/type'
|
5
|
-
require 'attributary/types/array_type'
|
6
|
-
require 'attributary/types/big_decimal_type'
|
7
|
-
require 'attributary/types/boolean_type'
|
8
|
-
require 'attributary/types/float_type'
|
9
|
-
require 'attributary/types/hash_type'
|
10
|
-
require 'attributary/types/integer_type'
|
11
|
-
require 'attributary/types/string_type'
|
12
|
-
require 'attributary/types/symbol_type'
|
13
5
|
require 'attributary/initializer'
|
14
6
|
require 'attributary/serializer'
|
15
7
|
require 'attributary/dsl'
|
@@ -18,9 +18,13 @@ module Attributary
|
|
18
18
|
if options[:validate].is_a?(Symbol)
|
19
19
|
unless send(options[:validate])
|
20
20
|
self.class._attributary_handle_error(name, value, :validation)
|
21
|
+
write = false
|
21
22
|
end
|
22
23
|
end
|
23
|
-
|
24
|
+
if write
|
25
|
+
instance_variable_set(:"@#{name}", value)
|
26
|
+
self.class._attributary_errors.delete(name)
|
27
|
+
end
|
24
28
|
end
|
25
29
|
end
|
26
30
|
|
@@ -18,8 +18,9 @@ module Attributary
|
|
18
18
|
def _attributary_check_collection(attribute, value, collection)
|
19
19
|
unless collection.include?(value)
|
20
20
|
_attributary_handle_error(attribute, value, :collection, :message => "Attribute #{attribute} `#{value}' is not in the collection #{collection}")
|
21
|
-
false
|
21
|
+
return false
|
22
22
|
end
|
23
|
+
true
|
23
24
|
end
|
24
25
|
|
25
26
|
def _attributary_validate_attribute(attribute, value, validator)
|
@@ -28,6 +29,7 @@ module Attributary
|
|
28
29
|
_attributary_handle_error(attribute, value, :validation)
|
29
30
|
false
|
30
31
|
end
|
32
|
+
true
|
31
33
|
end
|
32
34
|
|
33
35
|
def _add_attributary_error(name, klass, message)
|
data/lib/attributary/type.rb
CHANGED
@@ -7,3 +7,12 @@ module Attributary
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
10
|
+
|
11
|
+
require 'attributary/types/array_type'
|
12
|
+
require 'attributary/types/big_decimal_type'
|
13
|
+
require 'attributary/types/boolean_type'
|
14
|
+
require 'attributary/types/float_type'
|
15
|
+
require 'attributary/types/hash_type'
|
16
|
+
require 'attributary/types/integer_type'
|
17
|
+
require 'attributary/types/string_type'
|
18
|
+
require 'attributary/types/symbol_type'
|
data/lib/attributary/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attributary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Brody
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- lib/attributary/types/array_type.rb
|
102
102
|
- lib/attributary/types/big_decimal_type.rb
|
103
103
|
- lib/attributary/types/boolean_type.rb
|
104
|
+
- lib/attributary/types/fixnum_type.rb
|
104
105
|
- lib/attributary/types/float_type.rb
|
105
106
|
- lib/attributary/types/hash_type.rb
|
106
107
|
- lib/attributary/types/integer_type.rb
|