fast_attributes 0.4.0 → 0.5.0.rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/lib/fast_attributes/builder.rb +3 -10
- data/lib/fast_attributes/type_cast.rb +38 -0
- data/lib/fast_attributes/version.rb +1 -1
- data/lib/fast_attributes.rb +23 -13
- data/spec/fast_attributes/type_cast_spec.rb +88 -0
- data/spec/fast_attributes_spec.rb +7 -14
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21691e1f341e62ad8f2385beba675b5336dcec8d
|
4
|
+
data.tar.gz: 4fea26af5ad7141d8fa7135cfd89a14f157d54f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1b6315c4da736758364a2d960b4ba2d6aefa5ada49e72621e176ca87fc012ed0ddaf95dc950732683e8a2ceed4b29f81f86c36802547f0b6be0ed252b8d6568
|
7
|
+
data.tar.gz: 40d037c007494c058666dd5cd423e5196da90d3b199280672f3eb0c04c35e0c268bd77e169b4e8f3e23036ecd0c140d4d40f0ff170001dd3d94849838a1f93a0
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,29 @@
|
|
1
|
+
**0.5.0 (...)***
|
2
|
+
* Allow to control any switch statements during typecasting usign new DSL.
|
3
|
+
|
4
|
+
The default typecasting rule which `fast_attributes` generates for `String` is:
|
5
|
+
```ruby
|
6
|
+
case value
|
7
|
+
when nil then nil
|
8
|
+
when String then value
|
9
|
+
else String(%s)
|
10
|
+
end
|
11
|
+
```
|
12
|
+
Method `FastAttributes.set_type_casting` allows only to change `else` condition.
|
13
|
+
```ruby
|
14
|
+
FastAttributes.set_type_casting(String, 'String("#{%s}-suffix")')
|
15
|
+
```
|
16
|
+
|
17
|
+
Using `FastAttributes.type_cast` method it's possible to define custom `switch` condition
|
18
|
+
```ruby
|
19
|
+
FastAttributes.type_cast String do # case value
|
20
|
+
from 'nil', to: 'nil' # when nil then nil
|
21
|
+
from 'String', to: '%s' # when String then value
|
22
|
+
from Array, to: 'raise "Error"' # when Array then raise "Error"
|
23
|
+
otherwise 'String(%s)' # else String(value)
|
24
|
+
end # end
|
25
|
+
```
|
26
|
+
|
1
27
|
**0.4.0 (July 5, 2014)**
|
2
28
|
* Allow to override generated methods
|
3
29
|
|
@@ -47,17 +47,10 @@ module FastAttributes
|
|
47
47
|
|
48
48
|
def compile_setter
|
49
49
|
each_attribute do |attribute, type|
|
50
|
-
|
51
|
-
type_casting = FastAttributes.get_type_casting(type) % 'value'
|
52
|
-
|
50
|
+
type_cast = FastAttributes.get_type_casting(type)
|
53
51
|
@methods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
54
|
-
def #{attribute}=(value)
|
55
|
-
@#{attribute} =
|
56
|
-
when nil then nil # when nil then nil
|
57
|
-
#{type_matching} # when String then value
|
58
|
-
else # else
|
59
|
-
#{type_casting} # String(value)
|
60
|
-
end # end
|
52
|
+
def #{attribute}=(value)
|
53
|
+
@#{attribute} = #{type_cast.template.gsub('%s', 'value')}
|
61
54
|
end
|
62
55
|
EOS
|
63
56
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module FastAttributes
|
2
|
+
class TypeCast
|
3
|
+
class UnknownTypeCastingError < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@if_conditions = []
|
8
|
+
@else_condition = %q(raise UnknownTypeCastingError, 'Type casting is not defined')
|
9
|
+
end
|
10
|
+
|
11
|
+
def from(condition, options = {})
|
12
|
+
@if_conditions << [condition, options[:to]]
|
13
|
+
end
|
14
|
+
|
15
|
+
def otherwise(else_condition)
|
16
|
+
@else_condition = else_condition
|
17
|
+
end
|
18
|
+
|
19
|
+
def template
|
20
|
+
@template ||= begin
|
21
|
+
if @if_conditions.any?
|
22
|
+
conditions = @if_conditions.map do |from, to|
|
23
|
+
"when #{from}\n" +
|
24
|
+
" #{to}\n"
|
25
|
+
end
|
26
|
+
|
27
|
+
"case %s\n" +
|
28
|
+
conditions.join +
|
29
|
+
"else\n" +
|
30
|
+
" #{@else_condition}\n" +
|
31
|
+
"end"
|
32
|
+
else
|
33
|
+
@else_condition
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/fast_attributes.rb
CHANGED
@@ -1,18 +1,13 @@
|
|
1
1
|
require 'date'
|
2
2
|
require 'time'
|
3
|
+
require 'fast_attributes/version'
|
4
|
+
require 'fast_attributes/builder'
|
5
|
+
require 'fast_attributes/type_cast'
|
3
6
|
|
4
7
|
module FastAttributes
|
5
8
|
class << self
|
6
9
|
def type_casting
|
7
|
-
@type_casting ||= {
|
8
|
-
String => 'String(%s)',
|
9
|
-
Integer => 'Integer(%s)',
|
10
|
-
Float => 'Float(%s)',
|
11
|
-
Array => 'Array(%s)',
|
12
|
-
Date => 'Date.parse(%s)',
|
13
|
-
Time => 'Time.parse(%s)',
|
14
|
-
DateTime => 'DateTime.parse(%s)'
|
15
|
-
}
|
10
|
+
@type_casting ||= {}
|
16
11
|
end
|
17
12
|
|
18
13
|
def get_type_casting(klass)
|
@@ -20,7 +15,11 @@ module FastAttributes
|
|
20
15
|
end
|
21
16
|
|
22
17
|
def set_type_casting(klass, casting)
|
23
|
-
|
18
|
+
type_cast klass do
|
19
|
+
from 'nil', to: 'nil'
|
20
|
+
from klass.name, to: '%s'
|
21
|
+
otherwise casting
|
22
|
+
end
|
24
23
|
end
|
25
24
|
|
26
25
|
def remove_type_casting(klass)
|
@@ -30,6 +29,12 @@ module FastAttributes
|
|
30
29
|
def type_exists?(klass)
|
31
30
|
type_casting.has_key?(klass)
|
32
31
|
end
|
32
|
+
|
33
|
+
def type_cast(klass, &block)
|
34
|
+
type_cast = TypeCast.new
|
35
|
+
type_cast.instance_eval(&block)
|
36
|
+
type_casting[klass] = type_cast
|
37
|
+
end
|
33
38
|
end
|
34
39
|
|
35
40
|
def define_attributes(options = {}, &block)
|
@@ -43,7 +48,12 @@ module FastAttributes
|
|
43
48
|
builder.attribute *attributes, type
|
44
49
|
builder.compile!
|
45
50
|
end
|
46
|
-
end
|
47
51
|
|
48
|
-
|
49
|
-
|
52
|
+
set_type_casting String, 'String(%s)'
|
53
|
+
set_type_casting Integer, 'Integer(%s)'
|
54
|
+
set_type_casting Float, 'Float(%s)'
|
55
|
+
set_type_casting Array, 'Array(%s)'
|
56
|
+
set_type_casting Date, 'Date.parse(%s)'
|
57
|
+
set_type_casting Time, 'Time.parse(%s)'
|
58
|
+
set_type_casting DateTime, 'DateTime.parse(%s)'
|
59
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FastAttributes::TypeCast do
|
4
|
+
describe '#template' do
|
5
|
+
let(:type_cast) { FastAttributes::TypeCast.new }
|
6
|
+
|
7
|
+
describe 'without any conditions' do
|
8
|
+
it 'return exception' do
|
9
|
+
expect(type_cast.template.gsub(' ', '')).to eq <<-EOS.gsub(' ', '').chomp
|
10
|
+
raise UnknownTypeCastingError, 'Type casting is not defined'
|
11
|
+
EOS
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'when one rule is defined' do
|
16
|
+
before do
|
17
|
+
type_cast.from 'nil', to: 'nil'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns one when statement' do
|
21
|
+
expect(type_cast.template.gsub(' ', '')).to eq <<-EOS.gsub(' ', '').chomp
|
22
|
+
case %s
|
23
|
+
when nil
|
24
|
+
nil
|
25
|
+
else
|
26
|
+
raise UnknownTypeCastingError, 'Type casting is not defined'
|
27
|
+
end
|
28
|
+
EOS
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'when three rules are defined' do
|
33
|
+
before do
|
34
|
+
type_cast.from 'String', to: 'String(%s)'
|
35
|
+
type_cast.from 'Array', to: 'Array(%s)'
|
36
|
+
type_cast.from 'Integer', to: 'Integer(%s)'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns three when statements' do
|
40
|
+
expect(type_cast.template.gsub(' ', '')).to eq <<-EOS.gsub(' ', '').chomp
|
41
|
+
case %s
|
42
|
+
when String
|
43
|
+
String(%s)
|
44
|
+
when Array
|
45
|
+
Array(%s)
|
46
|
+
when Integer
|
47
|
+
Integer(%s)
|
48
|
+
else
|
49
|
+
raise UnknownTypeCastingError, 'Type casting is not defined'
|
50
|
+
end
|
51
|
+
EOS
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'when 2 rules and otherwise condition are defined' do
|
56
|
+
before do
|
57
|
+
type_cast.from 'Date', to: 'Date.parse(%s)'
|
58
|
+
type_cast.from 'Time', to: 'Time.parse(%s)'
|
59
|
+
type_cast.otherwise 'Float(%s)'
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'returns 2 when statements and overrides default else condition' do
|
63
|
+
expect(type_cast.template.gsub(' ', '')).to eq <<-EOS.gsub(' ', '').chomp
|
64
|
+
case %s
|
65
|
+
when Date
|
66
|
+
Date.parse(%s)
|
67
|
+
when Time
|
68
|
+
Time.parse(%s)
|
69
|
+
else
|
70
|
+
Float(%s)
|
71
|
+
end
|
72
|
+
EOS
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'when only otherwise rule is defined' do
|
77
|
+
before do
|
78
|
+
type_cast.otherwise '42 * %s'
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'returns otherwise statement only' do
|
82
|
+
expect(type_cast.template.gsub(' ', '')).to eq <<-EOS.gsub(' ', '').chomp
|
83
|
+
42 * %s
|
84
|
+
EOS
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -3,24 +3,16 @@ require 'spec_helper'
|
|
3
3
|
describe FastAttributes do
|
4
4
|
describe '.type_casting' do
|
5
5
|
it 'returns predefined type casting rules' do
|
6
|
-
expect(FastAttributes.type_casting).to
|
7
|
-
String
|
8
|
-
|
9
|
-
Float => 'Float(%s)',
|
10
|
-
Array => 'Array(%s)',
|
11
|
-
Date => 'Date.parse(%s)',
|
12
|
-
Time => 'Time.parse(%s)',
|
13
|
-
DateTime => 'DateTime.parse(%s)'
|
14
|
-
})
|
6
|
+
expect(FastAttributes.type_casting.keys).to match_array([
|
7
|
+
String, Integer, Float, Array, Date, Time, DateTime
|
8
|
+
])
|
15
9
|
end
|
16
10
|
end
|
17
11
|
|
18
12
|
describe '.get_type_casting' do
|
19
|
-
before { FastAttributes.send(:remove_instance_variable, :@type_casting) }
|
20
|
-
|
21
13
|
it 'returns type casting function' do
|
22
|
-
expect(FastAttributes.get_type_casting(String)).to
|
23
|
-
expect(FastAttributes.get_type_casting(Time)).to
|
14
|
+
expect(FastAttributes.get_type_casting(String)).to be_a(FastAttributes::TypeCast)
|
15
|
+
expect(FastAttributes.get_type_casting(Time)).to be_a(FastAttributes::TypeCast)
|
24
16
|
end
|
25
17
|
end
|
26
18
|
|
@@ -30,8 +22,9 @@ describe FastAttributes do
|
|
30
22
|
end
|
31
23
|
|
32
24
|
it 'adds type to supported type casting list' do
|
25
|
+
expect(FastAttributes.get_type_casting(OpenStruct)).to be(nil)
|
33
26
|
FastAttributes.set_type_casting(OpenStruct, 'OpenStruct.new(a: %s)')
|
34
|
-
expect(FastAttributes.get_type_casting(OpenStruct)).to
|
27
|
+
expect(FastAttributes.get_type_casting(OpenStruct)).to be_a(FastAttributes::TypeCast)
|
35
28
|
end
|
36
29
|
end
|
37
30
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kostiantyn Stepaniuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -101,7 +101,9 @@ files:
|
|
101
101
|
- fast_attributes.gemspec
|
102
102
|
- lib/fast_attributes.rb
|
103
103
|
- lib/fast_attributes/builder.rb
|
104
|
+
- lib/fast_attributes/type_cast.rb
|
104
105
|
- lib/fast_attributes/version.rb
|
106
|
+
- spec/fast_attributes/type_cast_spec.rb
|
105
107
|
- spec/fast_attributes_spec.rb
|
106
108
|
- spec/fixtures/classes.rb
|
107
109
|
- spec/spec_helper.rb
|
@@ -120,9 +122,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
120
122
|
version: 1.9.2
|
121
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
124
|
requirements:
|
123
|
-
- - "
|
125
|
+
- - ">"
|
124
126
|
- !ruby/object:Gem::Version
|
125
|
-
version:
|
127
|
+
version: 1.3.1
|
126
128
|
requirements: []
|
127
129
|
rubyforge_project:
|
128
130
|
rubygems_version: 2.2.2
|
@@ -130,6 +132,7 @@ signing_key:
|
|
130
132
|
specification_version: 4
|
131
133
|
summary: Fast attributes with data types
|
132
134
|
test_files:
|
135
|
+
- spec/fast_attributes/type_cast_spec.rb
|
133
136
|
- spec/fast_attributes_spec.rb
|
134
137
|
- spec/fixtures/classes.rb
|
135
138
|
- spec/spec_helper.rb
|