tainbox 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +95 -0
- data/lib/tainbox/version.rb +1 -1
- data/tainbox.gemspec +3 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f97ba89e290c39aa627d72acdd087f15d8425535
|
4
|
+
data.tar.gz: 4187cef87c10dea2fc9be5cf80c26f0bc936ff40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d445dad53f0615cd342ec4adb1bd80885328e68308d933f13c7993066fd168255d7703c4540354b02f698051082ff2a665649d36cfeea4c0062ca1b8aee7bc57
|
7
|
+
data.tar.gz: 103da0ff13be9e547dfa04f40a63d8507de78bbcde8e0a3a63e43273a265b24f6e233e555ba68b00fcd24db379866e0b8bf80039fa44d4c09dd3c2b55c527404
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Tainbox
|
2
|
+
|
3
|
+
Tainbox is a utility gem that can be used to inject attributes into ruby objects. It is similar to <a href="https://github.com/solnic/virtus">Virtus</a>, but works a bit more sensibly (hopefully) and throws in some additional features.
|
4
|
+
|
5
|
+
## But we already have attr_accessor!
|
6
|
+
|
7
|
+
Consider this code:
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
class Person
|
11
|
+
include Tainbox
|
12
|
+
attribute :name, default: -> { "person_#{age}" }
|
13
|
+
attribute :age, Integer, default: 20
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
Here are the basic features you get:
|
18
|
+
|
19
|
+
``` ruby
|
20
|
+
person = Person.new(name: 'John', age: '30')
|
21
|
+
person.attributes # => { :name => "John", :age => 30 }
|
22
|
+
person.age = 50
|
23
|
+
person.age # => 50
|
24
|
+
person.attributes = {}
|
25
|
+
person.attributes # => { :name => "person_20", :age => 20 }
|
26
|
+
```
|
27
|
+
|
28
|
+
## Additional features
|
29
|
+
|
30
|
+
### Method overrides
|
31
|
+
|
32
|
+
Tainbox attributes can be freely overriden:
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
def name
|
36
|
+
super.strip
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
``` ruby
|
41
|
+
def name=(value)
|
42
|
+
super('Stupid example')
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
### #attribute_provided?
|
47
|
+
|
48
|
+
Attribute is considered provided if its setter was explicitly invoked via a setter, `.new`, or `#attributes=` or it has a default value.
|
49
|
+
|
50
|
+
``` ruby
|
51
|
+
class Person
|
52
|
+
include Tainbox
|
53
|
+
attribute :name, default: 'John'
|
54
|
+
attribute :age
|
55
|
+
end
|
56
|
+
|
57
|
+
person = Person.new
|
58
|
+
person.attribute_provided?(:age) # => false
|
59
|
+
person.attribute_provided?(:name) # => true
|
60
|
+
```
|
61
|
+
|
62
|
+
### readonly and writeonly attributes
|
63
|
+
|
64
|
+
Speaks for itself:
|
65
|
+
|
66
|
+
``` ruby
|
67
|
+
class Person
|
68
|
+
include Tainbox
|
69
|
+
attribute :name, writeonly: true
|
70
|
+
attribute :age, readonly: true
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
### Built-in type converters
|
75
|
+
|
76
|
+
All converters return nil if conversion could not be made.
|
77
|
+
|
78
|
+
- Integer
|
79
|
+
- Float
|
80
|
+
- String
|
81
|
+
- Symbol
|
82
|
+
- Time
|
83
|
+
- Boolean
|
84
|
+
|
85
|
+
### Custom type converters
|
86
|
+
|
87
|
+
You can define a custom type converter like so:
|
88
|
+
|
89
|
+
``` ruby
|
90
|
+
# value and options are automatically available in scope
|
91
|
+
Tainbox.define_converter(MyType) do
|
92
|
+
# Do whatever you want with value
|
93
|
+
options[:strict] ? MyType.convert!(value) : MyType.convert(value)
|
94
|
+
end
|
95
|
+
```
|
data/lib/tainbox/version.rb
CHANGED
data/tainbox.gemspec
CHANGED
@@ -8,8 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ['Dmitry Gubitskiy']
|
9
9
|
spec.email = ['d.gubitskiy@gmail.com']
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
spec.summary = 'Tainbox is a utility gem that can be used to inject attributes '\
|
12
|
+
'into ruby objects. It is similar to Virtus, but works a bit more '\
|
13
|
+
'sensibly (hopefully) and throws in some additional features'
|
13
14
|
spec.homepage = 'https://github.com/enthrops/tainbox'
|
14
15
|
spec.license = 'MIT'
|
15
16
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tainbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Gubitskiy
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- ".rspec"
|
78
78
|
- Gemfile
|
79
79
|
- LICENSE.txt
|
80
|
+
- README.md
|
80
81
|
- bin/console
|
81
82
|
- lib/tainbox.rb
|
82
83
|
- lib/tainbox/attribute_definer.rb
|
@@ -110,6 +111,8 @@ rubyforge_project:
|
|
110
111
|
rubygems_version: 2.4.6
|
111
112
|
signing_key:
|
112
113
|
specification_version: 4
|
113
|
-
summary: Tainbox
|
114
|
+
summary: Tainbox is a utility gem that can be used to inject attributes into ruby
|
115
|
+
objects. It is similar to Virtus, but works a bit more sensibly (hopefully) and
|
116
|
+
throws in some additional features
|
114
117
|
test_files: []
|
115
118
|
has_rdoc:
|