static_models 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 +4 -4
- data/README.md +30 -6
- data/lib/static_models/version.rb +1 -1
- data/lib/static_models.rb +42 -19
- data/static_models.gemspec +2 -0
- metadata +36 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea4da2c5cca0318b880691f4a2bc58709e30ddd9
|
4
|
+
data.tar.gz: 2aa14a89d48db484bb8a55b72cbb2816760f1da4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c3e819e42b7fab9be0573ccd8780aba93e58b82963732f63145d3cb62d12e9aeeff7449d13b946f85d07027b8cc338f8595fa193fe3245de1ba65a3ac24f10c
|
7
|
+
data.tar.gz: 8e35bebe78a36757e41318a601f788491e749d7b3d8f2c9c70e5bf613e202edba1de1062fcdf3362c18deaedf56fc71dac6c4d788d70e089a5e82f602af0c7a9
|
data/README.md
CHANGED
@@ -75,7 +75,7 @@ Or install it yourself as:
|
|
75
75
|
attr_accessor :breed_id
|
76
76
|
|
77
77
|
include StaticModels::BelongsTo
|
78
|
-
|
78
|
+
belongs_to :breed
|
79
79
|
end
|
80
80
|
|
81
81
|
Dog.new.tap do |d|
|
@@ -89,14 +89,38 @@ Or install it yourself as:
|
|
89
89
|
d.breed.should == Breed.doberman
|
90
90
|
end
|
91
91
|
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
# StaticModels::BelongsTo plays nice with ActiveRecords belongs_to.
|
93
|
+
# You can use it in your models transparently, it will know
|
94
|
+
# when to use a StaticModel or call out to ActiveRecord's code.
|
95
|
+
# You can even set up polymorphic associations that point to either
|
96
|
+
# a StaticModel or an ActiveRecord model.
|
96
97
|
|
98
|
+
class StoreDog < ActiveRecord::Base
|
97
99
|
include StaticModels::BelongsTo
|
98
|
-
|
100
|
+
belongs_to :breed
|
101
|
+
belongs_to :classification, class_name: 'Breed'
|
102
|
+
belongs_to :anything, polymorphic: true
|
103
|
+
belongs_to :store_dog
|
104
|
+
belongs_to :another_dog, class_name: 'StoreDog'
|
105
|
+
belongs_to :anydog, polymorphic: true
|
99
106
|
end
|
107
|
+
|
108
|
+
dog = StoreDog.new
|
109
|
+
dog.breed = Breed.corgi
|
110
|
+
dog.classification = Breed.collie
|
111
|
+
dog.anything = Breed.doberman
|
112
|
+
dog.store_dog = dog
|
113
|
+
dog.another_dog = dog
|
114
|
+
dog.anydog = dog
|
115
|
+
dog.save!
|
116
|
+
dog.reload
|
117
|
+
dog.breed == Breed.corgi
|
118
|
+
dog.classification == Breed.collie
|
119
|
+
dog.anything == Breed.doberman
|
120
|
+
dog.store_dog == dog
|
121
|
+
dog.another_dog == dog
|
122
|
+
dog.anydog == dog
|
123
|
+
|
100
124
|
```
|
101
125
|
|
102
126
|
## Development
|
data/lib/static_models.rb
CHANGED
@@ -17,17 +17,9 @@ module StaticModels
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def to_s
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def to_i
|
25
|
-
id
|
26
|
-
end
|
27
|
-
|
28
|
-
def name
|
29
|
-
code
|
30
|
-
end
|
20
|
+
def to_s; code.to_s; end
|
21
|
+
def to_i; id; end
|
22
|
+
def name; code; end
|
31
23
|
end
|
32
24
|
|
33
25
|
class_methods do
|
@@ -74,18 +66,49 @@ module StaticModels
|
|
74
66
|
extend ActiveSupport::Concern
|
75
67
|
|
76
68
|
class_methods do
|
77
|
-
def
|
78
|
-
|
69
|
+
def belongs_to(association, opts = {})
|
70
|
+
super(association, opts) if defined?(super)
|
71
|
+
|
72
|
+
define_method("#{association}") do
|
73
|
+
klass_name = if opts[:polymorphic]
|
74
|
+
send("#{association}_type")
|
75
|
+
else
|
76
|
+
(opts[:class_name] || association.to_s.camelize)
|
77
|
+
end
|
79
78
|
|
80
|
-
|
81
|
-
|
79
|
+
return nil unless Object.const_defined?(klass_name)
|
80
|
+
|
81
|
+
klass = klass_name.constantize
|
82
|
+
|
83
|
+
if klass.include?(Model)
|
84
|
+
klass.find(send("#{association}_id"))
|
85
|
+
else
|
86
|
+
super()
|
87
|
+
end
|
82
88
|
end
|
83
89
|
|
84
|
-
define_method("#{
|
85
|
-
unless
|
86
|
-
|
90
|
+
define_method("#{association}=") do |value|
|
91
|
+
unless opts[:polymorphic]
|
92
|
+
expected = [opts[:class_name], association.to_s.camelize].compact
|
93
|
+
got = value.class.name
|
94
|
+
unless expected.include?(got)
|
95
|
+
raise TypeError.new("Expected #{expected.first} got #{got}")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
if value.class.include?(Model)
|
100
|
+
if opts[:polymorphic]
|
101
|
+
# This next line resets the old polymorphic association
|
102
|
+
# if it was set to an ActiveRecord::Model. Otherwise
|
103
|
+
# ActiveRecord will get confused and ask for our StaticModel's
|
104
|
+
# table and a bunch of other things that don't apply.
|
105
|
+
super(nil) if defined?(super)
|
106
|
+
send("#{association}_type=", value.class.name )
|
107
|
+
end
|
108
|
+
send("#{association}_id=", value && value.id)
|
109
|
+
else
|
110
|
+
super(value)
|
87
111
|
end
|
88
|
-
send("#{attr_name}_id=", value && value.id)
|
89
112
|
end
|
90
113
|
end
|
91
114
|
end
|
data/static_models.gemspec
CHANGED
@@ -29,4 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_development_dependency "bundler", "~> 1.13"
|
30
30
|
spec.add_development_dependency "rake", "~> 10.0"
|
31
31
|
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
spec.add_development_dependency "activerecord", '~> 4.2', '>= 4.2.0'
|
33
|
+
spec.add_development_dependency "sqlite3", "~> 0"
|
32
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nubis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -72,6 +72,40 @@ dependencies:
|
|
72
72
|
- - "~>"
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '3.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: activerecord
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '4.2'
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 4.2.0
|
85
|
+
type: :development
|
86
|
+
prerelease: false
|
87
|
+
version_requirements: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - "~>"
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '4.2'
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 4.2.0
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: sqlite3
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - "~>"
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
75
109
|
description: "\n Replace your key/value classes with this.\n Define classes
|
76
110
|
with several 'singleton' instances.\n "
|
77
111
|
email:
|