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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a2114fbd9afdf72c4e55bde93c734dd864d998f
4
- data.tar.gz: 2abc9dbb758e7353028d9af080ee5a45c32514e3
3
+ metadata.gz: b7ea031470768663369ee9f4274d678222ac5cb9
4
+ data.tar.gz: f39b72f1dfa6af281f0004d5c48880138e449056
5
5
  SHA512:
6
- metadata.gz: 240567d5706da9de714ce682c693a1188ac4532a371f8b5a53a9ebbd3c771c02f5a62a4dd785c5cf64611f3b858c8fcf152196f9dbd028dae0d5c1e81efcf5e7
7
- data.tar.gz: 421c47a19154061669920ab601b98e078e9705ecba6bb89d5d21fbf88791b6769ff8ebb0c8cf852b0286a4b321ce25ec4feb62045803db52813d75089527f093
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
@@ -1,3 +1,3 @@
1
1
  module ActiveOopish
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
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.3
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-19 00:00:00.000000000 Z
11
+ date: 2015-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport