ruson 1.3.2 → 1.3.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5beb4ec0c137e8258f492afe125896967a3e1d13f7d35d1acc8590c9e9791b1b
4
- data.tar.gz: 7a74b9b661870665d00630253ca8b860e809ea14a09c67d2a0d66c084ce48e1a
3
+ metadata.gz: c99a43791beb73f9ba6b3e9a882e5acdc30983ee39a0a9524f4cb6b897bb3fb9
4
+ data.tar.gz: ca5e3bf725ac122a55472e1b3a9cc2f7536676f0ee4938329161159917997100
5
5
  SHA512:
6
- metadata.gz: 79c4580249dfa2c701ff28c4e81dd108949742007fc52600e600f639ddac2314cb4b2cb3ea22ebe18eb5b1e4257d2311fcdcce4597555e538235ec9262474ece
7
- data.tar.gz: 7b98c33a2d5f2bb373fa9a62a567cd4989a13b7a330c357c3e794b9b187355ec79fe39f3d7c95d33abae2487cc2b5aeb3b07d8b521a033325020e770bb89230f
6
+ metadata.gz: 6a36c12e060045810642d6bc35943b3a1f9f7e59e7f6209ff20b3350ef4d8758c2f3f0f0d5e1a3f30912a089206625f057159168c0eed05f6bd1d29bb468b19e
7
+ data.tar.gz: 304a9fbcf72f4a8ad53b70979eb93b27f1cecf97c8897903d739971ccfe9333293026ab26966bf83e4b10d849778da646a03d996c4cc10917495a0330d591ffa
data/README.md CHANGED
@@ -123,11 +123,11 @@ post.picture.url #=> 'http://sample.com/picture.png'
123
123
 
124
124
  ###### Primary classes
125
125
 
126
- * Array
127
- * Boolean
128
- * Float
129
- * Integer
130
- * Time
126
+ * Ruson::Array
127
+ * Ruson::Boolean
128
+ * Ruson::Float
129
+ * Ruson::Integer
130
+ * Ruson::Time
131
131
 
132
132
 
133
133
  post.json
@@ -145,11 +145,11 @@ post.json
145
145
  ```ruby
146
146
  class Post < Ruson::Base
147
147
  field :title
148
- field :items, class: Array
149
- field :is_new, class: Boolean
150
- field :rate, class: Float
151
- field :view, class: Integer
152
- field :expired_at, class: Time
148
+ field :items, class: Ruson::Array
149
+ field :is_new, class: Ruson::Boolean
150
+ field :rate, class: Ruson::Float
151
+ field :view, class: Ruson::Integer
152
+ field :expired_at, class: Ruson::Time
153
153
  end
154
154
 
155
155
  json = File.read('post.json')
@@ -362,6 +362,35 @@ User.first #=> nil
362
362
  User.first! #=> raises Ruson::RecordNotFound
363
363
  ```
364
364
 
365
+ #### Find a record by attributes
366
+
367
+ post.json
368
+
369
+ ```json
370
+ {
371
+ "title": "Ruson",
372
+ "content": "Welcome!"
373
+ }
374
+ ```
375
+
376
+ ```ruby
377
+ Post.create(File.read('post.json'))
378
+ ```
379
+
380
+ ```ruby
381
+ Post.where(title: 'Ruson')
382
+ #=> [#<Post:0x000055bb2e907b78 @title="Ruson", @content="Welcome!", @id=1>]
383
+
384
+ Post.where(content: 'Wel')
385
+ #=> []
386
+
387
+ Post.where(content: 'Welcome!')
388
+ #=> [#<Post:0x000055bb2e907b78 @title="Ruson", @content="Welcome!", @id=1>]
389
+
390
+ Post.where(title: 'Ruson', content: 'Welcome!')
391
+ #=> [#<Post:0x000055bb2e907b78 @title="Ruson", @content="Welcome!", @id=1>]
392
+ ```
393
+
365
394
  ## Development
366
395
 
367
396
  ### Without Docker
@@ -388,7 +417,7 @@ rake install:local # Build and install ruson-1.2.0.gem into system gems witho
388
417
  rake release[remote] # Create tag v1.2.0 and build and push ruson-1.2.0.gem to rubygems.org
389
418
  rake spec # Run RSpec code examples
390
419
  ```
391
- _`--rm` means delete the container after the command ended._
420
+ _`--rm` means delete the container after the command has ended._
392
421
 
393
422
  In order to execute the tests:
394
423
 
@@ -6,5 +6,12 @@ module Ruson
6
6
 
7
7
  (json.class == Hash ? json : JSON.parse(json)).with_indifferent_access
8
8
  end
9
+
10
+ #
11
+ # Returns the ID from the JSON file path.
12
+ #
13
+ def id_from_file_path(path)
14
+ File.basename(path, '.json').to_i
15
+ end
9
16
  end
10
17
  end
@@ -2,6 +2,7 @@ module Ruson
2
2
  module Persistence
3
3
  def self.included(klass)
4
4
  klass.extend(ClassMethods)
5
+ klass.extend(Ruson::Json)
5
6
  end
6
7
 
7
8
  module ClassMethods
@@ -38,8 +39,8 @@ module Ruson
38
39
 
39
40
  id = 0
40
41
 
41
- Dir.glob(File.join(self.class.model_base_path, '*.json')).each do |file|
42
- file_id = File.basename(file, '.json').to_i
42
+ Dir.glob(File.join(self.class.model_base_path, '*.json')).each do |path|
43
+ file_id = id_from_file_path(path)
43
44
 
44
45
  id = file_id if file_id > id
45
46
  end
@@ -2,6 +2,7 @@ module Ruson
2
2
  module Querying
3
3
  def self.included(klass)
4
4
  klass.extend(ClassMethods)
5
+ klass.extend(Ruson::Json)
5
6
  end
6
7
 
7
8
  module ClassMethods
@@ -26,11 +27,11 @@ module Ruson
26
27
  def first
27
28
  ensure_output_folder_is_defined
28
29
 
29
- file_path = Dir.glob(File.join(model_base_path, '*.json')).first
30
+ file_path = model_files.first
30
31
 
31
32
  return unless file_path
32
33
 
33
- id = File.basename(file_path, '.json').to_i
34
+ id = id_from_file_path(file_path)
34
35
 
35
36
  load(file_path, id: id)
36
37
  end
@@ -50,6 +51,30 @@ module Ruson
50
51
 
51
52
  new json
52
53
  end
54
+
55
+ def where(attributes)
56
+ ensure_output_folder_is_defined
57
+
58
+ query_attributes = attributes.stringify_keys
59
+
60
+ models = model_files.collect do |path|
61
+ json = JSON.parse(File.read(path))
62
+
63
+ query_attributes_matches = query_attributes.keys.all? do |key|
64
+ json[key] == query_attributes[key]
65
+ end
66
+
67
+ if query_attributes_matches
68
+ new(json.merge(id: id_from_file_path(path)))
69
+ end
70
+ end.compact
71
+
72
+ Array(models)
73
+ end
74
+
75
+ def model_files
76
+ Dir.glob(File.join(model_base_path, '*.json'))
77
+ end
53
78
  end
54
79
  end
55
80
  end
@@ -1,3 +1,3 @@
1
1
  module Ruson
2
- VERSION = '1.3.2'
2
+ VERSION = '1.3.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruson
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - klriutsa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-07 00:00:00.000000000 Z
11
+ date: 2020-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport