static_collection 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/.ruby-version +1 -1
- data/README.md +33 -3
- data/lib/static_collection.rb +10 -0
- data/lib/static_collection/version.rb +1 -1
- data/static_collection.gemspec +2 -2
- metadata +5 -6
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aee7a2335834e9e570ff2bc0e0289c33babb47131cfbba9a7e99f23d117f7583
|
4
|
+
data.tar.gz: 26d5d69190b140785b8355e45c99cdcc0de4173e380ce8b195d8fab56ee5ed23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd6b84129218e0a98310d745aab5ef54e8a6ef5c14f868acb3dcc4a0d0aa701f5897f2e760a4beb4471960f393b1dc4e34303e13939799eb217a3cb4d567b9f5
|
7
|
+
data.tar.gz: d2c325572a184526e2fde0a1e89f6d85bd7c074ccbc42826b82af5b804c8f79eda390e5e0b16d2670ffbd5bb8596b414720d73f8eef8ce2c5899e38cd2ab8b9d
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.5.1
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# StaticCollection [![CircleCI](https://circleci.com/gh/wealthsimple/static_collection.svg?style=svg)](https://circleci.com/gh/wealthsimple/static_collection)
|
1
|
+
# StaticCollection [![CircleCI](https://circleci.com/gh/wealthsimple/static_collection.svg?style=svg)](https://circleci.com/gh/wealthsimple/static_collection) [![](https://img.shields.io/gem/v/static_collection.svg)](https://rubygems.org/gems/static_collection)
|
2
2
|
|
3
|
-
Rubygem for running basic queries against static
|
3
|
+
Rubygem for running basic queries against static data.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,7 +20,37 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
To create a StaticCollection model, inherit from `StaticCollection::Base`
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class AccountType < StaticCollection::Base
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Then set the source for the static collection data. To read from YAML,
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class AccountType < StaticCollection::Base
|
34
|
+
set_source YAML.load_file('./data/account_types_test.yml')
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
To set a default value for an attribute, pass a defaults hash into `set_source`
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class AccountType < StaticCollection::Base
|
42
|
+
set_source YAML.load_file('./data/account_types_test.yml'), defaults: { recommended_by_default: false }
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
StaticCollection supports the following query methods: `:count`, `:all`, `:find_by`, and `:where`.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
> AccountType.find_by(type: 'joint').ownership_type
|
50
|
+
=> "multi-owner"
|
51
|
+
```
|
52
|
+
|
53
|
+
To see the full `AccountType` example, take a look at our [blog post](http://code.wealthsimple.com/introducing-staticcollection-an-activerecord-interface-for-static-data/).
|
24
54
|
|
25
55
|
## Development
|
26
56
|
|
data/lib/static_collection.rb
CHANGED
@@ -12,9 +12,11 @@ module StaticCollection
|
|
12
12
|
all.first.attributes.each do |attribute_name, attribute_value|
|
13
13
|
# Class methods
|
14
14
|
self.define_singleton_method("find_by_#{attribute_name}") do |value|
|
15
|
+
ActiveSupport::Deprecation.warn("find_by_#{attribute_name} is deprecated for StaticCollection, use find_by(#{attribute_name}: [value])")
|
15
16
|
all.find { |instance| instance.send(attribute_name) == value }
|
16
17
|
end
|
17
18
|
self.define_singleton_method("find_all_by_#{attribute_name}") do |value|
|
19
|
+
ActiveSupport::Deprecation.warn("find_all_by_#{attribute_name} is deprecated for StaticCollection, use where(#{attribute_name}: [value])")
|
18
20
|
all.select { |instance| instance.send(attribute_name) == value }
|
19
21
|
end
|
20
22
|
|
@@ -24,6 +26,14 @@ module StaticCollection
|
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
29
|
+
def self.find_by(opts)
|
30
|
+
all.find { |instance| opts.all? { |k, v| instance.send(k) == v } }
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.where(opts)
|
34
|
+
all.select { |instance| opts.all? { |k, v| instance.send(k) == v } }
|
35
|
+
end
|
36
|
+
|
27
37
|
def self.all
|
28
38
|
defaults = self.class_variable_get(:@@defaults)
|
29
39
|
self.class_variable_get(:@@source).map { |s| self.new(defaults.merge(s)) }
|
data/static_collection.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Peter Graham"]
|
10
10
|
spec.email = ["peter@wealthsimple.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{Run queries against static
|
13
|
-
spec.description = %q{Rubygem for running basic queries against static
|
12
|
+
spec.summary = %q{Run queries against static data.}
|
13
|
+
spec.description = %q{Rubygem for running basic queries against static data.}
|
14
14
|
spec.homepage = "https://github.com/wealthsimple/static_collection"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static_collection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Graham
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.2'
|
83
|
-
description: Rubygem for running basic queries against static
|
83
|
+
description: Rubygem for running basic queries against static data.
|
84
84
|
email:
|
85
85
|
- peter@wealthsimple.com
|
86
86
|
executables: []
|
@@ -90,7 +90,6 @@ files:
|
|
90
90
|
- ".gitignore"
|
91
91
|
- ".rspec"
|
92
92
|
- ".ruby-version"
|
93
|
-
- ".travis.yml"
|
94
93
|
- Gemfile
|
95
94
|
- LICENSE.txt
|
96
95
|
- README.md
|
@@ -120,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
119
|
version: '0'
|
121
120
|
requirements: []
|
122
121
|
rubyforge_project:
|
123
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.7.6
|
124
123
|
signing_key:
|
125
124
|
specification_version: 4
|
126
|
-
summary: Run queries against static
|
125
|
+
summary: Run queries against static data.
|
127
126
|
test_files: []
|