staticmodel 1.0.0 → 1.0.2

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
  SHA1:
3
- metadata.gz: 89e840ef250a7502da3f788a2c211db15cd2d914
4
- data.tar.gz: a06c94ca8374d4ef8cd5b08aeba68f0b8e429a11
3
+ metadata.gz: dfc783f3c6a3bcef6e22dfb42a356936822dc3b6
4
+ data.tar.gz: d327a1ea6096c87b22ffab01133ff1988019d761
5
5
  SHA512:
6
- metadata.gz: abd7fd8933701a9edb5c67f602311e449afc623adc7e768ca5784fa5fc63df9a8218d35836ddbd0b1f227eddd43194887d15267d133e11051d83dbaa794bfeac
7
- data.tar.gz: 08e25d28cb2262d02ced4b8966ac8cd9b920bdc8fe844eca3f0fac3a6f8e3581ae68ee5f6d60b5982a930dee9a24bd122ea655b0fa3236c42455e76331f5c669
6
+ metadata.gz: 96bfbb74156e8db0a9163cef1b22c26c4107f7ee6d070561de6b90503e76cab5087dc8a221adcb796fbf2881c92b8bae1e3f45ebe7a5fe78f6ad5d455599ae44
7
+ data.tar.gz: 2f2cbf9eea84a2bb9ee77d4d4952ae6b45a65739103b20688ab8d5cd40242be138b8c8ab5fa9023fb4bf7364ef33ecf05958008fae7d5a8d645b39a137a3c69a
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # StaticModel
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/staticmodel`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Read-only ActiveRecord-like interface to query static YAML files.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,20 +20,235 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ StaticModel usage is pretty similar to ActiveRecord. Although the gem was designed with Rails in mind, it can be used in any Ruby project.
24
+
25
+ To start using StaticModel you need to:
26
+
27
+ * Inherit from `StaticModel::Base`.
28
+ * Define the source of your data.
29
+ * Define your attributes and/or your default values.
30
+
31
+
32
+ ### Inherit from `StaticModel::Base`
33
+
34
+ Just like with ActiveRecord, your models need to inherit from `StaticModel::Base`.
35
+
36
+ ```ruby
37
+ require "staticmodel"
38
+
39
+ class Country < StaticModel::Base
40
+ end
41
+ ```
42
+
43
+ #### Primary key
44
+
45
+ StaticModel assumes that each model can be identified by a primary key. A model instance’s primary key is always available as `model.id` whether you name it the default `id` or set it to something else.
46
+
47
+ It's possible to override the attribute that should be used as the model's primary key using the `primary_key=` method:
48
+
49
+ ```ruby
50
+ class Country < ActiveRecord::Base
51
+ self.primary_key = "code"
52
+
53
+ attribute :code, String
54
+ attribute :name, String
55
+ end
56
+
57
+ Country.all
58
+ # => [
59
+ # #<Country code="ES", name="Spain">,
60
+ # #<Country code="NL", name="Netherlands">
61
+ # ]
62
+
63
+ Country.primary_key # => "code"
64
+ country = Country.find("ES") # => #<Country code="ES", name="Spain">
65
+ country.id # => "ES"
66
+ contry.name # => "Spain"
67
+ ```
68
+
69
+ Or you can also override the `primary_key` method yourself:
70
+
71
+ ```ruby
72
+ class Country < ActiveRecord::Base
73
+ def self.primary_key
74
+ "code"
75
+ end
76
+
77
+ attribute :code, String
78
+ attribute :name, String
79
+ end
80
+
81
+ Country.primary_key # => "code"
82
+ ```
83
+
84
+ #### Levels of inheritance
85
+
86
+ StaticModel also supports several levels of inheritance:
87
+
88
+ ```ruby
89
+ require "staticmodel"
90
+
91
+ class Country < StaticModel::Base
92
+ self.primary_key = "code"
93
+
94
+ attribute :code, String
95
+ attribute :name, String
96
+ end
97
+
98
+ class MyCountry < Country
99
+ end
100
+
101
+ class MyOtherCountry < Country
102
+ self.primary_key = "my_other_code"
103
+ end
104
+
105
+ MyCountry.primary_key # => "code"
106
+ MyOtherCountry.primary_key # => "my_other_code"
107
+ ```
108
+
109
+ ### Define the source of your data
110
+
111
+ By default, StaticModel uses a naming convention to find out how the mapping between models and YAML files should be created. It will pluralize your class names to find the respective YAML file. So, for a class Book, you should have a YAML file called **books**. The StaticModel pluralization mechanisms are based on [ActiveSupport](https://github.com/rails/rails/tree/master/activesupport) which is very powerful, being capable to pluralize (and singularize) both regular and irregular words. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the CamelCase form, while the file name must contain the words separated by underscores. Examples:
112
+
113
+ * YAML file - Plural with underscores separating words (e.g., book_clubs).
114
+ * Model Class - Singular with the first letter of each word capitalized (e.g., BookClub).
115
+
116
+ It's very common to organize all your YAML files into a single directory. To tell StaticModel where it can find your static files you can specify the load path anywhere in your code with:
117
+
118
+ ```ruby
119
+ StaticModel.configure do |config|
120
+ config.base_data_path = File.join(Rails.root, "config/data")
121
+ end
122
+ ```
123
+
124
+ If your YAML files are not located in the same directory or you don't want to follow the naming conventions, you can define per model the specific YAML file's path with the `data_path=` method:
125
+
126
+ ```ruby
127
+ class Country < StaticModel::Base
128
+ self.data_path = File.join(Rails.root, "config/data/countries.yml")
129
+ end
130
+ ```
131
+
132
+ #### YAML format
133
+
134
+ The format of the YAML file is very simple, an array of hashes. This could be an example for a `countries.yml` file:
135
+
136
+ ```yaml
137
+ ---
138
+ - code: ES
139
+ name: Spain
140
+ population: 46770000
141
+ - code: NL
142
+ name: Netherlands
143
+ population: 16800000
144
+ ```
145
+
146
+ ### Define your attributes and/or your default values
147
+
148
+ StaticModel depends on the well-written [Virtus](https://github.com/solnic/virtus) gem to define its attributes. Here is a basic example of the flexibility that it provides:
149
+
150
+
151
+ ```ruby
152
+ class Country < StaticModel::Base
153
+ attribute :id, String
154
+ attribute :name, String
155
+ attribute :population, Integer, default: 0
156
+ attribute :european, Boolean, default: false
157
+ attribute :province_names, Array[String]
158
+ end
159
+
160
+ country = Country.all.first
161
+ country.id # => "ES"
162
+ country.name # => "Spain"
163
+ country.population # => 46770000
164
+ country.european # => true
165
+ country.province_names # => ["Madrid", "Barcelona", ...]
166
+ ```
167
+
168
+ Advanced usage of the StaticModel attributes can be found at the [Virtus documentation](https://github.com/solnic/virtus).
169
+
170
+
171
+ ## Query interface
172
+
173
+ ### all
174
+
175
+ Returns all the records.
176
+
177
+ ```ruby
178
+ Country.all
179
+ # => [
180
+ # #<Country id="ES", name="Spain" european=true>,
181
+ # #<Country id="NL", name="Netherlands" european=true>,
182
+ # #<Country id="CO", name="Colombia" european=true>
183
+ # ]
184
+ ```
185
+
186
+ ### where(_criteria_)
187
+
188
+ The `where` method allows you to specify conditions to limit the records returned, like the `WHERE`-part of the SQL statement. Conditions must be specified as a hash.
189
+
190
+ ```ruby
191
+ Country.where(european: true)
192
+ # => [
193
+ # #<Country id="ES", name="Spain" european=true>,
194
+ # #<Country id="NL", name="Netherlands" european=true>
195
+ # ]
196
+ ```
197
+
198
+ ### find(_id_)
199
+
200
+ Using the `find` method, you can retrieve the object corresponding to the specified primary key that matches the supplied `id`.
201
+
202
+ ```ruby
203
+ Country.find("ES") # => #<Country id="ES", name="Spain">
204
+ Country.find("KO") # => StaticModel::NotFound
205
+ ```
206
+
207
+ The `find` method will raise a `StaticModel::NotFound` exception if no matching record is found.
208
+
209
+ ### find\_by(_criteria_)
210
+
211
+ The `find_by` method finds the first record matching some conditions.
212
+
213
+ ```ruby
214
+ Country.find_by(name: "Spain") # => #<Country id="ES", name="Spain">
215
+ Country.find_by(name: "Invalid") # => nil
216
+ ```
217
+
218
+ ### find\_by!(_criteria_)
219
+
220
+ The `find_by!` method behaves exactly like `find_by`, except that it will raise `StaticModel::NotFound` if no matching record is found.
221
+
222
+ ```ruby
223
+ Country.find_by!(name: "Spain") # => #<Country id="ES", name="Spain">
224
+ Country.find_by!(name: "Invalid") # => StaticModel::NotFound
225
+ ```
226
+
227
+ ### exists?(_criteria_)
228
+
229
+ If you simply want to check for the existence of the object there's a method called `exists?`. This method will use the same mechanism as `find`, but instead of returning an object it will return either `true` or `false`.
230
+
231
+ ```ruby
232
+ Country.exists?(name: "Spain") # => true
233
+ Country.exists?(name: "Invalid") # => false
234
+ ```
26
235
 
27
236
  ## Development
28
237
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
238
+ After checking out the repo, run `script/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `script/console` for an interactive prompt that will allow you to experiment.
30
239
 
31
240
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
241
 
33
242
  ## Contributing
34
243
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/staticmodel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
244
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fertapric/staticmodel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
245
 
37
246
 
38
247
  ## License
39
248
 
40
249
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
250
 
251
+ ## Author
252
+
253
+ Fernando Tapia Rico, @fertapric
254
+
data/lib/static_model.rb CHANGED
@@ -5,6 +5,7 @@ require "yaml"
5
5
  require "static_model/base"
6
6
  require "static_model/configuration"
7
7
  require "static_model/errors"
8
+ require "static_model/version"
8
9
 
9
10
  module StaticModel
10
11
  autoload :Shortcut, "static_model/shortcut"
@@ -3,7 +3,7 @@ module StaticModel
3
3
  include Virtus.model
4
4
 
5
5
  def id
6
- defined?(super) ? super : __send__(primary_key)
6
+ defined?(super) ? super : __send__(self.class.primary_key)
7
7
  end
8
8
 
9
9
  def self.primary_key
@@ -52,7 +52,7 @@ module StaticModel
52
52
  where(criteria).first
53
53
  end
54
54
 
55
- def self.exist?(criteria = {})
55
+ def self.exists?(criteria = {})
56
56
  if criteria.is_a?(Hash)
57
57
  !!find_by(criteria)
58
58
  else
@@ -2,7 +2,7 @@ module StaticModel
2
2
  VERSION = [
3
3
  VERSION_MAJOR = 1,
4
4
  VERSION_MINOR = 0,
5
- VERSION_TINY = 0,
5
+ VERSION_TINY = 2,
6
6
  VERSION_PRE = nil
7
7
  ].compact.join(".")
8
8
  end
@@ -0,0 +1 @@
1
+ require "static_model"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: staticmodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Tapia Rico
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-16 00:00:00.000000000 Z
11
+ date: 2015-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -95,6 +95,7 @@ files:
95
95
  - lib/static_model/errors.rb
96
96
  - lib/static_model/shortcut.rb
97
97
  - lib/static_model/version.rb
98
+ - lib/staticmodel.rb
98
99
  homepage: https://github.com/fertapric/staticmodel
99
100
  licenses:
100
101
  - MIT