activeoopish 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/activeoopish/inheritance.rb +1 -4
- data/lib/activeoopish/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7ea031470768663369ee9f4274d678222ac5cb9
|
4
|
+
data.tar.gz: f39b72f1dfa6af281f0004d5c48880138e449056
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94bf53a48e6a42e778512ff8ce45bac4c1678f3bd11274860163d14c19e41ec59819b2f1a013f898d37e57f471dc95f162c5826f7b2ddaaf41ed7dad55dae728
|
7
|
+
data.tar.gz: 414ac20e021fea8ef5390ab9341807e8d495d62aacc0534ee95d419a6a5487eeace25bf51ef6436e190b02a39a7c0888d7ca127f382708ad98fa9c4ca97123bd
|
data/README.md
CHANGED
@@ -4,6 +4,27 @@
|
|
4
4
|
|
5
5
|
Simple tools for better OOP in Rails projects.
|
6
6
|
|
7
|
+
## ActiveOopish::Inheritance
|
8
|
+
|
9
|
+
Instantiate a model instance as sub class's instance based on records value.
|
10
|
+
|
11
|
+
```rb
|
12
|
+
class Book < ActiveRecord::Base
|
13
|
+
include ActiveOopish::Inheritance
|
14
|
+
|
15
|
+
# If the record whose 'category_code' column equals `BIOGRAPHY` is fetched from
|
16
|
+
# the database, it will be instantiated as Biography class instance.
|
17
|
+
instantiate_as 'Biography', category_code: BIOGRAPHY
|
18
|
+
end
|
19
|
+
|
20
|
+
class Biography < Book
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
### Why not STI?
|
25
|
+
|
26
|
+
Single table inheritance (STI) requires a string column which stores the instance's class name, but it is hard to prepare such thing in some cases. For example suppose your project came from another WAP to Rails, it may already have running DB and it does not satisfy the requirement by default. In addition, these columns are too Rails specific.
|
27
|
+
|
7
28
|
## ActiveOopish::Validator
|
8
29
|
|
9
30
|
Encapsulates the responsibility of validating a model into a validator.
|
@@ -4,10 +4,6 @@ module ActiveOopish
|
|
4
4
|
module Inheritance
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
included do
|
8
|
-
instance_variable_set('@instantiation_rules', [])
|
9
|
-
end
|
10
|
-
|
11
7
|
module ClassMethods
|
12
8
|
# Public:
|
13
9
|
#
|
@@ -25,6 +21,7 @@ module ActiveOopish
|
|
25
21
|
#
|
26
22
|
# Returns a Class to instantiate an instance.
|
27
23
|
def discriminate_class_for_record(record)
|
24
|
+
@instantiation_rules ||= []
|
28
25
|
@instantiation_rules.each do |rule|
|
29
26
|
satisfy_rule = rule[:condition].each_pair.all? do |column, expected|
|
30
27
|
record[column] == expected
|
data/lib/activeoopish/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeoopish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuku Takahashi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|