activeoopish 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +11 -6
- data/activeoopish.gemspec +2 -2
- data/lib/activeoopish/inheritance.rb +38 -0
- data/lib/activeoopish/rspec_helper.rb +3 -3
- data/lib/activeoopish/validator.rb +3 -3
- data/lib/activeoopish/version.rb +2 -2
- data/lib/activeoopish.rb +2 -1
- data/spec/activeoopish/rspec_helper_spec.rb +2 -2
- data/spec/activeoopish/validator_spec.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 650b235e105ff9d1e787a92b7c71415f66cc810a
|
4
|
+
data.tar.gz: b7f3d38032ee7ecc1d2f100d542ad1d6a4095163
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6efd69a0876b47abbfddc1df64dbf96cd7a5f3bbe40e1dc74ee8bb964d3d21706a6a604107e05691eb17a994e33e233c9a3f49061b246ade2f3deedf91c14c9f
|
7
|
+
data.tar.gz: 0b8509f75c81fe3bdd01ab15c685c6e0a7ffa26286807627146109defa18fbdcdd5810db2464dbde5fc28ce81e0a10c9f1b9c091dc6a81a7eb80a44290c001fa
|
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# AvtiveOopish
|
2
2
|
|
3
3
|
[](https://travis-ci.org/yuku-t/activeoopish) [](https://codeclimate.com/github/yuku-t/activeoopish) [](https://coveralls.io/r/yuku-t/activeoopish) [](https://gemnasium.com/yuku-t/activeoopish)
|
4
4
|
|
5
5
|
Simple tools for better OOP in Rails projects.
|
6
6
|
|
7
|
-
##
|
7
|
+
## ActiveOopish::Validator
|
8
8
|
|
9
9
|
Encapsulates the responsibility of validating a model into a validator.
|
10
10
|
|
11
11
|
```rb
|
12
|
-
class BookValidator <
|
12
|
+
class BookValidator < ActiveOopish::Validator
|
13
13
|
declear do
|
14
14
|
validates :author, presence: true
|
15
15
|
|
@@ -39,10 +39,14 @@ end
|
|
39
39
|
### RSpec
|
40
40
|
|
41
41
|
```rb
|
42
|
-
require 'activeoopish/
|
42
|
+
require 'activeoopish/rspec_helper'
|
43
43
|
require 'shoulda-matchers'
|
44
44
|
|
45
|
-
|
45
|
+
describe Book do
|
46
|
+
it { should be_monitored_by BookValidator }
|
47
|
+
end
|
48
|
+
|
49
|
+
describe BookValidator, :with_activeoopish_helper do
|
46
50
|
include_context 'describe declaration' do
|
47
51
|
it { should validate_presence_of(:author) }
|
48
52
|
|
@@ -50,7 +54,8 @@ declear BookValidator, :with_activeoopish_helper do
|
|
50
54
|
|
51
55
|
context 'when #biography? returns true' do
|
52
56
|
before do
|
53
|
-
allow_any_instance_of(described_class)
|
57
|
+
allow_any_instance_of(described_class)
|
58
|
+
.to receive(:biography?).and_return(true)
|
54
59
|
end
|
55
60
|
|
56
61
|
it 'calls #title_must_include_author_name' do
|
data/activeoopish.gemspec
CHANGED
@@ -4,11 +4,11 @@ require "activeoopish/version"
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'activeoopish'
|
7
|
-
spec.version =
|
7
|
+
spec.version = ActiveOopish::VERSION
|
8
8
|
spec.authors = ['Yuku Takahashi']
|
9
9
|
spec.email = ['yuku@qiita.com']
|
10
10
|
spec.summary = 'Simple OOP-ish tools for Rails'
|
11
|
-
spec.homepage = 'https://github.com/
|
11
|
+
spec.homepage = 'https://github.com/yuku-t/activeoopish'
|
12
12
|
spec.license = 'MIT'
|
13
13
|
|
14
14
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module ActiveOopish
|
4
|
+
module Inheritance
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
instance_variable_set('@instantiation_rules', [])
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
# Public:
|
13
|
+
#
|
14
|
+
# class_name - A String represents the class which instantiates an instance.
|
15
|
+
# condition - A Hash with :when key.
|
16
|
+
#
|
17
|
+
def instantiate_as(class_name, options = {})
|
18
|
+
@instantiation_rules << { class_name: class_name, condition: options[:when].stringify_keys }
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# Internal: Called by ActiveRecord::Persistence.instantiate to decide which
|
24
|
+
# class to use for a new record instance.
|
25
|
+
#
|
26
|
+
# Returns a Class to instantiate an instance.
|
27
|
+
def discriminate_class_for_record(record)
|
28
|
+
@instantiation_rules.each do |rule|
|
29
|
+
satisfy_rule = rule[:condition].each_pair.all? do |column, expected|
|
30
|
+
record[column] == expected
|
31
|
+
end
|
32
|
+
return rule[:class_name].constantize if satisfy_rule
|
33
|
+
end
|
34
|
+
self
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -9,14 +9,14 @@ RSpec::Matchers.define :be_monitored_by do |validator_class|
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
module
|
12
|
+
module ActiveOopish
|
13
13
|
module RSpecHelper
|
14
14
|
module SharedContext
|
15
15
|
extend ActiveSupport::Concern
|
16
16
|
|
17
17
|
included do
|
18
18
|
let(:model_class) do
|
19
|
-
Class.new(
|
19
|
+
Class.new(ActiveOopish::RSpecHelper::ValidationTarget) do
|
20
20
|
def self.name
|
21
21
|
'ValidationTarget'
|
22
22
|
end
|
@@ -62,5 +62,5 @@ module ActiveOOPish
|
|
62
62
|
end
|
63
63
|
|
64
64
|
RSpec.configure do |config|
|
65
|
-
config.include
|
65
|
+
config.include ActiveOopish::RSpecHelper::SharedContext, :with_activeoopish_helpers
|
66
66
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'active_support/core_ext/module/delegation'
|
2
2
|
|
3
|
-
module
|
3
|
+
module ActiveOopish
|
4
4
|
# Public: Base class for validators.
|
5
5
|
#
|
6
6
|
# Example
|
7
7
|
#
|
8
|
-
# class BookValidator <
|
8
|
+
# class BookValidator < ActiveOopish::Validator
|
9
9
|
# declear do
|
10
10
|
# validates :author, presence: true
|
11
11
|
# validate :title_must_include_author_name, if: :biography?
|
@@ -41,7 +41,7 @@ module ActiveOOPish
|
|
41
41
|
# # => "author cannot write a biography of another person"
|
42
42
|
#
|
43
43
|
class Validator
|
44
|
-
# Public: Generic
|
44
|
+
# Public: Generic ActiveOopish::Validator related error.
|
45
45
|
# Exceptions raised in this class should inherit from Error.
|
46
46
|
class Error < StandardError
|
47
47
|
end
|
data/lib/activeoopish/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = '0.0.
|
1
|
+
module ActiveOopish
|
2
|
+
VERSION = '0.0.2'
|
3
3
|
end
|
data/lib/activeoopish.rb
CHANGED
@@ -3,7 +3,7 @@ require 'activeoopish/rspec_helper'
|
|
3
3
|
describe 'activeoopish matchers' do
|
4
4
|
describe 'be_monitored_by matcher', :with_activeoopish_helpers do
|
5
5
|
let(:validator_class) do
|
6
|
-
Class.new(
|
6
|
+
Class.new(ActiveOopish::Validator) do
|
7
7
|
declear do
|
8
8
|
end
|
9
9
|
|
@@ -45,7 +45,7 @@ describe 'activeoopish matchers' do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
class SampleValidator <
|
48
|
+
class SampleValidator < ActiveOopish::Validator
|
49
49
|
declear do
|
50
50
|
validates(
|
51
51
|
:attr,
|
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.2
|
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-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -109,13 +109,14 @@ files:
|
|
109
109
|
- Rakefile
|
110
110
|
- activeoopish.gemspec
|
111
111
|
- lib/activeoopish.rb
|
112
|
+
- lib/activeoopish/inheritance.rb
|
112
113
|
- lib/activeoopish/rspec_helper.rb
|
113
114
|
- lib/activeoopish/validator.rb
|
114
115
|
- lib/activeoopish/version.rb
|
115
116
|
- spec/activeoopish/rspec_helper_spec.rb
|
116
117
|
- spec/activeoopish/validator_spec.rb
|
117
118
|
- spec/spec_helper.rb
|
118
|
-
homepage: https://github.com/
|
119
|
+
homepage: https://github.com/yuku-t/activeoopish
|
119
120
|
licenses:
|
120
121
|
- MIT
|
121
122
|
metadata: {}
|