rasti-db 0.4.0 → 0.4.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: 935cee1239288342a27bdcde6660f205b9fa1685
4
- data.tar.gz: 2bc7b5c0cf6559c7ae5716b4814fea412e0481f3
3
+ metadata.gz: 25e0bbb3407cfae9d3217b22dff5f9756f451d2c
4
+ data.tar.gz: 8e92b0f310f1a7761e62b288cf3482b788f6c89c
5
5
  SHA512:
6
- metadata.gz: b564fb1e883999d26e06b73c81d5ef68a68c2cdebc870403376c7c3b49e4729c020b552a010ffa47c13ca24743dfe9327b12963000a8892c55b3a4d0477f9170
7
- data.tar.gz: 1f68b62bc955b2d7a4955a217877d9013c2ab7546740a0b90c452952e5cc241fa2573a7c1766da724e22013dfa176894c4ec81aa2da20bf322f0b84598f2ff63
6
+ metadata.gz: 6ce81a8a43cba1212c251d86a7cd32293c649059b0b1674bfb83fbc845cd4f9376ca2cd5f89046eecf26b69c63ce624f255a9f62f67511afca7e86d308eea985
7
+ data.tar.gz: f61e038f876cb4064fb75b61d2141eda0d013d17841d6b6b1e3933564e147c5a75d35df0f9595e42a7adebd0456e8b8ae7280f07cb1860fd58aa67e9f44a0a03
data/.travis.yml CHANGED
@@ -7,6 +7,7 @@ rvm:
7
7
  - 2.2
8
8
  - 2.3.0
9
9
  - 2.4.0
10
+ - 2.5.0
10
11
  - jruby-1.7.25
11
12
  - jruby-9.1.7.0
12
13
  - ruby-head
data/README.md CHANGED
@@ -64,15 +64,24 @@ DB.create_table :categories_posts do
64
64
  foreign_key :post_id, :posts, null: false, index: true
65
65
  primary_key [:category_id, :post_id]
66
66
  end
67
+
68
+ DB.create_table :people do
69
+ String :document_number, null: false, primary_key: true
70
+ String :first_name, null: false
71
+ String :last_name, null: false
72
+ Date :birth_date, null: false
73
+ foreign_key :user_id, :users, null: false, unique: true
74
+ end
67
75
  ```
68
76
 
69
77
  ### Models
70
78
 
71
79
  ```ruby
72
- User = Rasti::DB::Model[:id, :name, :posts, :comments]
80
+ User = Rasti::DB::Model[:id, :name, :posts, :comments, :person]
73
81
  Post = Rasti::DB::Model[:id, :title, :body, :user_id, :user, :comments, :categories]
74
82
  Comment = Rasti::DB::Model[:id, :text, :user_id, :user, :post_id, :post]
75
83
  Category = Rasti::DB::Model[:id, :name, :posts]
84
+ Person = Rasti::DB::Model[:document_number, :first_name, :last_name, :birth_date, :user_id, :user]
76
85
  ```
77
86
 
78
87
  ### Collections
@@ -81,6 +90,7 @@ Category = Rasti::DB::Model[:id, :name, :posts]
81
90
  class Users < Rasti::DB::Collection
82
91
  one_to_many :posts
83
92
  one_to_many :comments
93
+ one_to_one :person
84
94
  end
85
95
 
86
96
  class Posts < Rasti::DB::Collection
@@ -113,10 +123,20 @@ class Categories < Rasti::DB::Collection
113
123
  many_to_many :posts
114
124
  end
115
125
 
126
+ class People < Rasti::DB::Collection
127
+ set_collection_name :people
128
+ set_primary_key :document_number
129
+ set_foreign_key :document_number
130
+ set_model Person
131
+
132
+ many_to_one :user
133
+ end
134
+
116
135
  users = Users.new DB
117
136
  posts = Posts.new DB
118
137
  comments = Comments.new DB
119
138
  categories = Categories.new DB
139
+ people = People.new DB
120
140
  ```
121
141
 
122
142
  ### Persistence
@@ -18,7 +18,7 @@ module Rasti
18
18
 
19
19
  def [](*attribute_names)
20
20
  Class.new(self) do
21
- attribute *attribute_names
21
+ attribute(*attribute_names)
22
22
 
23
23
  def self.inherited(subclass)
24
24
  subclass.instance_variable_set :@attributes, attributes.dup
@@ -82,15 +82,16 @@ module Rasti
82
82
  def to_h
83
83
  self.class.attributes.each_with_object({}) do |name, hash|
84
84
  if attributes.key? name
85
- case attributes[name]
85
+ value = fetch_attribute name
86
+ case value
86
87
  when Model
87
- hash[name] = attributes[name].to_h
88
+ hash[name] = value.to_h
88
89
  when Array
89
- hash[name] = attributes[name].map do |e|
90
+ hash[name] = value.map do |e|
90
91
  e.is_a?(Model) ? e.to_h : e
91
92
  end
92
93
  else
93
- hash[name] = attributes[name]
94
+ hash[name] = value
94
95
  end
95
96
  end
96
97
  end
@@ -101,7 +102,11 @@ module Rasti
101
102
  attr_reader :attributes
102
103
 
103
104
  def fetch_attribute(name)
104
- attributes.key?(name) ? attributes[name] : raise(UninitializedAttributeError, name)
105
+ attributes.key?(name) ? casted_attribute(name) : raise(UninitializedAttributeError, name)
106
+ end
107
+
108
+ def casted_attribute(name)
109
+ attributes[name].is_a?(Time) ? Timing::TimeInZone.new(attributes[name]) : attributes[name]
105
110
  end
106
111
 
107
112
  end
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module DB
3
- VERSION = '0.4.0'
3
+ VERSION = '0.4.1'
4
4
  end
5
5
  end
data/lib/rasti/db.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'sequel'
2
2
  require 'consty'
3
+ require 'time'
4
+ require 'timing'
3
5
 
4
6
  require_relative 'db/version'
5
7
  require_relative 'db/helpers'
data/rasti-db.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency 'sequel', '~> 5.0'
22
22
  spec.add_runtime_dependency 'consty', '~> 1.0', '>= 1.0.3'
23
+ spec.add_runtime_dependency 'timing', '~> 0.1', '>= 0.1.3'
23
24
 
24
25
  spec.add_development_dependency 'bundler', '~> 1.12'
25
26
  spec.add_development_dependency 'rake', '~> 11.0'
@@ -368,6 +368,11 @@ describe 'Collection' do
368
368
  it 'Graph' do
369
369
  1.upto(3) do |i|
370
370
  db[:users].insert name: "User #{i}"
371
+ db[:people].insert document_number: "document_#{i}",
372
+ first_name: "John #{i}",
373
+ last_name: "Doe #{i}",
374
+ birth_date: Time.now - i,
375
+ user_id: i
371
376
  db[:categories].insert name: "Category #{i}"
372
377
  db[:posts].insert user_id: i, title: "Post #{i}", body: '...'
373
378
  db[:categories_posts].insert post_id: i, category_id: i
@@ -379,11 +384,12 @@ describe 'Collection' do
379
384
  end
380
385
  end
381
386
 
382
- posts_graph = posts.where(id: 1).graph(:user, :categories, 'comments.user.posts.categories').all
387
+ posts_graph = posts.where(id: 1).graph('user.person', :categories, 'comments.user.posts.categories').all
383
388
 
384
389
  posts_graph.count.must_equal 1
385
390
 
386
- posts_graph[0].user.must_equal users.find(1)
391
+ posts_graph[0].user.id.must_equal 1
392
+ posts_graph[0].user.person.must_equal people.detect(user_id: 1)
387
393
 
388
394
  posts_graph[0].categories.must_equal [categories.find(1)]
389
395
 
data/spec/model_spec.rb CHANGED
@@ -28,6 +28,12 @@ describe 'Model' do
28
28
  proc { post.invalid_method }.must_raise NoMethodError
29
29
  end
30
30
 
31
+ it 'Time conversion' do
32
+ person = Person.new birth_date: Time.parse('2019-01-07T11:00:00-03:00')
33
+
34
+ person.birth_date.must_be_instance_of Timing::TimeInZone
35
+ end
36
+
31
37
  end
32
38
 
33
39
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-27 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -44,6 +44,26 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: 1.0.3
47
+ - !ruby/object:Gem::Dependency
48
+ name: timing
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.1'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.1.3
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '0.1'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.1.3
47
67
  - !ruby/object:Gem::Dependency
48
68
  name: bundler
49
69
  requirement: !ruby/object:Gem::Requirement