static_models 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/static_models/version.rb +3 -0
- data/lib/static_models.rb +96 -0
- data/static_models.gemspec +32 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ea954799a4013aebd0fd97a706fb65cdd3a04f5
|
4
|
+
data.tar.gz: 991966ce298336a266fa9b8859d41fa4f2fd2aca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 968c9408edeed32e1f509eb0b117193f9f3235c52f7785c9889d7562f6d0ae14ca520d24df0a181f10d7cc78e948b951da503658defdbfbe86ca8d9894d448a0
|
7
|
+
data.tar.gz: 482f3bfedef5c81ac5da2a573cadbc3c16f5b55d2361fe88a660f12d08125ef73dd6fc892376a92a82226b8905d8e432adcb3305db77a52f2dc788c764530d4e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 nubis
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# StaticModels
|
2
|
+
|
3
|
+
DRY your accesory "key - value" classes. Use them as associations on a parent model.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'static_models'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install static_models
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# We're modelling Dog's, each of them has a Breed.
|
25
|
+
# We support a static set of Breeds.
|
26
|
+
|
27
|
+
class Breed
|
28
|
+
# Enhance Breed to be a StaticModel
|
29
|
+
include StaticModels::Model
|
30
|
+
|
31
|
+
# Actually define the breeds we support using a Hash
|
32
|
+
# Keys are the 'id' attribute of each model. Must be Integers.
|
33
|
+
# Values are the 'code' attribute. Must be Symbols, and unique.
|
34
|
+
# If you want your model to have extra attributes, make te value
|
35
|
+
# be an Array of 2 elements: [Symbol, Hash].
|
36
|
+
static_models(
|
37
|
+
1 => :collie,
|
38
|
+
2 => :foxhound,
|
39
|
+
6 => [:corgi, height: 'short'],
|
40
|
+
7 => [:doberman, height: 'tall']
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
# You can find your Breed.
|
45
|
+
Breed.find(6).tap do |b|
|
46
|
+
# You also get class methods for accessing each singleton instance.
|
47
|
+
b == Breed.corgi
|
48
|
+
|
49
|
+
b.code == :corgi
|
50
|
+
b.height == 'short'
|
51
|
+
end
|
52
|
+
|
53
|
+
# Definitions can be sparse, no need to set a value for all attributes.
|
54
|
+
Breed.collie.height.should be_nil
|
55
|
+
|
56
|
+
# All just returns the ordered collection.
|
57
|
+
Breed.all.should == [
|
58
|
+
Breed.collie,
|
59
|
+
Breed.foxhound,
|
60
|
+
Breed.corgi,
|
61
|
+
Breed.doberman
|
62
|
+
]
|
63
|
+
|
64
|
+
# The low level 'values' dictionary is public.
|
65
|
+
Breed.values.should == {
|
66
|
+
1 => Breed.collie,
|
67
|
+
2 => Breed.foxhound,
|
68
|
+
6 => Breed.corgi,
|
69
|
+
7 => Breed.doberman
|
70
|
+
}
|
71
|
+
|
72
|
+
# You can use your StaticModels similar to an association.
|
73
|
+
# Setting a Breed will update a breed_id attribute.
|
74
|
+
class Dog
|
75
|
+
attr_accessor :breed_id
|
76
|
+
|
77
|
+
include StaticModels::BelongsTo
|
78
|
+
belongs_to_static_model :breed
|
79
|
+
end
|
80
|
+
|
81
|
+
Dog.new.tap do |d|
|
82
|
+
# Setting a Breed will update the underlying breed_id column.
|
83
|
+
d.breed_id == nil
|
84
|
+
d.breed = Breed.corgi
|
85
|
+
d.breed_id == 6
|
86
|
+
|
87
|
+
# Also, setting the breed_id column will update the Breed.
|
88
|
+
d.breed_id = 7
|
89
|
+
d.breed.should == Breed.doberman
|
90
|
+
end
|
91
|
+
|
92
|
+
# Set your model manually if it can't be inferred from the attribute name
|
93
|
+
|
94
|
+
class WeirdDoggie
|
95
|
+
attr_accessor :dog_kind_id
|
96
|
+
|
97
|
+
include StaticModels::BelongsTo
|
98
|
+
belongs_to_static_model :dog_kind, Breed
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
## Development
|
103
|
+
|
104
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
105
|
+
|
106
|
+
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).
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/static_models.
|
111
|
+
|
112
|
+
|
113
|
+
## License
|
114
|
+
|
115
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
116
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "static_models"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "static_models/version"
|
2
|
+
require "active_support"
|
3
|
+
require "active_support/inflector"
|
4
|
+
|
5
|
+
module StaticModels
|
6
|
+
module Model
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do |i|
|
10
|
+
cattr_accessor :values
|
11
|
+
|
12
|
+
def initialize(id, code, *args)
|
13
|
+
self.id = id
|
14
|
+
self.code = code
|
15
|
+
unless args.empty?
|
16
|
+
args.first.each{|name,value| send("#{name}=", value) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
code.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_i
|
25
|
+
id
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
code
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class_methods do
|
34
|
+
def static_models(new_values)
|
35
|
+
attr_accessor :id, :code
|
36
|
+
|
37
|
+
self.values = {}
|
38
|
+
new_values.each do |k,v|
|
39
|
+
unless k.is_a?(Integer)
|
40
|
+
raise TypeError.new("Expected Integer for keys, found #{k.class}")
|
41
|
+
end
|
42
|
+
|
43
|
+
if v.is_a?(Array)
|
44
|
+
unless v.size == 2 && v.first.is_a?(Symbol) && v.last.is_a?(Hash)
|
45
|
+
raise TypeError.new("Expected [Symbol, Hash] found #{v.class}")
|
46
|
+
end
|
47
|
+
attr_accessor *v.last.keys
|
48
|
+
else
|
49
|
+
unless v.is_a?(Symbol)
|
50
|
+
raise TypeError.new("Expected Symbol, found #{v.class}")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
item = new(k, *v)
|
54
|
+
values[k] = item
|
55
|
+
|
56
|
+
raise DuplicateCodes.new if singleton_methods.include?(item.code)
|
57
|
+
define_singleton_method(item.code){ item }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def find(id)
|
62
|
+
values[id.to_i]
|
63
|
+
end
|
64
|
+
|
65
|
+
def all
|
66
|
+
values.values
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# When included, it adds a class method that works similar to rails
|
72
|
+
# 'belongs_to', but instead of fetching an association it gets a static model.
|
73
|
+
module BelongsTo
|
74
|
+
extend ActiveSupport::Concern
|
75
|
+
|
76
|
+
class_methods do
|
77
|
+
def belongs_to_static_model(attr_name, cls=nil)
|
78
|
+
cls ||= attr_name.to_s.humanize.constantize
|
79
|
+
|
80
|
+
define_method(attr_name) do
|
81
|
+
cls.find(send("#{attr_name}_id"))
|
82
|
+
end
|
83
|
+
|
84
|
+
define_method("#{attr_name}=") do |value|
|
85
|
+
unless value.nil? || value.is_a?(cls)
|
86
|
+
raise TypeError.new("Expected #{cls} got #{value.class}")
|
87
|
+
end
|
88
|
+
send("#{attr_name}_id=", value && value.id)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class TypeError < StandardError; end
|
95
|
+
class DuplicateCodes < StandardError; end
|
96
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'static_models/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "static_models"
|
8
|
+
spec.version = StaticModels::VERSION
|
9
|
+
spec.authors = ["nubis"]
|
10
|
+
spec.email = ["yo@nubis.im"]
|
11
|
+
|
12
|
+
spec.summary = %q{DRY your key-value classes. Use them as associations}
|
13
|
+
spec.description = %q{
|
14
|
+
Replace your key/value classes with this.
|
15
|
+
Define classes with several 'singleton' instances.
|
16
|
+
}
|
17
|
+
spec.homepage = "https://github.com/bitex-la/static_models"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
spec.add_dependency "activesupport", '~> 4.2', '>= 4.2.0'
|
27
|
+
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: static_models
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nubis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.2.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.2.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.13'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.13'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.0'
|
75
|
+
description: "\n Replace your key/value classes with this.\n Define classes
|
76
|
+
with several 'singleton' instances.\n "
|
77
|
+
email:
|
78
|
+
- yo@nubis.im
|
79
|
+
executables: []
|
80
|
+
extensions: []
|
81
|
+
extra_rdoc_files: []
|
82
|
+
files:
|
83
|
+
- ".gitignore"
|
84
|
+
- ".rspec"
|
85
|
+
- Gemfile
|
86
|
+
- LICENSE.txt
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- bin/console
|
90
|
+
- bin/setup
|
91
|
+
- lib/static_models.rb
|
92
|
+
- lib/static_models/version.rb
|
93
|
+
- static_models.gemspec
|
94
|
+
homepage: https://github.com/bitex-la/static_models
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.5.1
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: DRY your key-value classes. Use them as associations
|
118
|
+
test_files: []
|