sruby 0.1.0 → 0.1.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
  SHA256:
3
- metadata.gz: 84dd5f9e3cb31dee2f5f1d68c6d931bf5fd6028d8a1b66d40cab5874e5a05a02
4
- data.tar.gz: 36b7a3af010ce03a3c292aff550f3eff2e48f3f57b21d2a7c979d9f42de2a629
3
+ metadata.gz: 7f0b3f7eae88703fdb9989e97072baf6da6a641c595bd4d4b8815e36f52f8ae1
4
+ data.tar.gz: 2df873b2c0ff2ebf65bd64cf622fbfec5e6d3b55b53ebed66427f837bebe21c9
5
5
  SHA512:
6
- metadata.gz: 87f9baa49c2be0c158a400c5e4ab0b1aeb9ccb426b0a9cb2a092c0e5b507fc19947b125af382f10e26fbb5967ca49cc99227c7ab28cab0c82ace5c89fdf2ee45
7
- data.tar.gz: e53c52b1b7c687dc19c2d4c9897ad555b99d1f0fb84010791d73bc97a4ea11736e310240c68d02ec0c7c7662b0deb3125f5a9d7c42dd4afaa08bb71f4a59b5a9
6
+ metadata.gz: f2f65eb0916343f1f1dcc21bc86ad9ff664c8917491b11fda1c252384d0c6030c7249857b2faf3fdcf262b999ed11bf95bea6b4b2ab8a5a332f5a1ea0d08d646
7
+ data.tar.gz: 4f7bb3f11866160e7da8c49450905162961db2c1e244a4ba0dd6844098a482f6d8189a6c30b1a0f8fb7824b7c827f2af7be25ae11031b796f221420755daddfd
data/.idea/.gitignore CHANGED
@@ -1,8 +1,8 @@
1
- # Default ignored files
2
- /shelf/
3
- /workspace.xml
4
- # Editor-based HTTP Client requests
5
- /httpRequests/
6
- # Datasource local storage ignored files
7
- /dataSources/
8
- /dataSources.local.xml
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
data/.idea/misc.xml CHANGED
@@ -1,4 +1,4 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectRootManager" version="2" project-jdk-name="ruby-3.0.3-p157" project-jdk-type="RUBY_SDK" />
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-3.0.3-p157" project-jdk-type="RUBY_SDK" />
4
4
  </project>
data/.idea/modules.xml CHANGED
@@ -1,8 +1,8 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/sruby.iml" filepath="$PROJECT_DIR$/.idea/sruby.iml" />
6
- </modules>
7
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/sruby.iml" filepath="$PROJECT_DIR$/.idea/sruby.iml" />
6
+ </modules>
7
+ </component>
8
8
  </project>
data/.idea/sruby.iml CHANGED
@@ -1,17 +1,57 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="RUBY_MODULE" version="4">
3
- <component name="ModuleRunConfigurationManager">
4
- <shared />
5
- </component>
6
- <component name="NewModuleRootManager">
7
- <content url="file://$MODULE_DIR$">
8
- <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
- <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
- <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
- </content>
12
- <orderEntry type="inheritedJdk" />
13
- <orderEntry type="sourceFolder" forTests="false" />
14
- <orderEntry type="library" scope="PROVIDED" name="bundler (v2.3.4, ruby-3.0.3-p157) [gem]" level="application" />
15
- <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.6, ruby-3.0.3-p157) [gem]" level="application" />
16
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="jdk" jdkName="RVM: ruby-3.0.0" jdkType="RUBY_SDK" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.3.4, RVM: ruby-3.0.0) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.5.0, RVM: ruby-3.0.0) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.6, RVM: ruby-3.0.0) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.11.0, RVM: ruby-3.0.0) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.11.0, RVM: ruby-3.0.0) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.11.0, RVM: ruby-3.0.0) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.11.1, RVM: ruby-3.0.0) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.11.0, RVM: ruby-3.0.0) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="sqlite (v1.0.2, RVM: ruby-3.0.0) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="sqlite3 (v1.4.4, RVM: ruby-3.0.0) [gem]" level="application" />
24
+ </component>
25
+ <component name="RakeTasksCache">
26
+ <option name="myRootTask">
27
+ <RakeTaskImpl id="rake">
28
+ <subtasks>
29
+ <RakeTaskImpl description="Build sruby-0.1.1.gem into the pkg directory" fullCommand="build" id="build" />
30
+ <RakeTaskImpl id="build">
31
+ <subtasks>
32
+ <RakeTaskImpl description="Generate SHA512 checksum if sruby-0.1.1.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
33
+ </subtasks>
34
+ </RakeTaskImpl>
35
+ <RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
36
+ <RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
37
+ <RakeTaskImpl description="Build and install sruby-0.1.1.gem into system gems" fullCommand="install" id="install" />
38
+ <RakeTaskImpl id="install">
39
+ <subtasks>
40
+ <RakeTaskImpl description="Build and install sruby-0.1.1.gem into system gems without network access" fullCommand="install:local" id="local" />
41
+ </subtasks>
42
+ </RakeTaskImpl>
43
+ <RakeTaskImpl description="Create tag v0.1.1 and build and push sruby-0.1.1.gem to https://rubygems.org" fullCommand="release[remote]" id="release[remote]" />
44
+ <RakeTaskImpl description="" fullCommand="default" id="default" />
45
+ <RakeTaskImpl description="" fullCommand="release" id="release" />
46
+ <RakeTaskImpl id="release">
47
+ <subtasks>
48
+ <RakeTaskImpl description="" fullCommand="release:guard_clean" id="guard_clean" />
49
+ <RakeTaskImpl description="" fullCommand="release:rubygem_push" id="rubygem_push" />
50
+ <RakeTaskImpl description="" fullCommand="release:source_control_push" id="source_control_push" />
51
+ </subtasks>
52
+ </RakeTaskImpl>
53
+ </subtasks>
54
+ </RakeTaskImpl>
55
+ </option>
56
+ </component>
17
57
  </module>
data/.idea/vcs.xml CHANGED
@@ -1,6 +1,6 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
6
  </project>
data/.rubocop.yml CHANGED
@@ -1,44 +1,44 @@
1
- AllCops:
2
- TargetRubyVersion: 2.5
3
-
4
- Style/StringLiterals:
5
- Enabled: true
6
- EnforcedStyle: double_quotes
7
-
8
- Style/StringLiteralsInInterpolation:
9
- Enabled: true
10
- EnforcedStyle: double_quotes
11
-
12
- Layout/LineLength:
13
- Max: 200
14
-
15
- Style/HashSyntax:
16
- Enabled: true
17
- EnforcedStyle: hash_rockets
18
-
19
- Layout/EndOfLine:
20
- Enabled: false
21
-
22
- Metrics/ModuleLength:
23
- Enabled: false
24
-
25
- Metrics/MethodLength:
26
- Enabled: false
27
-
28
- Lint/RedundantStringCoercion:
29
- Enabled: false
30
-
31
- Style/Documentation:
32
- Enabled: false
33
-
34
- Metrics/PerceivedComplexity:
35
- Enabled: false
36
-
37
- Metrics/AbcSize:
38
- Enabled: false
39
-
40
- Metrics/CyclomaticComplexity:
41
- Enabled: false
42
-
43
- Metrics/BlockLength:
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 200
14
+
15
+ Style/HashSyntax:
16
+ Enabled: true
17
+ EnforcedStyle: hash_rockets
18
+
19
+ Layout/EndOfLine:
20
+ Enabled: false
21
+
22
+ Metrics/ModuleLength:
23
+ Enabled: false
24
+
25
+ Metrics/MethodLength:
26
+ Enabled: false
27
+
28
+ Lint/RedundantStringCoercion:
29
+ Enabled: false
30
+
31
+ Style/Documentation:
32
+ Enabled: false
33
+
34
+ Metrics/PerceivedComplexity:
35
+ Enabled: false
36
+
37
+ Metrics/AbcSize:
38
+ Enabled: false
39
+
40
+ Metrics/CyclomaticComplexity:
41
+ Enabled: false
42
+
43
+ Metrics/BlockLength:
44
44
  Enabled: false
data/Gemfile CHANGED
@@ -1,10 +1,12 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in sruby.gemspec
6
- gemspec
7
-
8
- gem "rake", "~> 13.0"
9
-
10
- gem "sqlite", "~> 1.0", ">= 1.0.2"
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in sruby.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "sqlite", "~> 1.0", ">= 1.0.2"
data/Gemfile.lock CHANGED
@@ -1,20 +1,37 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sruby (0.1.0)
4
+ sruby (0.1.1)
5
5
  sqlite3
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ diff-lcs (1.5.0)
10
11
  rake (13.0.6)
11
- sqlite3 (1.4.2)
12
+ rspec (3.11.0)
13
+ rspec-core (~> 3.11.0)
14
+ rspec-expectations (~> 3.11.0)
15
+ rspec-mocks (~> 3.11.0)
16
+ rspec-core (3.11.0)
17
+ rspec-support (~> 3.11.0)
18
+ rspec-expectations (3.11.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.11.0)
21
+ rspec-mocks (3.11.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-support (3.11.0)
25
+ sqlite (1.0.2)
26
+ sqlite3 (1.4.4)
12
27
 
13
28
  PLATFORMS
14
29
  x86_64-linux
15
30
 
16
31
  DEPENDENCIES
17
32
  rake (~> 13.0)
33
+ rspec (~> 3.0)
34
+ sqlite (~> 1.0, >= 1.0.2)
18
35
  sruby!
19
36
 
20
37
  BUNDLED WITH
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Les Laboratoires Ruby](https://invidget.switchblade.xyz/4P7XcmbDnt)](https://discord.gg/4P7XcmbDnt)
1
2
  # Sruby
2
3
 
3
4
  Sruby is a Ruby Library for easy SQLite interactions. It is a wrapper around the SQLite3 Ruby library.
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- task :default => %i[]
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task :default => %i[]
data/lib/sruby/errors.rb CHANGED
@@ -1,9 +1,9 @@
1
- # frozen_string_literal: true
2
-
3
- module Sruby
4
- class SrubyError < StandardError
5
- def initialize(msg = String.new)
6
- super("SRUBY ERROR : #{msg}")
7
- end
8
- end
9
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Sruby
4
+ class SrubyError < StandardError
5
+ def initialize(msg = String.new)
6
+ super("SRUBY ERROR : #{msg}")
7
+ end
8
+ end
9
+ end
data/lib/sruby/index.rb CHANGED
@@ -1,186 +1,194 @@
1
- # frozen_string_literal: true
2
-
3
- require "sqlite3"
4
- require File.expand_path("mixin.rb", __dir__)
5
- require File.expand_path("errors.rb", __dir__)
6
-
7
- module Sruby
8
- class Database
9
- attr_reader :db
10
-
11
- # Creates a new database object
12
- # @param [String, Hash, Sqlite3::Database] options The database file to open, or a hash of options
13
- def initialize(options = Hash[:name => "sruby.db"])
14
- case options
15
- when String
16
- @db = SQLite3::Database.new(options)
17
- when Hash
18
- @db = SQLite3::Database.new(options[:name])
19
- when SQLite3::Database
20
- @db = options
21
- else
22
- raise SrubyError, "Invalid argument: #{options.inspect}"
23
- end
24
- @db.results_as_hash = true if options.is_a?(Hash) && options[:results_as_hash]
25
- end
26
-
27
- # Creates a new Table in database
28
- # @param [String] table_name The name of the table
29
- # @example
30
- # db = Sruby::Database.new
31
- # db.create_table("users")
32
- # @return [Table] The database object
33
- def create(table_name)
34
- @db.execute <<~SQL
35
- CREATE TABLE IF NOT EXISTS #{table_name} (
36
- name TEXT PRIMARY KEY,
37
- value TEXT
38
- );
39
- SQL
40
-
41
- Database.attr_accessor table_name
42
- instance_variable_set("@#{table_name}", Table.new(@db, table_name))
43
- instance_variable_get("@#{table_name}")
44
- end
45
-
46
- # Insert values into a table
47
- # @param [String] table_name The name of the table
48
- # @param [String, Hash] values The values to insert
49
- # @example
50
- # db = Sruby::Database.new
51
- # db.create_table("users")
52
- # db.insert("users", "person" => "John", "age" => "24")
53
- # db.insert("users", Hash["person" => "John", "age" => "24"])
54
- # db.insert("users", "person", "John")
55
- # @return [Array] The database object
56
- def insert(table_name, *values)
57
- case values[0]
58
- when Hash
59
- values[0].stringify_keys!
60
- values_as_paths = values[0].paths
61
- values_as_paths.each do |path, value|
62
- p path, value
63
- @db.execute("INSERT INTO #{table_name} (name, value) VALUES (?, ?)", path.join("."), value)
64
- end
65
- else
66
- @db.execute("INSERT INTO #{table_name} (name, value) VALUES (?, ?)", values[0].to_s, values[1])
67
- end
68
- rescue SQLite3::ConstraintException
69
- raise SrubyError, "Duplicated key"
70
- end
71
-
72
- # Updates values in a table
73
- # @param [String] table_name The name of the table
74
- # @param [String, Hash] values The values to update
75
- # @example
76
- # db = Sruby::Database.new
77
- # db.create_table("users")
78
- # db.insert("users", "person" => "John", "age" => "24")
79
- # db.update("users", "person" => "John", "age" => "25")
80
- # db.update("users", Hash["person" => "John", "age" => "25"])
81
- # @return [Array] The database object
82
- def update(table_name, *values)
83
- case values[0]
84
- when Hash
85
- values[0].stringify_keys!
86
- values_as_paths = values[0].paths
87
- values_as_paths.each do |path, value|
88
- p path, value
89
- @db.execute("REPLACE INTO #{table_name} (name, value) VALUES (?, ?)", path.join("."), value)
90
- end
91
- else
92
- @db.execute("REPLACE INTO #{table_name} (name, value) VALUES (?, ?)", values[0].to_s, values[1])
93
- end
94
- end
95
-
96
- # Get a value from a table
97
- # @param [String] table_name The name of the table
98
- # @param [String] name The value to get
99
- # @param [Nil, String] path The path to get
100
- # @example
101
- # db = Sruby::Database.new
102
- # db.create_table("users")
103
- # db.insert("users", "person" => "John", "age" => "24")
104
- # db.get("users", "person")
105
- # @return [String] The value
106
- def get(table_name, name, path = nil)
107
- if path.nil?
108
- @db.execute("SELECT value FROM #{table_name} WHERE name = ?", name)
109
- else
110
- path_name = case path
111
- when Array
112
- "#{path.map(&:to_s).join(".")}.#{name}"
113
- when String
114
- "#{path}.#{name}"
115
- else
116
- name
117
- end
118
- data = @db.execute("SELECT value FROM #{table_name} WHERE name = ?", path_name)
119
- raise SrubyError, "No value found for #{path_name}" if data.empty?
120
- data
121
- end
122
- end
123
-
124
- # Deletes a row from a table
125
- # @param [String] table_name The name of the table
126
- # @param [String] name The name of the row to delete
127
- # @param [Nil, String] path The path to delete
128
- # @example
129
- # db = Sruby::Database.new
130
- # db.create_table("users")
131
- # db.insert("users", "person" => "John", "age" => "24")
132
- # db.delete("users", "person")
133
- # @return [Array] The database object
134
- def delete(table_name, name, path = nil)
135
- if path.nil?
136
- @db.execute("DELETE FROM #{table_name} WHERE name = ?", name)
137
- else
138
- path_name = case path
139
- when Array
140
- "#{path.map(&:to_s).join(".")}.#{name}"
141
- when String
142
- "#{path}.#{name}"
143
- else
144
- name
145
- end
146
- @db.execute("DELETE FROM #{table_name} WHERE name = ?", path_name)
147
- end
148
- end
149
-
150
- # Get all the values from a table
151
- # @param [String] table_name The name of the table
152
- # @return [Array] The values
153
- def all(table_name)
154
- @db.execute("SELECT * FROM #{table_name}")
155
- end
156
- end
157
-
158
- class Table
159
- attr_reader :db, :name
160
-
161
- def initialize(db, name)
162
- @db = db
163
- @name = name
164
- end
165
-
166
- def insert(*values)
167
- Database.new(@db).insert(@name, *values)
168
- end
169
-
170
- def update(*values)
171
- Database.new(@db).update(@name, *values)
172
- end
173
-
174
- def get(name, path = nil)
175
- Database.new(@db).get(@name, name, path)
176
- end
177
-
178
- def delete(name, path = nil)
179
- Database.new(@db).delete(@name, name, path)
180
- end
181
-
182
- def all
183
- Database.new(@db).all(@name)
184
- end
185
- end
186
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "sqlite3"
4
+ require File.expand_path("mixin.rb", __dir__)
5
+ require File.expand_path("errors.rb", __dir__)
6
+
7
+ module Sruby
8
+ class Database
9
+ attr_reader :db, :tables
10
+
11
+ # Creates a new database object
12
+ # @param [String, Hash, Sqlite3::Database] options The database file to open, or a hash of options
13
+ def initialize(options = Hash[:name => "sruby.db"])
14
+ case options
15
+ when String
16
+ @db = SQLite3::Database.new(options)
17
+ when Hash
18
+ @db = SQLite3::Database.new(options[:name])
19
+ when SQLite3::Database
20
+ @db = options
21
+ else
22
+ raise SrubyError, "Invalid argument: #{options.inspect}"
23
+ end
24
+ @db.results_as_hash = true if options.is_a?(Hash) && options[:results_as_hash]
25
+ end
26
+
27
+ # Creates a new Table in database
28
+ # @param [String] table_name The name of the table
29
+ # @example
30
+ # db = Sruby::Database.new
31
+ # db.create_table("users")
32
+ # @return [Table] The database object
33
+ def create(table_name)
34
+ @db.execute <<~SQL
35
+ CREATE TABLE IF NOT EXISTS #{table_name} (
36
+ name TEXT PRIMARY KEY,
37
+ value TEXT
38
+ );
39
+ SQL
40
+
41
+ @tables ||= Hash.new
42
+ @tables[table_name] = Table.new(@db, table_name)
43
+ end
44
+
45
+ def [](table_name)
46
+ @tables ||= Hash.new
47
+ @tables[table_name.to_s]
48
+ end
49
+
50
+ def method_missing(symbol, *args)
51
+ return self[symbol] if self[symbol]
52
+ super
53
+ end
54
+
55
+ # Insert values into a table
56
+ # @param [String] table_name The name of the table
57
+ # @param [String, Hash] values The values to insert
58
+ # @example
59
+ # db = Sruby::Database.new
60
+ # db.create_table("users")
61
+ # db.insert("users", "person" => "John", "age" => "24")
62
+ # db.insert("users", Hash["person" => "John", "age" => "24"])
63
+ # db.insert("users", "person", "John")
64
+ # @return [Array] The database object
65
+ def insert(table_name, *values)
66
+ case values[0]
67
+ when Hash
68
+ values[0].stringify_keys!
69
+ values_as_paths = values[0].paths
70
+ values_as_paths.each do |path, value|
71
+ p path, value
72
+ @db.execute("INSERT INTO #{table_name} (name, value) VALUES (?, ?)", path.join("."), value)
73
+ end
74
+ else
75
+ @db.execute("INSERT INTO #{table_name} (name, value) VALUES (?, ?)", values[0].to_s, values[1])
76
+ end
77
+ rescue SQLite3::ConstraintException
78
+ raise SrubyError, "Duplicated key"
79
+ end
80
+
81
+ # Updates values in a table
82
+ # @param [String] table_name The name of the table
83
+ # @param [String, Hash] values The values to update
84
+ # @example
85
+ # db = Sruby::Database.new
86
+ # db.create_table("users")
87
+ # db.insert("users", "person" => "John", "age" => "24")
88
+ # db.update("users", "person" => "John", "age" => "25")
89
+ # db.update("users", Hash["person" => "John", "age" => "25"])
90
+ # @return [Array] The database object
91
+ def update(table_name, *values)
92
+ case values[0]
93
+ when Hash
94
+ values[0].stringify_keys!
95
+ values_as_paths = values[0].paths
96
+ values_as_paths.each do |path, value|
97
+ @db.execute("REPLACE INTO #{table_name} (name, value) VALUES (?, ?)", path.join("."), value)
98
+ end
99
+ else
100
+ @db.execute("REPLACE INTO #{table_name} (name, value) VALUES (?, ?)", values[0].to_s, values[1])
101
+ end
102
+ end
103
+
104
+ # Get a value from a table
105
+ # @param [String] table_name The name of the table
106
+ # @param [String] name The value to get
107
+ # @param [Nil, String] path The path to get
108
+ # @example
109
+ # db = Sruby::Database.new
110
+ # db.create_table("users")
111
+ # db.insert("users", "person" => "John", "age" => "24")
112
+ # db.get("users", "person")
113
+ # @return [String] The value
114
+ def get(table_name, name, path = nil)
115
+ if path.nil?
116
+ @db.execute("SELECT value FROM #{table_name} WHERE name = ?", name)
117
+ else
118
+ path_name = case path
119
+ when Array
120
+ "#{path.map(&:to_s).join(".")}.#{name}"
121
+ when String
122
+ "#{path}.#{name}"
123
+ else
124
+ name
125
+ end
126
+ data = @db.execute("SELECT value FROM #{table_name} WHERE name = ?", path_name)
127
+ raise SrubyError, "No value found for #{path_name}" if data.empty?
128
+ data
129
+ end
130
+ end
131
+
132
+ # Deletes a row from a table
133
+ # @param [String] table_name The name of the table
134
+ # @param [String] name The name of the row to delete
135
+ # @param [Nil, String] path The path to delete
136
+ # @example
137
+ # db = Sruby::Database.new
138
+ # db.create_table("users")
139
+ # db.insert("users", "person" => "John", "age" => "24")
140
+ # db.delete("users", "person")
141
+ # @return [Array] The database object
142
+ def delete(table_name, name, path = nil)
143
+ if path.nil?
144
+ @db.execute("DELETE FROM #{table_name} WHERE name = ?", name)
145
+ else
146
+ path_name = case path
147
+ when Array
148
+ "#{path.map(&:to_s).join(".")}.#{name}"
149
+ when String
150
+ "#{path}.#{name}"
151
+ else
152
+ name
153
+ end
154
+ @db.execute("DELETE FROM #{table_name} WHERE name = ?", path_name)
155
+ end
156
+ end
157
+
158
+ # Get all the values from a table
159
+ # @param [String] table_name The name of the table
160
+ # @return [Array] The values
161
+ def all(table_name)
162
+ @db.execute("SELECT * FROM #{table_name}")
163
+ end
164
+ end
165
+
166
+ class Table
167
+ attr_reader :db, :name
168
+
169
+ def initialize(db, name)
170
+ @db = db
171
+ @name = name
172
+ end
173
+
174
+ def insert(*values)
175
+ Database.new(@db).insert(@name, *values)
176
+ end
177
+
178
+ def update(*values)
179
+ Database.new(@db).update(@name, *values)
180
+ end
181
+
182
+ def get(name, path = nil)
183
+ Database.new(@db).get(@name, name, path)
184
+ end
185
+
186
+ def delete(name, path = nil)
187
+ Database.new(@db).delete(@name, name, path)
188
+ end
189
+
190
+ def all
191
+ Database.new(@db).all(@name)
192
+ end
193
+ end
194
+ end
data/lib/sruby/mixin.rb CHANGED
@@ -1,44 +1,44 @@
1
- # frozen_string_literal: true
2
-
3
- class Hash
4
- def stringify_keys!
5
- transform_keys!(&:to_s)
6
- end
7
-
8
- def stringify_values!
9
- transform_values!(&:to_s)
10
- end
11
-
12
- # Get the deepest keys
13
- # @param hash [Hash] the hash to get the deepest keys from
14
- # @return [Array<String>] the deepest keys
15
- def keys(hash = self)
16
- hash.each_with_object([]) do |(key, value), paths|
17
- if value.is_a?(Hash)
18
- paths.concat(keys(value))
19
- else
20
- paths << key
21
- end
22
- end
23
- end
24
-
25
- # Get the deep path of a certain key
26
- # @param key [String] the key to get the path for
27
- # @param path [Array] the current path
28
- # @return [NilClass, Array<String, String>] the path to the key
29
- def path_to(key, path = [], hash: self)
30
- hash.stringify_keys!
31
- hash.each_pair do |k, v|
32
- return [path + [k], v] if k == key.to_s
33
- if v.is_a?(Hash) &&
34
- (p = path_to(key.to_s, path + [k], :hash => v))
35
- return p
36
- end
37
- end
38
- nil
39
- end
40
-
41
- def paths(hash = self)
42
- keys(hash).map { |key| path_to(key, :hash => hash) }
43
- end
44
- end
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ def stringify_keys!
5
+ transform_keys!(&:to_s)
6
+ end
7
+
8
+ def stringify_values!
9
+ transform_values!(&:to_s)
10
+ end
11
+
12
+ # Get the deepest keys
13
+ # @param hash [Hash] the hash to get the deepest keys from
14
+ # @return [Array<String>] the deepest keys
15
+ def keys(hash = self)
16
+ hash.each_with_object([]) do |(key, value), paths|
17
+ if value.is_a?(Hash)
18
+ paths.concat(keys(value))
19
+ else
20
+ paths << key
21
+ end
22
+ end
23
+ end
24
+
25
+ # Get the deep path of a certain key
26
+ # @param key [String] the key to get the path for
27
+ # @param path [Array] the current path
28
+ # @return [NilClass, Array<String, String>] the path to the key
29
+ def path_to(key, path = [], hash: self)
30
+ hash.stringify_keys!
31
+ hash.each_pair do |k, v|
32
+ return [path + [k], v] if k == key.to_s
33
+ if v.is_a?(Hash) &&
34
+ (p = path_to(key.to_s, path + [k], :hash => v))
35
+ return p
36
+ end
37
+ end
38
+ nil
39
+ end
40
+
41
+ def paths(hash = self)
42
+ keys(hash).map { |key| path_to(key, :hash => hash) }
43
+ end
44
+ end
data/lib/sruby/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sruby
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/sruby.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "sruby/version"
3
+ require File.expand_path("sruby/index", File.dirname(__FILE__))
4
4
 
5
5
  module Sruby
6
6
  end
data/sig/sruby/index.rbs CHANGED
@@ -1,5 +1,5 @@
1
- module Sruby
2
- class Database
3
-
4
- end
1
+ module Sruby
2
+ class Database
3
+
4
+ end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Senchu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-26 00:00:00.000000000 Z
11
+ date: 2022-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -56,7 +56,6 @@ files:
56
56
  - lib/sruby/version.rb
57
57
  - sig/sruby.rbs
58
58
  - sig/sruby/index.rbs
59
- - tests/index.rb
60
59
  homepage: https://github.com/Senchuu/Sruby
61
60
  licenses:
62
61
  - MIT
@@ -65,7 +64,7 @@ metadata:
65
64
  homepage_uri: https://github.com/Senchuu/Sruby
66
65
  source_code_uri: https://github.com/Senchuu/Sruby
67
66
  changelog_uri: https://github.com/Senchuu/Sruby/blob/main/CHANGELOG.md
68
- post_install_message:
67
+ post_install_message:
69
68
  rdoc_options: []
70
69
  require_paths:
71
70
  - lib
@@ -80,8 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
79
  - !ruby/object:Gem::Version
81
80
  version: '0'
82
81
  requirements: []
83
- rubygems_version: 3.3.4
84
- signing_key:
82
+ rubygems_version: 3.2.3
83
+ signing_key:
85
84
  specification_version: 4
86
85
  summary: Create, read, update and delete data from SQLite database.
87
86
  test_files: []
data/tests/index.rb DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../lib/sruby/index"
4
-
5
- db = Sruby::Database.new
6
- test = db.create("test")
7
- db.test == test
8
- test.insert("one" => 1)
9
- test.insert("member" => Hash[:name=>"John", :age=>25])
10
- test.delete("one")
11
- test.all
12
- db.all("test")