pod4 0.6.4 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pod4/typecasting.rb +75 -0
- data/lib/pod4/version.rb +1 -1
- data/md/fixme.md +0 -13
- data/spec_mri/model_plus_typecasting_spec.rb +82 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94046b48debbb6c1be2839e60a1b2a4519f0e711
|
4
|
+
data.tar.gz: 083cf3ff2f1d154d71c867878448bf9e855db67e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd8210e95713b24475f1415dd6c60b62e3d56478ab813bbe614a2e30cb8e2a46ed9a3f62865f336e158a16a46de8b6a3e8e8c0c01e68914b25536263e0163563
|
7
|
+
data.tar.gz: 48e38561b7ae5fd64c399c4ed9576be0f9f8f068bb3130cb829348472933f19862c4a70c951d7f19315886dc11cb50d48d65376909d3dbd1f12faa977a0ab10e
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'pod4/errors'
|
2
|
+
require 'pod4/metaxing'
|
3
|
+
|
4
|
+
|
5
|
+
module Pod4
|
6
|
+
|
7
|
+
|
8
|
+
##
|
9
|
+
# A mixin to give you some more options to control how Pod4 maps the
|
10
|
+
# interface to the model.
|
11
|
+
#
|
12
|
+
# Eventually we will actually have typecasting in here. For now all this
|
13
|
+
# allows you to do is enforce an encoding -- which will be of use if you are
|
14
|
+
# dealing with MSSQL, or with certain interfaces which appear to deal with
|
15
|
+
# the code page poorly:
|
16
|
+
#
|
17
|
+
# class FOo < Pod4::Model
|
18
|
+
# include Pod4::TypeCasting
|
19
|
+
#
|
20
|
+
# force_encoding Encoding::UTF-8
|
21
|
+
#
|
22
|
+
# ...
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
module TypeCasting
|
26
|
+
|
27
|
+
##
|
28
|
+
# A little bit of magic, for which I apologise.
|
29
|
+
#
|
30
|
+
# When you include this module it actually adds the methods in ClassMethods
|
31
|
+
# to the class as if you had called `extend TypeCasting:ClassMethds` *AND*
|
32
|
+
# adds the methods in InstanceMethods as if you had written `prepend
|
33
|
+
# TypeCasting::InstanceMethods`.
|
34
|
+
#
|
35
|
+
# In my defence: I didn't want to have to make you remember to do that...
|
36
|
+
#
|
37
|
+
def self.included(base)
|
38
|
+
base.extend ClassMethods
|
39
|
+
base.prepend InstanceMethods
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
module ClassMethods
|
44
|
+
include Metaxing
|
45
|
+
|
46
|
+
def force_encoding(enc)
|
47
|
+
raise Pod4Error, "Bad encoding" unless enc.kind_of? Encoding
|
48
|
+
define_class_method(:encoding){enc}
|
49
|
+
end
|
50
|
+
|
51
|
+
def encoding; nil; end
|
52
|
+
end
|
53
|
+
##
|
54
|
+
|
55
|
+
|
56
|
+
module InstanceMethods
|
57
|
+
|
58
|
+
def map_to_model(ot)
|
59
|
+
enc = self.class.encoding
|
60
|
+
|
61
|
+
ot.each do |_,v|
|
62
|
+
v.force_encoding(enc) if v.kind_of?(String) && enc
|
63
|
+
end
|
64
|
+
|
65
|
+
super(ot)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
##
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
end
|
75
|
+
|
data/lib/pod4/version.rb
CHANGED
data/md/fixme.md
CHANGED
@@ -4,13 +4,6 @@ Things I Wish I Could Do
|
|
4
4
|
* TinyTds gem fails to cast date fields as date. Nothing we can do about that,
|
5
5
|
AFAICS.
|
6
6
|
|
7
|
-
* PG gem raises lots of "please cast this type explicitly" warnings for money,
|
8
|
-
numeric types. There is no documentation for how to do this, and apparently
|
9
|
-
no-one knows how /O\
|
10
|
-
|
11
|
-
* Like an idiot I've made some tidying "raise an error" instance methods .. and
|
12
|
-
then called them from class methods. ::headdesk::
|
13
|
-
|
14
7
|
|
15
8
|
Things To Do
|
16
9
|
============
|
@@ -21,15 +14,9 @@ Things To Do
|
|
21
14
|
passing selection parameters to SequelInterface.list if the schema is set. We
|
22
15
|
need to tie down a test for that and fix it if it exists.
|
23
16
|
|
24
|
-
* PgInterface works pretty well for the PG gem, but not the pg_jruby gem. We
|
25
|
-
need to take a rather more paranoid approach to the thing; how we go about
|
26
|
-
adding test coverage for this I have literally no idea...
|
27
|
-
|
28
17
|
* TinyTDS just updated to 1.0 and ... fell over. We need to work out what's
|
29
18
|
going on there.
|
30
19
|
|
31
20
|
* Ideally interfaces should support parameterised insertion. Ideally in a
|
32
21
|
manner consistent for all interfaces...
|
33
22
|
|
34
|
-
* We should have a test suite for jRuby.
|
35
|
-
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'octothorpe'
|
2
|
+
|
3
|
+
require 'pod4'
|
4
|
+
require 'pod4/typecasting'
|
5
|
+
require 'pod4/null_interface'
|
6
|
+
|
7
|
+
|
8
|
+
class ProductModel < Pod4::Model
|
9
|
+
include Pod4::TypeCasting
|
10
|
+
force_encoding Encoding::ISO_8859_1 # I assume we are running as UTF8 here
|
11
|
+
attr_columns :id, :code, :product, :price
|
12
|
+
set_interface NullInterface.new(:id, :code, :product, :price, [])
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
describe 'ProductModel' do
|
18
|
+
|
19
|
+
let(:records) do
|
20
|
+
[ {id: 10, code: 'aa1', product: 'beans', price: 1.23},
|
21
|
+
{id: 20, code: 'bb1', product: 'pears', price: 2.34},
|
22
|
+
{id: 30, code: 'cc1', product: 'soap', price: 3.45},
|
23
|
+
{id: 40, code: 'cc2', product: 'matches', price: 4.56} ]
|
24
|
+
end
|
25
|
+
|
26
|
+
let(:model) { ProductModel.new(20) }
|
27
|
+
|
28
|
+
let(:model2) do
|
29
|
+
m = ProductModel.new(30)
|
30
|
+
|
31
|
+
allow( m.interface ).to receive(:read).
|
32
|
+
and_return( Octothorpe.new(records[2]) )
|
33
|
+
|
34
|
+
m.read.or_die
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:records_as_ot) { records.map{|r| Octothorpe.new(r) } }
|
38
|
+
|
39
|
+
|
40
|
+
###
|
41
|
+
|
42
|
+
|
43
|
+
describe 'Model.force_encoding' do
|
44
|
+
|
45
|
+
it 'requires an encoding' do
|
46
|
+
expect( ProductModel ).to respond_to(:force_encoding).with(1).argument
|
47
|
+
|
48
|
+
expect{ ProductModel.force_encoding('foo') }.to raise_exception Pod4Error
|
49
|
+
|
50
|
+
# Cheating here: this has to be the same as above or other tests will
|
51
|
+
# fail...
|
52
|
+
expect{ ProductModel.force_encoding(Encoding::ISO_8859_1) }.
|
53
|
+
not_to raise_exception
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'sets the encoding to be returned by Model.encoding' do
|
58
|
+
expect{ ProductModel.encoding }.not_to raise_exception
|
59
|
+
expect( ProductModel.encoding ).to eq(Encoding::ISO_8859_1)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
##
|
64
|
+
|
65
|
+
|
66
|
+
describe '#map_to_model' do
|
67
|
+
|
68
|
+
it 'forces each string to map to the given encoding' do
|
69
|
+
# map_to_model has already happened at this point. No matter.
|
70
|
+
ot = model2.to_ot
|
71
|
+
expect( ot.>>.id ).to eq 30
|
72
|
+
expect( ot.>>.price ).to eq 3.45
|
73
|
+
expect( ot.>>.code.encoding ).to eq Encoding::ISO_8859_1
|
74
|
+
expect( ot.>>.product.encoding ).to eq Encoding::ISO_8859_1
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
end
|
82
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pod4
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Jones
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: devnull
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/pod4/pg_interface.rb
|
72
72
|
- lib/pod4/sequel_interface.rb
|
73
73
|
- lib/pod4/tds_interface.rb
|
74
|
+
- lib/pod4/typecasting.rb
|
74
75
|
- lib/pod4/version.rb
|
75
76
|
- md/fixme.md
|
76
77
|
- md/roadmap.md
|
@@ -103,6 +104,7 @@ files:
|
|
103
104
|
- spec_mri/basic_model_spec.rb
|
104
105
|
- spec_mri/doc_no_pending.rb
|
105
106
|
- spec_mri/fixtures
|
107
|
+
- spec_mri/model_plus_typecasting_spec.rb
|
106
108
|
- spec_mri/model_spec.rb
|
107
109
|
- spec_mri/nebulous_interface_spec.rb
|
108
110
|
- spec_mri/null_interface_spec.rb
|