acfs 0.22.2.b193 → 0.22.2.b194

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NWI5YzUzYWUxMTk1ZTgyMjhhZDFiYmIzNTMwYTY4Zjg5ZThlYWM4ZQ==
4
+ ZDBlODUyYTBjYWJmOTJmYTczYjVjNTc5YTc3OTkzYjc3YTQ5NWE1NA==
5
5
  data.tar.gz: !binary |-
6
- NDBjYTFiZTg4ZGQ2YjQwNTZiNDE3NGE1ZTU4YmFhODc1ZWRhY2RkNg==
6
+ NzZiYTA2NGNhZWViZGM2NzY1YTcxYWYzZGNiNmZlYmU3YWM0ZTE1Yg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- N2E1ZWFmM2QzMTA0NWM5OWU2ZTgwYzlhNTY0MmIyNDA5MTc0ZTFhNTVmYzU2
10
- ZjI3MDA5NmRkYmI3YzNkOWU0N2ZiMTc4OGUzYWEzZWRiNWRhZGI5OWExMGQx
11
- YmY0ZjRkMjQ1YjQwZmZjNWY2MzVkZGFjNTEyZjdhMTAwNWY1M2Q=
9
+ OWUwYjdjMjVkMzMwY2Q4MGY1YWU2YmJiNWMxNDY3MDRkYWFkNWNhYzkwMGMx
10
+ NjZiZDZkYzg0NWM0ZTk1NWI2ZDg1ZmIwNmZkM2Q0NDUxZDYwMWFhMDM1NjMz
11
+ ZDdlNzU1ODEzMWI0ZDAxZmM5ZDQ5ZmNlN2M4ODFmYjY2MzlhMzU=
12
12
  data.tar.gz: !binary |-
13
- ZjcwMzEyYzhlNWQzNDM4MjI1NjQwYTg3YTEyOWU4M2QxZmNlMjM5MGNlMDk1
14
- ZWI0MWVhNDE3ZTNhYTcxZmU1ZGM4ODJiNzFiZjE2YjdmNTA0YzYyZWEwZjI3
15
- YWQ5MTJiY2FhZDhiNzQyMTM5MjQ2M2ZiYzY1N2Q3NWJjYWU4OTU=
13
+ YmNlOWU1YzkyNGNjZDRmMmI2ZWIzNDMwN2RhMWU0M2FkNTZmNjFjMjFkNzUy
14
+ ZWI5MmQ0MWZhM2YwYzY5MmZjYmRhZmViYzViYmI5OWRhN2ExZDc3Njc5NWE5
15
+ MGRiYmNjYmRkZGQ0YzcxNjljZmY2MzM2ODU1YjdkNDkxYjE5NmY=
@@ -125,11 +125,11 @@ module Acfs::Model
125
125
  # @raise [ ArgumentError ] If no attribute with given name is defined.
126
126
  #
127
127
  def write_attribute(name, value, opts = {})
128
- if (type = self.class.attribute_types[name.to_sym]).nil?
128
+ if (attr = self.class.defined_attributes[name.to_s]).nil?
129
129
  raise ArgumentError.new "Unknown attribute `#{name}`."
130
130
  end
131
131
 
132
- write_raw_attribute name, value.nil? ? nil : type.cast(value), opts
132
+ write_raw_attribute name, attr.cast(value), opts
133
133
  end
134
134
 
135
135
  # @api private
@@ -187,7 +187,15 @@ module Acfs::Model
187
187
  # @return [ Hash{ String => Object, Proc } ] Attributes with default values.
188
188
  #
189
189
  def attributes
190
- @attributes ||= {}.merge superclass.respond_to?(:attributes) ? superclass.attributes : {}
190
+ Hash.new.tap do |attrs|
191
+ defined_attributes.each do |key, attr|
192
+ attrs[key] = attr.default_value
193
+ end
194
+ end
195
+ end
196
+
197
+ def defined_attributes
198
+ @attributes ||= {}.merge superclass.respond_to?(:defined_attributes) ? superclass.defined_attributes : {}
191
199
  end
192
200
 
193
201
  # @api public
@@ -211,11 +219,9 @@ module Acfs::Model
211
219
  private
212
220
  def define_attribute(name, type, opts = {})
213
221
  name = name.to_s
214
- default_value = opts.has_key?(:default) ? opts[:default] : nil
215
- default_value = type.cast default_value unless default_value.is_a? Proc or default_value.nil?
222
+ attribute = type.new opts
216
223
 
217
- attributes[name] = default_value
218
- attribute_types[name.to_sym] = type
224
+ defined_attributes[name] = attribute
219
225
  define_attribute_method name
220
226
 
221
227
  self.send :define_method, name do
@@ -0,0 +1,28 @@
1
+ module Acfs::Model::Attributes
2
+
3
+ class Base
4
+ attr_reader :options
5
+
6
+ def initialize(opts = {})
7
+ @options = opts
8
+ @options.reverse_merge! allow_nil: true
9
+ end
10
+
11
+ def nil_allowed?
12
+ !!options[:allow_nil]
13
+ end
14
+
15
+ def default_value
16
+ options[:default].is_a?(Proc) ? options[:default] : cast(options[:default])
17
+ end
18
+
19
+ def cast(obj)
20
+ return nil if obj.nil? && nil_allowed?
21
+ cast_type obj
22
+ end
23
+
24
+ def cast_type(obj)
25
+ raise NotImplementedError
26
+ end
27
+ end
28
+ end
@@ -16,7 +16,7 @@ module Acfs::Model
16
16
  #
17
17
  # true, on, yes
18
18
  #
19
- module Boolean
19
+ class Boolean < Base
20
20
 
21
21
  TRUE_VALUES = %w(true on yes)
22
22
 
@@ -27,7 +27,7 @@ module Acfs::Model
27
27
  # @param [Object] obj Object to cast.
28
28
  # @return [TrueClass, FalseClass] Casted boolean.
29
29
  #
30
- def self.cast(obj)
30
+ def cast_type(obj)
31
31
  return true if obj.is_a? TrueClass
32
32
  return false if obj.is_a? FalseClass
33
33
 
@@ -1,31 +1,30 @@
1
- module Acfs::Model
2
- module Attributes
1
+ module Acfs::Model::Attributes
2
+
3
+ # @api public
4
+ #
5
+ # DateTime attribute type. Use it in your model as an attribute type:
6
+ #
7
+ # @example
8
+ # class User
9
+ # include Acfs::Model
10
+ # attribute :name, :date_time
11
+ # end
12
+ #
13
+ class DateTime < Base
3
14
 
4
15
  # @api public
5
16
  #
6
- # DateTime attribute type. Use it in your model as an attribute type:
17
+ # Cast given object to DateTime.
18
+ # Expect
7
19
  #
8
- # @example
9
- # class User
10
- # include Acfs::Model
11
- # attribute :name, :date_time
12
- # end
20
+ # @param [Object] obj Object to cast.
21
+ # @return [DateTime] Casted object as DateTime.
13
22
  #
14
- module DateTime
15
-
16
- # @api public
17
- #
18
- # Cast given object to DateTime.
19
- # Expect
20
- #
21
- # @param [Object] obj Object to cast.
22
- # @return [DateTime] Casted object as DateTime.
23
- #
24
- def self.cast(obj)
25
- return obj if obj.is_a? ::DateTime
26
- return ::DateTime.iso8601(obj.iso8601) if obj.is_a? Time or obj.is_a? Date
27
- return ::DateTime.iso8601(obj)
28
- end
23
+ def cast_type(obj)
24
+ return nil if nil_allowed? and obj.blank?
25
+ return obj if obj.is_a? ::DateTime
26
+ return ::DateTime.iso8601(obj.iso8601) if obj.is_a? Time or obj.is_a? Date
27
+ return ::DateTime.iso8601(obj)
29
28
  end
30
29
  end
31
30
  end
@@ -1,28 +1,26 @@
1
- module Acfs::Model
2
- module Attributes
1
+ module Acfs::Model::Attributes
2
+
3
+ # @api public
4
+ #
5
+ # Float attribute type. Use it in your model as an attribute type:
6
+ #
7
+ # @example
8
+ # class User
9
+ # include Acfs::Model
10
+ # attribute :name, :float
11
+ # end
12
+ #
13
+ class Float < Base
3
14
 
4
15
  # @api public
5
16
  #
6
- # Float attribute type. Use it in your model as an attribute type:
17
+ # Cast given object to float.
7
18
  #
8
- # @example
9
- # class User
10
- # include Acfs::Model
11
- # attribute :name, :float
12
- # end
19
+ # @param [Object] obj Object to cast.
20
+ # @return [Float] Casted object as float.
13
21
  #
14
- module Float
15
-
16
- # @api public
17
- #
18
- # Cast given object to float.
19
- #
20
- # @param [Object] obj Object to cast.
21
- # @return [Float] Casted object as float.
22
- #
23
- def self.cast(obj)
24
- obj.to_f
25
- end
22
+ def cast_type(obj)
23
+ obj.to_f
26
24
  end
27
25
  end
28
26
  end
@@ -1,28 +1,26 @@
1
- module Acfs::Model
2
- module Attributes
1
+ module Acfs::Model::Attributes
2
+
3
+ # @api public
4
+ #
5
+ # Integer attribute type. Use it in your model as an attribute type:
6
+ #
7
+ # @example
8
+ # class User
9
+ # include Acfs::Model
10
+ # attribute :name, :integer
11
+ # end
12
+ #
13
+ class Integer < Base
3
14
 
4
15
  # @api public
5
16
  #
6
- # Integer attribute type. Use it in your model as an attribute type:
17
+ # Cast given object to integer.
7
18
  #
8
- # @example
9
- # class User
10
- # include Acfs::Model
11
- # attribute :name, :integer
12
- # end
19
+ # @param [Object] obj Object to cast.
20
+ # @return [Fixnum] Casted object as fixnum.
13
21
  #
14
- module Integer
15
-
16
- # @api public
17
- #
18
- # Cast given object to integer.
19
- #
20
- # @param [Object] obj Object to cast.
21
- # @return [Fixnum] Casted object as fixnum.
22
- #
23
- def self.cast(obj)
24
- obj.to_i
25
- end
22
+ def cast_type(obj)
23
+ obj.to_i
26
24
  end
27
25
  end
28
26
  end
@@ -1,30 +1,28 @@
1
- module Acfs::Model
2
- module Attributes
1
+ module Acfs::Model::Attributes
2
+
3
+ # @api public
4
+ #
5
+ # List attribute type. Use it in your model as an attribute type:
6
+ #
7
+ # @example
8
+ # class User
9
+ # include Acfs::Model
10
+ # attribute :name, :list
11
+ # end
12
+ #
13
+ class List < Base
3
14
 
4
15
  # @api public
5
16
  #
6
- # List attribute type. Use it in your model as an attribute type:
17
+ # Cast given object to a list.
7
18
  #
8
- # @example
9
- # class User
10
- # include Acfs::Model
11
- # attribute :name, :list
12
- # end
19
+ # @param [Object] obj Object to cast.
20
+ # @return [Fixnum] Casted object as list.
21
+ # @raise [TypeError] If object cannot be casted to a list.
13
22
  #
14
- module List
15
-
16
- # @api public
17
- #
18
- # Cast given object to a list.
19
- #
20
- # @param [Object] obj Object to cast.
21
- # @return [Fixnum] Casted object as list.
22
- # @raise [TypeError] If object cannot be casted to a list.
23
- #
24
- def self.cast(obj)
25
- return obj.to_a if obj.respond_to? :to_a
26
- raise TypeError.new "Cannot cast #{obj.inspect} to array."
27
- end
23
+ def cast_type(obj)
24
+ return obj.to_a if obj.respond_to? :to_a
25
+ raise TypeError.new "Cannot cast #{obj.inspect} to array."
28
26
  end
29
27
  end
30
28
  end
@@ -1,28 +1,26 @@
1
- module Acfs::Model
2
- module Attributes
1
+ module Acfs::Model::Attributes
2
+
3
+ # @api public
4
+ #
5
+ # String attribute type. Use it in your model as an attribute type:
6
+ #
7
+ # @example
8
+ # class User
9
+ # include Acfs::Model
10
+ # attribute :name, :string
11
+ # end
12
+ #
13
+ class String < Base
3
14
 
4
15
  # @api public
5
16
  #
6
- # String attribute type. Use it in your model as an attribute type:
17
+ # Cast given object to string.
7
18
  #
8
- # @example
9
- # class User
10
- # include Acfs::Model
11
- # attribute :name, :string
12
- # end
19
+ # @param [Object] obj Object to cast.
20
+ # @return [String] Casted string.
13
21
  #
14
- module String
15
-
16
- # @api public
17
- #
18
- # Cast given object to string.
19
- #
20
- # @param [Object] obj Object to cast.
21
- # @return [String] Casted string.
22
- #
23
- def self.cast(obj)
24
- obj.to_s
25
- end
22
+ def cast_type(obj)
23
+ obj.to_s
26
24
  end
27
25
  end
28
26
  end
@@ -2,44 +2,54 @@ require 'spec_helper'
2
2
 
3
3
  describe Acfs::Model::Attributes::DateTime do
4
4
  let(:model) { Class.new.tap { |c| c.send :include, Acfs::Model }}
5
+ let(:params) { {} }
6
+ subject { Acfs::Model::Attributes::DateTime.new params }
5
7
 
6
8
  describe 'cast' do
7
9
  it 'should return same object, if obj is already of DateTime class' do
8
10
  date_time = DateTime.now
9
- retval = Acfs::Model::Attributes::DateTime.cast(date_time)
11
+ retval = subject.cast(date_time)
10
12
  expect(retval).to be == date_time
11
13
  end
12
14
 
13
15
  it 'should return parsed object, if obj is of Time class' do
14
16
  time = Time.now
15
- retval = Acfs::Model::Attributes::DateTime.cast(time)
17
+ retval = subject.cast(time)
16
18
  expect(retval).to be == DateTime.iso8601(time.iso8601)
17
19
  end
18
20
 
19
21
  it 'should return parsed object, if obj is of Date class' do
20
22
  date = Date.today
21
- retval = Acfs::Model::Attributes::DateTime.cast(date)
23
+ retval = subject.cast(date)
22
24
  expect(retval).to be == DateTime.iso8601(date.iso8601)
23
25
  end
24
26
 
25
27
  it 'should return parsed object, if obj is of String class in ISO-8601 format' do
26
28
  date_time_string = DateTime.now.iso8601
27
- retval = Acfs::Model::Attributes::DateTime.cast(date_time_string)
29
+ retval = subject.cast(date_time_string)
28
30
  expect(retval).to be == DateTime.iso8601(date_time_string)
29
31
  end
30
32
 
31
33
  it 'should raise an error if obj is of String class not in valid ISO-8601 format' do
32
34
  malformed_string = 'qwe123'
33
35
  expect {
34
- Acfs::Model::Attributes::DateTime.cast(malformed_string)
36
+ subject.cast(malformed_string)
35
37
  }.to raise_error ArgumentError
36
38
  end
37
39
 
38
40
  it 'should raise an error if obj is of wrong class (Fixnum)' do
39
41
  fixnum = 12
40
42
  expect {
41
- Acfs::Model::Attributes::DateTime.cast(fixnum)
43
+ subject.cast(fixnum)
42
44
  }.to raise_error TypeError
43
45
  end
46
+
47
+ context 'with allow_nil option' do
48
+ let(:params) { {allow_nil: true} }
49
+
50
+ it 'should accept empty string as nil' do
51
+ expect(subject.cast('')).to eq nil
52
+ end
53
+ end
44
54
  end
45
55
  end
@@ -2,21 +2,19 @@ require 'spec_helper'
2
2
 
3
3
  describe Acfs::Model::Attributes::Float do
4
4
  let(:model) { Class.new.tap { |c| c.send :include, Acfs::Model }}
5
+ subject { Acfs::Model::Attributes::Float.new }
5
6
 
6
7
  describe 'cast' do
7
8
  it 'should return same object, if obj is already of float class' do
8
- retval = Acfs::Model::Attributes::Float.cast(1.3)
9
- expect(retval).to be == 1.3
9
+ expect(subject.cast(1.3)).to be == 1.3
10
10
  end
11
11
 
12
12
  it 'should return parsed object, if obj is of Fixnum class' do
13
- retval = Acfs::Model::Attributes::Float.cast(7)
14
- expect(retval).to be == 7.0
13
+ expect(subject.cast(7)).to be == 7.0
15
14
  end
16
15
 
17
16
  it 'should return parsed object, if obj is of String class containing a float' do
18
- retval = Acfs::Model::Attributes::Float.cast('1.7')
19
- expect(retval).to be == 1.7
17
+ expect(subject.cast('1.7')).to be == 1.7
20
18
  end
21
19
  end
22
20
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Acfs::Model::Attributes::List do
4
4
  let(:model) { Class.new.tap { |c| c.send :include, Acfs::Model }}
5
- subject { Acfs::Model::Attributes::List }
5
+ subject { Acfs::Model::Attributes::List.new }
6
6
 
7
7
  describe '.cast' do
8
8
  context 'with array' do
@@ -115,29 +115,42 @@ describe Acfs::Model::Attributes do
115
115
  end
116
116
  end
117
117
 
118
- describe '.attribute' do
119
- it 'should add an attribute to model attribute list' do
120
- model.send :attribute, :name, :string
118
+ describe 'class' do
119
+ describe '#attribute' do
120
+ it 'should add an attribute to model attribute list' do
121
+ model.send :attribute, :name, :string
121
122
 
122
- expect(model.attributes).to be == { :name => nil }.stringify_keys
123
- end
123
+ expect(model.attributes).to be == { :name => nil }.stringify_keys
124
+ end
124
125
 
125
- it 'should accept a default value' do
126
- model.send :attribute, :name, :string, default: 'John'
126
+ it 'should accept a default value' do
127
+ model.send :attribute, :name, :string, default: 'John'
127
128
 
128
- expect(model.attributes).to be == { :name => 'John' }.stringify_keys
129
- end
129
+ expect(model.attributes).to be == { :name => 'John' }.stringify_keys
130
+ end
130
131
 
131
- it 'should accept an symbolic type' do
132
- model.send :attribute, :age, :integer, default: '12'
132
+ it 'should accept an symbolic type' do
133
+ model.send :attribute, :age, :integer, default: '12'
133
134
 
134
- expect(model.attributes).to be == { :age => 12 }.stringify_keys
135
- end
135
+ expect(model.attributes).to be == { :age => 12 }.stringify_keys
136
+ end
137
+
138
+ it 'should accept an class type' do
139
+ model.send :attribute, :age, Acfs::Model::Attributes::Integer, default: '12'
136
140
 
137
- it 'should accept an class type' do
138
- model.send :attribute, :age, Acfs::Model::Attributes::Integer, default: '12'
141
+ expect(model.attributes).to be == { :age => 12 }.stringify_keys
142
+ end
139
143
 
140
- expect(model.attributes).to be == { :age => 12 }.stringify_keys
144
+ context 'allow nil option' do
145
+ it 'should allow nil as value' do
146
+ model.send :attribute, :updated_at, Acfs::Model::Attributes::DateTime, default: DateTime.new, allow_nil: true
147
+ resource = model.new
148
+ expect(resource.updated_at).to eq DateTime.new
149
+
150
+ resource.updated_at = ''
151
+ expect(resource.updated_at).to eq nil
152
+ end
153
+ end
141
154
  end
142
155
  end
143
156
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.2.b193
4
+ version: 0.22.2.b194
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-24 00:00:00.000000000 Z
11
+ date: 2013-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -145,6 +145,7 @@ files:
145
145
  - lib/acfs/middleware/print.rb
146
146
  - lib/acfs/model.rb
147
147
  - lib/acfs/model/attributes.rb
148
+ - lib/acfs/model/attributes/base.rb
148
149
  - lib/acfs/model/attributes/boolean.rb
149
150
  - lib/acfs/model/attributes/date_time.rb
150
151
  - lib/acfs/model/attributes/float.rb