minimodel 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +9 -0
- data/README.md +10 -8
- data/lib/minimodel.rb +9 -7
- data/minimodel.gemspec +10 -12
- data/spec/colour_spec.rb +5 -5
- data/spec/currency_spec.rb +25 -20
- data/spec/dutch_education_spec.rb +22 -22
- metadata +49 -43
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ead30d3e5905fd4a9cefbcc8acefafea60f88ac3
|
4
|
+
data.tar.gz: a91bd67fc303c5fa57b4f43865a5fd788bd5bcf8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cf78af9171a8a066e3e1fe244b306c9dfb33803a34b5efff3076a8f87d5441b6166141ac621a9ca623320c43d6675d71ffb270c9d991f642b64da68d3516c9b
|
7
|
+
data.tar.gz: 57328487523f9ab365bc11ce754e6066d8508ac64c18621573f7dc381aab4e317f9258d18da40167e82a8ff71d4f3ddfba2ef0d99a790e9d2ff241f704bc6a63
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Copyright (c) TIMCRAFT <http://timcraft.com>
|
2
|
+
|
3
|
+
This is an Open Source project licensed under the terms of the LGPLv3 license.
|
4
|
+
Please see <http://www.gnu.org/licenses/lgpl-3.0.html> for license text.
|
5
|
+
|
6
|
+
This code is distributed in the hope that it will be useful,
|
7
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
8
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
9
|
+
GNU Lesser General Public License for more details.
|
data/README.md
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
-
|
2
|
-
===========================================
|
1
|
+
# minimodel
|
3
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/minimodel.svg)](https://badge.fury.io/rb/minimodel) [![Build Status](https://api.travis-ci.org/timcraft/minimodel.svg?branch=master)](https://travis-ci.org/timcraft/minimodel)
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
|
6
|
+
A little Ruby library for defining little models.
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
7
10
|
|
8
11
|
$ gem install minimodel
|
9
12
|
|
10
13
|
|
11
|
-
Motivation
|
12
|
-
----------
|
14
|
+
## Motivation
|
13
15
|
|
14
16
|
Many apps use small "read only" datasets that can easily be held in memory,
|
15
17
|
for example: currency data, country data, colour data, advertising banners,
|
@@ -17,8 +19,7 @@ options for select boxes, payment plans etc. Sometimes it's useful to model
|
|
17
19
|
this data without using a database, and that's what minimodel is for.
|
18
20
|
|
19
21
|
|
20
|
-
Example
|
21
|
-
-------
|
22
|
+
## Example
|
22
23
|
|
23
24
|
Here's how you could implement a currency model using minimodel:
|
24
25
|
|
@@ -37,6 +38,7 @@ end
|
|
37
38
|
The Currency class will respond to `#count` (returning the total number of
|
38
39
|
currencies), `#all` (returning an array of currency objects), and `#find`
|
39
40
|
(to lookup a specific currency by its code). Similar to ActiveRecord.
|
41
|
+
|
40
42
|
There's also a `load_from` class method which will load data from a YAML
|
41
43
|
file; useful for when you'd rather store the raw data outside of the class.
|
42
44
|
|
data/lib/minimodel.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
|
3
3
|
class MiniModel
|
4
|
-
if defined?(id)
|
5
|
-
undef_method :id
|
6
|
-
end
|
7
|
-
|
8
4
|
def initialize(attributes)
|
9
5
|
@attributes = attributes.to_hash
|
10
6
|
end
|
@@ -30,7 +26,7 @@ class MiniModel
|
|
30
26
|
end
|
31
27
|
|
32
28
|
def method_missing(symbol, *args, &block)
|
33
|
-
if @attributes.
|
29
|
+
if @attributes.key?(symbol) && args.empty? && block.nil?
|
34
30
|
return @attributes[symbol]
|
35
31
|
else
|
36
32
|
super symbol, *args, &block
|
@@ -38,7 +34,7 @@ class MiniModel
|
|
38
34
|
end
|
39
35
|
|
40
36
|
def respond_to_missing?(symbol, include_private = false)
|
41
|
-
@attributes.
|
37
|
+
@attributes.key?(symbol)
|
42
38
|
end
|
43
39
|
|
44
40
|
class DuplicateKeyError < StandardError
|
@@ -67,6 +63,12 @@ class MiniModel
|
|
67
63
|
all.map(&primary_key)
|
68
64
|
end
|
69
65
|
|
66
|
+
def options(text_attribute)
|
67
|
+
all.each_with_object({}) do |model, hash|
|
68
|
+
hash[model.read_attribute(text_attribute)] = model.read_attribute(primary_key)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
70
72
|
def insert(attributes)
|
71
73
|
unless @auto_increment.nil?
|
72
74
|
attributes[primary_key] = @auto_increment
|
@@ -78,7 +80,7 @@ class MiniModel
|
|
78
80
|
|
79
81
|
pkey = object.send(primary_key)
|
80
82
|
|
81
|
-
if index.
|
83
|
+
if index.key?(pkey)
|
82
84
|
raise DuplicateKeyError
|
83
85
|
end
|
84
86
|
|
data/minimodel.gemspec
CHANGED
@@ -1,21 +1,19 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'minimodel'
|
3
|
-
s.version = '
|
3
|
+
s.version = '2.0.0'
|
4
|
+
s.license = 'LGPL-3.0'
|
4
5
|
s.platform = Gem::Platform::RUBY
|
5
6
|
s.authors = ['Tim Craft']
|
6
7
|
s.email = ['mail@timcraft.com']
|
7
8
|
s.homepage = 'http://github.com/timcraft/minimodel'
|
8
|
-
s.description = 'A little library for defining little models'
|
9
|
+
s.description = 'A little Ruby library for defining little models'
|
9
10
|
s.summary = 'See description'
|
10
|
-
s.files = Dir.glob('{lib,spec}/**/*') + %w(README.md Rakefile minimodel.gemspec)
|
11
|
-
s.
|
12
|
-
s.add_development_dependency('
|
13
|
-
s.add_development_dependency('activerecord',
|
14
|
-
s.add_development_dependency('sqlite3',
|
11
|
+
s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE.txt README.md Rakefile minimodel.gemspec)
|
12
|
+
s.required_ruby_version = '>= 1.9.3'
|
13
|
+
s.add_development_dependency('rake', '~> 10')
|
14
|
+
s.add_development_dependency('activerecord', '~> 5')
|
15
|
+
s.add_development_dependency('sqlite3', '~> 1.3')
|
16
|
+
s.add_development_dependency('minitest', '~> 5')
|
17
|
+
s.add_development_dependency('json', '~> 2')
|
15
18
|
s.require_path = 'lib'
|
16
|
-
|
17
|
-
if RUBY_VERSION == '1.8.7'
|
18
|
-
s.add_development_dependency('minitest', '>= 4.2.0')
|
19
|
-
s.add_development_dependency('json', '>= 1.7.7')
|
20
|
-
end
|
21
19
|
end
|
data/spec/colour_spec.rb
CHANGED
@@ -2,15 +2,15 @@ require 'minitest/autorun'
|
|
2
2
|
require 'minimodel'
|
3
3
|
|
4
4
|
class Colour < MiniModel
|
5
|
-
indexed_by :id, :
|
5
|
+
indexed_by :id, auto_increment: true
|
6
6
|
|
7
|
-
insert :
|
8
|
-
insert :
|
9
|
-
insert :
|
7
|
+
insert name: 'Blue', hexdigits: '0000FF'
|
8
|
+
insert name: 'Red', hexdigits: 'FF0000'
|
9
|
+
insert name: 'Green', hexdigits: '00FF00'
|
10
10
|
end
|
11
11
|
|
12
12
|
describe 'Colour' do
|
13
13
|
it 'assigns auto incrementing id values' do
|
14
|
-
Colour.all.map(&:id).must_equal([1, 2, 3])
|
14
|
+
Colour.all.map(&:id).sort.must_equal([1, 2, 3])
|
15
15
|
end
|
16
16
|
end
|
data/spec/currency_spec.rb
CHANGED
@@ -7,20 +7,18 @@ class Currency < MiniModel
|
|
7
7
|
|
8
8
|
load_from 'spec/currency_data.yml'
|
9
9
|
|
10
|
-
insert :
|
11
|
-
insert :
|
10
|
+
insert code: 'INR', name: 'Indian rupee'
|
11
|
+
insert code: 'JPY', name: 'Japanese yen'
|
12
12
|
end
|
13
13
|
|
14
14
|
describe 'A currency object' do
|
15
15
|
before do
|
16
|
-
@euro = Currency.new(:
|
16
|
+
@euro = Currency.new(code: 'EUR', name: 'Euro')
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
@euro.respond_to?(:name).must_equal(true)
|
23
|
-
end
|
19
|
+
it 'responds to code and name methods' do
|
20
|
+
@euro.respond_to?(:code).must_equal(true)
|
21
|
+
@euro.respond_to?(:name).must_equal(true)
|
24
22
|
end
|
25
23
|
|
26
24
|
it 'provides attribute reader methods' do
|
@@ -31,17 +29,17 @@ describe 'A currency object' do
|
|
31
29
|
describe 'eql query method' do
|
32
30
|
it 'returns true when passed a currency object with the same attributes' do
|
33
31
|
@euro.eql?(@euro).must_equal(true)
|
34
|
-
@euro.eql?(Currency.new(:
|
32
|
+
@euro.eql?(Currency.new(code: 'EUR', name: 'Euro')).must_equal(true)
|
35
33
|
end
|
36
34
|
|
37
35
|
it 'returns false when given a currency object with a different code' do
|
38
|
-
@euro.eql?(Currency.new(:
|
36
|
+
@euro.eql?(Currency.new(code: 'GBP', name: 'Pound sterling')).must_equal(false)
|
39
37
|
end
|
40
38
|
end
|
41
39
|
|
42
40
|
describe 'to_hash method' do
|
43
41
|
it 'returns a hash containing the object attributes' do
|
44
|
-
@euro.to_hash.must_equal({:
|
42
|
+
@euro.to_hash.must_equal({code: 'EUR', name: 'Euro'})
|
45
43
|
end
|
46
44
|
end
|
47
45
|
|
@@ -72,10 +70,21 @@ describe 'Currency' do
|
|
72
70
|
|
73
71
|
describe 'keys class method' do
|
74
72
|
it 'returns an array containing all the primary keys' do
|
75
|
-
keys
|
76
|
-
|
73
|
+
Currency.keys.must_equal(%w(EUR GBP USD INR JPY))
|
74
|
+
end
|
75
|
+
end
|
77
76
|
|
78
|
-
|
77
|
+
describe 'options class method' do
|
78
|
+
it 'returns a hash mapping the given text attribute to primary keys' do
|
79
|
+
options = {
|
80
|
+
'Euro' => 'EUR',
|
81
|
+
'Pound sterling' => 'GBP',
|
82
|
+
'United States dollar' => 'USD',
|
83
|
+
'Indian rupee' => 'INR',
|
84
|
+
'Japanese yen' => 'JPY'
|
85
|
+
}
|
86
|
+
|
87
|
+
Currency.options(:name).must_equal(options)
|
79
88
|
end
|
80
89
|
end
|
81
90
|
|
@@ -101,11 +110,7 @@ describe 'Currency' do
|
|
101
110
|
end
|
102
111
|
|
103
112
|
it 'raises an error if the currency cannot be found' do
|
104
|
-
|
105
|
-
proc { Currency.find('FOO') }.must_raise(KeyError) # 1.9
|
106
|
-
else
|
107
|
-
proc { Currency.find('FOO') }.must_raise(IndexError) # 1.8
|
108
|
-
end
|
113
|
+
proc { Currency.find('FOO') }.must_raise(KeyError) # 1.9
|
109
114
|
end
|
110
115
|
|
111
116
|
it 'yields if the currency cannot be found and the caller supplies a block' do
|
@@ -115,7 +120,7 @@ describe 'Currency' do
|
|
115
120
|
|
116
121
|
describe 'insert class method' do
|
117
122
|
it 'raises an error when passed a key that already exists' do
|
118
|
-
proc { Currency.insert(:
|
123
|
+
proc { Currency.insert(code: 'EUR', name: 'Euro') }.must_raise(MiniModel::DuplicateKeyError)
|
119
124
|
end
|
120
125
|
end
|
121
126
|
end
|
@@ -3,12 +3,12 @@ require 'minimodel'
|
|
3
3
|
require 'minimodel/associations'
|
4
4
|
require 'active_record'
|
5
5
|
|
6
|
-
ActiveRecord::Base.establish_connection(:
|
6
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
7
7
|
|
8
|
-
ActiveRecord::Schema.define(:
|
9
|
-
create_table :courses, :
|
10
|
-
t.string :name, :
|
11
|
-
t.string :level_name, :
|
8
|
+
ActiveRecord::Schema.define(version: 1) do
|
9
|
+
create_table :courses, force: true do |t|
|
10
|
+
t.string :name, index: true
|
11
|
+
t.string :level_name, index: true
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -18,30 +18,30 @@ class Level < MiniModel
|
|
18
18
|
has_many :courses
|
19
19
|
has_many :profiles
|
20
20
|
|
21
|
-
insert :
|
22
|
-
insert :
|
23
|
-
insert :
|
21
|
+
insert name: 'VMBO-T'
|
22
|
+
insert name: 'HAVO'
|
23
|
+
insert name: 'VWO'
|
24
24
|
end
|
25
25
|
|
26
26
|
class Profile < MiniModel
|
27
|
-
indexed_by :id, :
|
27
|
+
indexed_by :id, auto_increment: true
|
28
28
|
|
29
29
|
belongs_to :level
|
30
30
|
|
31
|
-
insert :
|
32
|
-
insert :
|
33
|
-
insert :
|
34
|
-
insert :
|
31
|
+
insert level_name: 'VMBO-T', name: 'Techniek'
|
32
|
+
insert level_name: 'VMBO-T', name: 'Zorg en Welzijn'
|
33
|
+
insert level_name: 'VMBO-T', name: 'Economie'
|
34
|
+
insert level_name: 'VMBO-T', name: 'Landbouw'
|
35
35
|
|
36
|
-
insert :
|
37
|
-
insert :
|
38
|
-
insert :
|
39
|
-
insert :
|
36
|
+
insert level_name: 'HAVO', name: 'Economie en Maatschappij'
|
37
|
+
insert level_name: 'HAVO', name: 'Cultuur en Maatschappij'
|
38
|
+
insert level_name: 'HAVO', name: 'Natuur en Gezondheid'
|
39
|
+
insert level_name: 'HAVO', name: 'Natuur en Techniek'
|
40
40
|
|
41
|
-
insert :
|
42
|
-
insert :
|
43
|
-
insert :
|
44
|
-
insert :
|
41
|
+
insert level_name: 'VWO', name: 'Economie en Maatschappij'
|
42
|
+
insert level_name: 'VWO', name: 'Cultuur en Maatschappij'
|
43
|
+
insert level_name: 'VWO', name: 'Natuur en Gezondheid'
|
44
|
+
insert level_name: 'VWO', name: 'Natuur en Techniek'
|
45
45
|
end
|
46
46
|
|
47
47
|
class Course < ActiveRecord::Base
|
@@ -52,7 +52,7 @@ end
|
|
52
52
|
|
53
53
|
Level.all.each do |level|
|
54
54
|
for course_name in %w( Nederlands Engels Duits )
|
55
|
-
Course.create!(:
|
55
|
+
Course.create!(level_name: level.name, name: "#{level.name}/#{course_name}")
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
metadata
CHANGED
@@ -1,118 +1,124 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Tim Craft
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2017-06-09 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: 10
|
19
|
+
version: '10'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: 10
|
26
|
+
version: '10'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
28
|
+
name: activerecord
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
33
|
+
version: '5'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
40
|
+
version: '5'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
42
|
+
name: sqlite3
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
47
|
+
version: '1.3'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - "~>"
|
60
53
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
54
|
+
version: '1.3'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: json
|
64
71
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
|
-
- - ~>
|
73
|
+
- - "~>"
|
68
74
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
75
|
+
version: '2'
|
70
76
|
type: :development
|
71
77
|
prerelease: false
|
72
78
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
79
|
requirements:
|
75
|
-
- - ~>
|
80
|
+
- - "~>"
|
76
81
|
- !ruby/object:Gem::Version
|
77
|
-
version:
|
78
|
-
description: A little library for defining little models
|
82
|
+
version: '2'
|
83
|
+
description: A little Ruby library for defining little models
|
79
84
|
email:
|
80
85
|
- mail@timcraft.com
|
81
86
|
executables: []
|
82
87
|
extensions: []
|
83
88
|
extra_rdoc_files: []
|
84
89
|
files:
|
85
|
-
-
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
86
93
|
- lib/minimodel.rb
|
94
|
+
- lib/minimodel/associations.rb
|
95
|
+
- minimodel.gemspec
|
87
96
|
- spec/colour_spec.rb
|
88
97
|
- spec/currency_data.yml
|
89
98
|
- spec/currency_spec.rb
|
90
99
|
- spec/dutch_education_spec.rb
|
91
|
-
- README.md
|
92
|
-
- Rakefile
|
93
|
-
- minimodel.gemspec
|
94
100
|
homepage: http://github.com/timcraft/minimodel
|
95
|
-
licenses:
|
101
|
+
licenses:
|
102
|
+
- LGPL-3.0
|
103
|
+
metadata: {}
|
96
104
|
post_install_message:
|
97
105
|
rdoc_options: []
|
98
106
|
require_paths:
|
99
107
|
- lib
|
100
108
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
109
|
requirements:
|
103
|
-
- -
|
110
|
+
- - ">="
|
104
111
|
- !ruby/object:Gem::Version
|
105
|
-
version:
|
112
|
+
version: 1.9.3
|
106
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
-
none: false
|
108
114
|
requirements:
|
109
|
-
- -
|
115
|
+
- - ">="
|
110
116
|
- !ruby/object:Gem::Version
|
111
117
|
version: '0'
|
112
118
|
requirements: []
|
113
119
|
rubyforge_project:
|
114
|
-
rubygems_version:
|
120
|
+
rubygems_version: 2.6.11
|
115
121
|
signing_key:
|
116
|
-
specification_version:
|
122
|
+
specification_version: 4
|
117
123
|
summary: See description
|
118
124
|
test_files: []
|