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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: deb27fe7217e17e870a86be97914caa6b8d06694
4
- data.tar.gz: 6b64a5897f5e12eb03d5e62551697733711df8b8
3
+ metadata.gz: 4e7052e1bd263822e4624f61bf1d33aa63663b3a
4
+ data.tar.gz: 9734c2c9f6dad10d63ae547898f74f82b07ef707
5
5
  SHA512:
6
- metadata.gz: 38fec92fd56ebd54e19cccfc2ed7c640b20f4d453f23ef25fed90c8ed63b168ba43548fe98e7edf3d26dd6e6f262f8c1ba0bfda07a6b538631f7460d0397ed51
7
- data.tar.gz: f0f593389f582857f67bec046cb7338a593282e9f2dee18e489c31cf63b5db9193af87d678252771c6f57a6c70a7759d009e4f2ae2eda432bd9ae3b53d467bd7
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
- :boolean, ->(adapter) { adapter.append_true_value('yes').append_false_value('no') }
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
 
@@ -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
- instance_variable_get(:@_attributes_factory).add_attribute(name, adapter, &block)
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,13 @@
1
+ module Koine
2
+ module Attributes
3
+ module Adapter
4
+ class Any < Base
5
+ def coerce(value)
6
+ secure do
7
+ value
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ 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
- value = value.dup if value.respond_to?(:dup)
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
@@ -0,0 +1,11 @@
1
+ module Koine
2
+ module Attributes
3
+ module Adapter
4
+ class Symbol < Base
5
+ def coerce(value)
6
+ value.to_sym
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module Koine
2
2
  module Attributes
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.2.1'.freeze
4
4
  end
5
5
  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.0
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