rails_static_record 0.2.0 → 0.2.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: 412378e001a7abb62ed7fbc45b5296329448c4c4
4
- data.tar.gz: db1b336cbe5fa56763a742eb86221742bafddcbb
3
+ metadata.gz: 60240b0ebf3743533a9112b72f859519c8712c5b
4
+ data.tar.gz: b901732caddbbfa2459b70c911e351f5cafbf51a
5
5
  SHA512:
6
- metadata.gz: 9e5b8a56032eac3f0e265cac188d911c9ed8a65fe30d2181c1e5768b938e6e9c44f2529434d54b4b91922dfd982ec9a41ff12b67a6f7d50d30fee00d75e66cc1
7
- data.tar.gz: dda4ac2ef01c1e33bfcdf022be03a8abaed51d8a0bfb2e45bbf8a2553a38779759990dfc5254b1136a1bbd707f46ed05e9bd222ba3077745a6711b2eca3b998c
6
+ metadata.gz: 07dd56083cb89e6349bc722e3416e9fd328d98fbf08e3e758c41ece280b2e11bface8fc23e9a12c28a4f30c30415023ac2b279df2e9d1ebd1faf95e817f30ff4
7
+ data.tar.gz: 2b28a32ae1432a8db122899b79a3b145e47940d94cd0c192cbfcd80792f20ec4bae1db50fe7dad9927b4713db7b68761bc1daf8e3877b1ede036893164ff8ad3
data/README.md CHANGED
@@ -4,6 +4,23 @@ locally, is useful for models like categories to store data
4
4
  without have to access to database. Is based on ActiveRecord
5
5
  and it permits relations with native models.
6
6
 
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'static_record'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install static_record
22
+ ```
23
+
7
24
  ## Usage
8
25
  You can create your own static model creating a ruby file in
9
26
  _app/models_. Remember name your model according to the rails convention.
@@ -16,6 +33,15 @@ class Category < StaticRecord::Base
16
33
  end
17
34
  ```
18
35
 
36
+ Or if you have your model static data in a custom directory, you can select it.
37
+ ```ruby
38
+ # app/models/category.rb
39
+
40
+ class Category < StaticRecord::Base
41
+ file '/path/to/file.yml'
42
+ end
43
+ ```
44
+
19
45
  Also you have to create the model data in *_db/static_*, the name of the YAML
20
46
  file is by default the name of the table for model. In other words, if your
21
47
  model name is category, you'll have to name your data file as categories.yml.
@@ -56,25 +82,48 @@ Category.find 4, 1
56
82
 
57
83
  ```
58
84
 
59
- ## Installation
60
- Add this line to your application's Gemfile:
85
+ Use find_by or find_by! like ActionRecord.
61
86
 
62
87
  ```ruby
63
- gem 'static_record'
64
- ```
88
+ # Find by attribute without throwing an error.
89
+ Category.find_by name: 'Is not stored'
90
+ # => []
91
+
92
+ # Find by attribute without throwing an error.
93
+ Category.find_by! name: 'Is not stored'
94
+ # =>
95
+ # StaticRecord::RecordNotFound: Couldn't find categories like name as Is not
96
+ # stored
97
+
98
+ # Also you can use dynamic methods.
99
+ Category.find_by_name 'Is not stored'
100
+ # => []
65
101
 
66
- And then execute:
67
- ```bash
68
- $ bundle
69
102
  ```
70
103
 
71
- Or install it yourself as:
72
- ```bash
73
- $ gem install static_record
104
+ Use array methods it's possible too.
105
+ ```ruby
106
+ Category.all
107
+
108
+ Category.first
109
+
110
+ Category.second
111
+
112
+ Category.last
113
+
114
+ Category.count
115
+
116
+ # Etc...
117
+
74
118
  ```
75
119
 
76
120
  ## Contributing
77
- Contribution directions go here.
121
+ If you want to contribute with the project, you can fork this repo and create
122
+ a pull request with your features or fixes. The code is written using the ruby
123
+ style guide. Read this [article](https://github.com/bbatsov/ruby-style-guide)
124
+ before contribute to this project.
125
+
126
+ Also you can follow me on Twitter [@molayab_](http://twitter.com/molayab_).
78
127
 
79
128
  ## License
80
129
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  begin
2
3
  require 'bundler/setup'
3
4
  rescue LoadError
@@ -14,11 +15,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
15
  rdoc.rdoc_files.include('lib/**/*.rb')
15
16
  end
16
17
 
17
-
18
-
19
-
20
-
21
-
22
18
  require 'bundler/gem_tasks'
23
19
 
24
20
  require 'rake/testtask'
@@ -13,3 +13,7 @@ require 'static_record/active_record'
13
13
  # require 'static_model/comparable'
14
14
  require 'static_record/loader_file'
15
15
  require 'static_record/base'
16
+
17
+ if defined?(Rails) && Rails.root
18
+ StaticRecord::Base.file_path = File.join(Rails.root, 'db', 'static')
19
+ end
@@ -10,7 +10,7 @@ module StaticRecord
10
10
  attr_accessor :file_path
11
11
 
12
12
  def file_path
13
- @file_path ||= File.join(Rails.root, 'db', 'static')
13
+ @file_path ||= './db/static'
14
14
  end
15
15
 
16
16
  def file(path)
@@ -32,6 +32,18 @@ module StaticRecord
32
32
  context
33
33
  end
34
34
 
35
+ def find_by!(hash)
36
+ context = find_by hash
37
+ key, value = hash.first
38
+
39
+ raise RecordNotFound.new(
40
+ "Couldn't find #{to_s.tableize} like #{key} as #{value}",
41
+ to_s.tableize,
42
+ 'id'
43
+ ) if context.empty?
44
+ context
45
+ end
46
+
35
47
  private
36
48
 
37
49
  def records
@@ -1,3 +1,3 @@
1
1
  module StaticRecord
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,3 +1,3 @@
1
1
  class Category < StaticRecord::Base
2
- file '/Users/molayab/Developer/CrowdProject/db/static/categories.yml'
2
+ file "#{Rails.root}/db/static/categories.yml"
3
3
  end
@@ -3,7 +3,7 @@ require_relative 'boot'
3
3
  require 'rails/all'
4
4
 
5
5
  Bundler.require(*Rails.groups)
6
- require "static_record"
6
+ require "rails_static_record"
7
7
 
8
8
  module Dummy
9
9
  class Application < Rails::Application
@@ -12,4 +12,3 @@ module Dummy
12
12
  # -- all .rb files in that directory are automatically loaded.
13
13
  end
14
14
  end
15
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_static_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateo Olaya Bernal
@@ -72,7 +72,7 @@ files:
72
72
  - MIT-LICENSE
73
73
  - README.md
74
74
  - Rakefile
75
- - lib/static_record.rb
75
+ - lib/rails_static_record.rb
76
76
  - lib/static_record/active_record.rb
77
77
  - lib/static_record/base.rb
78
78
  - lib/static_record/errors.rb
@@ -156,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
156
  requirements:
157
157
  - - ">="
158
158
  - !ruby/object:Gem::Version
159
- version: '0'
159
+ version: 2.1.0
160
160
  required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  requirements:
162
162
  - - ">="