frozen_record 0.9.0 → 0.10.0

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: 785548769c7ba7f3bbb52f2e17b9a3199b5dbcc7
4
- data.tar.gz: a35161fae1e80efc72d01ea0eb0e4c45e0cacd84
3
+ metadata.gz: 69d17666ac957cc0d3b75d51f49692a41be67947
4
+ data.tar.gz: 51214e7c50fae48f90a2bdf586cd351a266f028c
5
5
  SHA512:
6
- metadata.gz: c026e89f7d50bdf288da41935a7615d9e45906ab041da84f1fb564ab79bc53ebde815d2c63adeaad16dc7bfcba36b2c1a10bf99e9f91e6e181af53b9be3aee51
7
- data.tar.gz: 5081081b183668d2eb253576ffa04116a348d9e90901dee06e9ef0bec66d0b78224f906877551cd4dd255b5451b79d92722e440eee0ad045d82d393198ba06e3
6
+ metadata.gz: b6ea860e92550663a785be7589fc4740d15ec86ff8b2a42aecdf7d026f76b5fd674993b91d459570458a87130b780a2e054a1a2668864d4b94d0035c52211a1a
7
+ data.tar.gz: 5ff4a501f9996fdfc845a6cefad6571614c832fc865f70e5fc18ea4226a2d643aa73d4443c463f4b255f93da86125207ff345382ef571f7f1051d58e4b74123b
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Coverage Status](https://coveralls.io/repos/byroot/frozen_record/badge.svg)](https://coveralls.io/r/byroot/frozen_record)
6
6
  [![Gem Version](https://badge.fury.io/rb/frozen_record.svg)](http://badge.fury.io/rb/frozen_record)
7
7
 
8
- ActiveRecord-like interface for **read only** access to YAML static data.
8
+ ActiveRecord-like interface for **read only** access to static data files.
9
9
 
10
10
  ## Installation
11
11
 
@@ -44,6 +44,34 @@ class Country < FrozenRecord::Base
44
44
  end
45
45
  ```
46
46
 
47
+ You can also specify a custom backend. Backends are classes that know how to
48
+ load records from a static file. By default FrozenRecord expects an YAML file,
49
+ but this option can be changed per model:
50
+
51
+ ```ruby
52
+ class Country < FrozenRecord::Base
53
+ self.backend = FrozenRecord::Backends::Yaml
54
+ end
55
+ ```
56
+
57
+ ### Custom backends
58
+
59
+ A valid backend must implement two class methods, `filename` and `load`.
60
+
61
+ ```ruby
62
+ class MyCustomBackend
63
+ class << self
64
+ def filename(model_name)
65
+ # Returns the file name as a String
66
+ end
67
+
68
+ def load(file_path)
69
+ # Reads file and returns records as an Array of Hash objects
70
+ end
71
+ end
72
+ end
73
+ ```
74
+
47
75
  ## Query interface
48
76
 
49
77
  FrozenRecord aim to replicate only modern ActiveRecord querying interface, and only the non "string typed" ones.
@@ -0,0 +1,27 @@
1
+ module FrozenRecord
2
+ module Backends
3
+ module Yaml
4
+ extend self
5
+
6
+ # Transforms the model name into a valid filename.
7
+ #
8
+ # @param format [String] the model name that inherits
9
+ # from FrozenRecord::Base
10
+ # @return [String] the file name.
11
+ def filename(model_name)
12
+ "#{model_name.underscore.pluralize}.yml"
13
+ end
14
+
15
+ # Reads file in `file_path` and return records.
16
+ #
17
+ # @param format [String] the file path
18
+ # @return [Array] an Array of Hash objects with keys being attributes.
19
+ def load(file_path)
20
+ yml_erb_data = File.read(file_path)
21
+ yml_data = ERB.new(yml_erb_data).result
22
+
23
+ YAML.load(yml_data) || []
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,6 @@
1
1
  require 'set'
2
2
  require 'active_support/descendants_tracker'
3
+ require 'frozen_record/backends/yaml'
3
4
 
4
5
  module FrozenRecord
5
6
  class Base
@@ -21,6 +22,9 @@ module FrozenRecord
21
22
  class_attribute :primary_key
22
23
  self.primary_key = :id
23
24
 
25
+ class_attribute :backend
26
+ self.backend = FrozenRecord::Backends::Yaml
27
+
24
28
  class_attribute :auto_reloading
25
29
 
26
30
  attribute_method_suffix '?'
@@ -64,7 +68,7 @@ module FrozenRecord
64
68
 
65
69
  def file_path
66
70
  raise ArgumentError, "You must define `#{name}.base_path`" unless base_path
67
- File.join(base_path, "#{name.underscore.pluralize}.yml")
71
+ File.join(base_path, backend.filename(name))
68
72
  end
69
73
 
70
74
  def respond_to_missing?(name, *)
@@ -91,10 +95,7 @@ module FrozenRecord
91
95
  end
92
96
 
93
97
  @records ||= begin
94
- yml_erb_data = File.read(file_path)
95
- yml_data = ERB.new(yml_erb_data).result
96
-
97
- records = YAML.load(yml_data) || []
98
+ records = backend.load(file_path)
98
99
  define_attribute_methods(list_attributes(records))
99
100
  records.map(&method(:new)).freeze
100
101
  end
@@ -1,3 +1,3 @@
1
1
  module FrozenRecord
2
- VERSION = '0.9.0'
2
+ VERSION = '0.10.0'
3
3
  end
@@ -0,0 +1,14 @@
1
+ [
2
+ {
3
+ "name": "cat",
4
+ "family": "felidae"
5
+ },
6
+ {
7
+ "name": "dog",
8
+ "family": "canidae"
9
+ },
10
+ {
11
+ "name": "elephant",
12
+ "family": "elephantidae"
13
+ }
14
+ ]
@@ -55,11 +55,16 @@ describe FrozenRecord::Base do
55
55
 
56
56
  describe '#load_records' do
57
57
 
58
- it 'processes erb' do
58
+ it 'processes erb by default' do
59
59
  country = Country.first
60
60
  expect(country.capital).to be == 'Ottawa'
61
61
  end
62
62
 
63
+ it 'loads records with a custom backend' do
64
+ animal = Animal.first
65
+ expect(animal.name).to be == 'cat'
66
+ end
67
+
63
68
  end
64
69
 
65
70
  describe '#==' do
@@ -0,0 +1,16 @@
1
+ module JsonBackend
2
+ extend self
3
+
4
+ def filename(model_name)
5
+ "#{model_name.underscore.pluralize}.json"
6
+ end
7
+
8
+ def load(file_path)
9
+ json_data = File.read(file_path)
10
+ JSON.parse(json_data) || []
11
+ end
12
+ end
13
+
14
+ class Animal < FrozenRecord::Base
15
+ self.backend = JsonBackend
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frozen_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-01 00:00:00.000000000 Z
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -110,15 +110,18 @@ files:
110
110
  - Rakefile
111
111
  - frozen_record.gemspec
112
112
  - lib/frozen_record.rb
113
+ - lib/frozen_record/backends/yaml.rb
113
114
  - lib/frozen_record/base.rb
114
115
  - lib/frozen_record/scope.rb
115
116
  - lib/frozen_record/version.rb
117
+ - spec/fixtures/animals.json
116
118
  - spec/fixtures/cars.yml
117
119
  - spec/fixtures/countries.yml
118
120
  - spec/frozen_record_spec.rb
119
121
  - spec/scope_spec.rb
120
122
  - spec/spec_helper.rb
121
123
  - spec/support/abstract_model.rb
124
+ - spec/support/animal.rb
122
125
  - spec/support/car.rb
123
126
  - spec/support/country.rb
124
127
  homepage: https://github.com/byroot/frozen_record
@@ -146,11 +149,13 @@ signing_key:
146
149
  specification_version: 4
147
150
  summary: ActiveRecord like interface to read only access and query static YAML files
148
151
  test_files:
152
+ - spec/fixtures/animals.json
149
153
  - spec/fixtures/cars.yml
150
154
  - spec/fixtures/countries.yml
151
155
  - spec/frozen_record_spec.rb
152
156
  - spec/scope_spec.rb
153
157
  - spec/spec_helper.rb
154
158
  - spec/support/abstract_model.rb
159
+ - spec/support/animal.rb
155
160
  - spec/support/car.rb
156
161
  - spec/support/country.rb