koine-attributes 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 +12 -2
- data/lib/koine/attributes.rb +17 -2
- data/lib/koine/attributes/adapter/any.rb +13 -0
- data/lib/koine/attributes/adapter/array_of.rb +17 -0
- data/lib/koine/attributes/adapter/base.rb +5 -1
- data/lib/koine/attributes/adapter/hash_of.rb +25 -0
- data/lib/koine/attributes/adapter/symbol.rb +11 -0
- data/lib/koine/attributes/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e7052e1bd263822e4624f61bf1d33aa63663b3a
|
4
|
+
data.tar.gz: 9734c2c9f6dad10d63ae547898f74f82b07ef707
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4444da438bc1c5d497b6c4752d4b5672ebd4a9591318b51e0e5282588d96a658cb9d46ec52d54370f3d0c7e727b58d106b005eab40801a3858a1d7cb1619d08
|
7
|
+
data.tar.gz: 02f6055a3d32f166f4091c38fd5e7c3577b261382f086d39a33cbad2e015853c0cbd2845c0ab30ddceb0bd9c460045e7e3136c7dfaa0ed4df1ad5287bdb81ca7
|
data/README.md
CHANGED
@@ -82,7 +82,7 @@ Options:
|
|
82
82
|
|
83
83
|
person = Person.new(name: 'John Doe', birthday: '2001-01-31')
|
84
84
|
|
85
|
-
foo: attribute will raise error
|
85
|
+
# foo: attribute will raise error
|
86
86
|
person = Person.new(name: 'John Doe', birthday: '2001-01-31', foo: :bar)
|
87
87
|
```
|
88
88
|
|
@@ -110,6 +110,8 @@ class Product
|
|
110
110
|
attribute :price, MyCustom::Money.new
|
111
111
|
attribute :available, :boolean, ->(attribues){ attributes.with_default_value(true) }
|
112
112
|
attribute :available, Koine::Attributes::Drivers::Boolean.new.with_default_value(true)
|
113
|
+
attribute :tags, array_of(:string)
|
114
|
+
attribute :config, hash_of(:symbol, :string)
|
113
115
|
end
|
114
116
|
end
|
115
117
|
|
@@ -121,6 +123,10 @@ product.available # => false
|
|
121
123
|
|
122
124
|
product.price = { currency: 'USD', value: 100 }
|
123
125
|
|
126
|
+
product.tags = ['new']
|
127
|
+
|
128
|
+
product.config = { short_url: 'http://config.url' }
|
129
|
+
|
124
130
|
# or
|
125
131
|
product.price = "100 USD"
|
126
132
|
```
|
@@ -167,11 +173,15 @@ new_location = location.with_lon(3)
|
|
167
173
|
### Standard types
|
168
174
|
|
169
175
|
```ruby
|
170
|
-
:
|
176
|
+
:any
|
177
|
+
:array_of
|
178
|
+
:boolean
|
171
179
|
:date
|
172
180
|
:float
|
181
|
+
:hash_of
|
173
182
|
:integer
|
174
183
|
:string
|
184
|
+
:symbol
|
175
185
|
:time
|
176
186
|
```
|
177
187
|
|
data/lib/koine/attributes.rb
CHANGED
@@ -104,12 +104,16 @@ module Koine
|
|
104
104
|
autoload :AttributesFactory, 'koine/attributes/attributes_factory'
|
105
105
|
|
106
106
|
module Adapter
|
107
|
+
autoload :Any, 'koine/attributes/adapter/any'
|
108
|
+
autoload :ArrayOf, 'koine/attributes/adapter/array_of'
|
107
109
|
autoload :Boolean, 'koine/attributes/adapter/boolean'
|
108
110
|
autoload :Date, 'koine/attributes/adapter/date'
|
109
|
-
autoload :Time, 'koine/attributes/adapter/time'
|
110
111
|
autoload :Float, 'koine/attributes/adapter/float'
|
112
|
+
autoload :HashOf, 'koine/attributes/adapter/hash_of'
|
111
113
|
autoload :Integer, 'koine/attributes/adapter/integer'
|
112
114
|
autoload :String, 'koine/attributes/adapter/string'
|
115
|
+
autoload :Symbol, 'koine/attributes/adapter/symbol'
|
116
|
+
autoload :Time, 'koine/attributes/adapter/time'
|
113
117
|
end
|
114
118
|
|
115
119
|
Error = Class.new(StandardError)
|
@@ -144,7 +148,7 @@ module Koine
|
|
144
148
|
end
|
145
149
|
|
146
150
|
block = lambda_arg || block
|
147
|
-
|
151
|
+
@_attributes_factory.add_attribute(name, adapter, &block)
|
148
152
|
|
149
153
|
instance_eval do
|
150
154
|
def_delegators :attributes, name, "#{name}=", "with_#{name}"
|
@@ -154,6 +158,17 @@ module Koine
|
|
154
158
|
end
|
155
159
|
end
|
156
160
|
end
|
161
|
+
|
162
|
+
def array_of(item_adapter)
|
163
|
+
adapter = @_attributes_factory.coerce_adapter(item_adapter)
|
164
|
+
Adapter::ArrayOf.new(adapter)
|
165
|
+
end
|
166
|
+
|
167
|
+
def hash_of(key_adapter, value_adapter)
|
168
|
+
key_adapter = @_attributes_factory.coerce_adapter(key_adapter)
|
169
|
+
value_adapter = @_attributes_factory.coerce_adapter(value_adapter)
|
170
|
+
Adapter::HashOf.new(key_adapter, value_adapter)
|
171
|
+
end
|
157
172
|
end
|
158
173
|
end
|
159
174
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Koine
|
2
|
+
module Attributes
|
3
|
+
module Adapter
|
4
|
+
class ArrayOf < Base
|
5
|
+
def initialize(adapter)
|
6
|
+
@adapter = adapter
|
7
|
+
end
|
8
|
+
|
9
|
+
def coerce(collectionOfValues)
|
10
|
+
secure do
|
11
|
+
collectionOfValues.map { |value| @adapter.coerce(value) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -23,7 +23,11 @@ module Koine
|
|
23
23
|
# duplicates if possible and freezes object
|
24
24
|
def secure
|
25
25
|
value = yield
|
26
|
-
|
26
|
+
|
27
|
+
unless value.is_a?(::Symbol)
|
28
|
+
value = value.dup if value.respond_to?(:dup)
|
29
|
+
end
|
30
|
+
|
27
31
|
value.freeze
|
28
32
|
end
|
29
33
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Koine
|
2
|
+
module Attributes
|
3
|
+
module Adapter
|
4
|
+
class HashOf < Base
|
5
|
+
def initialize(key_adapter, value_adapter)
|
6
|
+
@key_adapter = key_adapter || raise(ArgumentError, 'Invalid key adapter')
|
7
|
+
@value_adapter = value_adapter || raise(ArgumentError, 'Invalid value adapter')
|
8
|
+
with_default_value({})
|
9
|
+
end
|
10
|
+
|
11
|
+
def coerce(hash)
|
12
|
+
secure do
|
13
|
+
{}.tap do |new_hash|
|
14
|
+
hash.each do |key, value|
|
15
|
+
key = @key_adapter.coerce(key)
|
16
|
+
value = @value_adapter.coerce(value)
|
17
|
+
new_hash[key] = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: koine-attributes
|
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
|
- Marcelo Jacobus
|
@@ -127,12 +127,16 @@ files:
|
|
127
127
|
- bin/setup
|
128
128
|
- koine-attributes.gemspec
|
129
129
|
- lib/koine/attributes.rb
|
130
|
+
- lib/koine/attributes/adapter/any.rb
|
131
|
+
- lib/koine/attributes/adapter/array_of.rb
|
130
132
|
- lib/koine/attributes/adapter/base.rb
|
131
133
|
- lib/koine/attributes/adapter/boolean.rb
|
132
134
|
- lib/koine/attributes/adapter/date.rb
|
133
135
|
- lib/koine/attributes/adapter/float.rb
|
136
|
+
- lib/koine/attributes/adapter/hash_of.rb
|
134
137
|
- lib/koine/attributes/adapter/integer.rb
|
135
138
|
- lib/koine/attributes/adapter/string.rb
|
139
|
+
- lib/koine/attributes/adapter/symbol.rb
|
136
140
|
- lib/koine/attributes/adapter/time.rb
|
137
141
|
- lib/koine/attributes/attributes.rb
|
138
142
|
- lib/koine/attributes/attributes_factory.rb
|