safe_finder 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/Gemfile +7 -0
- data/README.md +106 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/safe_finder/class_wrapper.rb +16 -0
- data/lib/safe_finder/null_object.rb +5 -0
- data/lib/safe_finder/null_object_generator.rb +61 -0
- data/lib/safe_finder/version.rb +3 -0
- data/lib/safe_finder.rb +35 -0
- data/safe_finder.gemspec +36 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7888a25bfecaee45bf6a1ae510d17f488b195573
|
4
|
+
data.tar.gz: 5d3dd4b94fabf2d2b3978ae4e74348955c12e4e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1c08c62dfee492ff309f3d029e85cb0b31541d83f18aca392c068ad7d31615c6608534efdd1795768772e0a247371e33a4792d9817a07a8bf2016c5b1e4eb7b
|
7
|
+
data.tar.gz: 3585c85b192eb6b42603948b5ddf5cb843934b50864c347f4b6b1e6d749b428aba8870f1b8f9bac31740de7d869e0287ca45c6d9c75790b891c651fb066fdacb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/st0012/SafeFinder.svg)](https://travis-ci.org/st0012/SafeFinder)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/st0012/SafeFinder/badges/gpa.svg)](https://codeclimate.com/github/st0012/SafeFinder)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/st0012/SafeFinder/badges/coverage.svg)](https://codeclimate.com/github/st0012/SafeFinder/coverage)
|
4
|
+
# SafeFinder
|
5
|
+
|
6
|
+
SafeFinder let you define model's null_object through simple DSL, and returns it when you can't find a result.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'safe_finder'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install safe_finder
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Basic
|
27
|
+
Let's say you have a `Post` class, and it has `title` and `content` column.
|
28
|
+
|
29
|
+
First, you need to include `SafeFinder` in your model:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Post < ActiveRecord::Base
|
33
|
+
include SafeFinder
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Now you can query like this, but it doesn't find anything:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
# It returns a null_object
|
41
|
+
null_object = Post.safely.find_by_title("New Post")
|
42
|
+
|
43
|
+
null_object.class # NullPost
|
44
|
+
null_object.title # nil
|
45
|
+
null_object.content # nil
|
46
|
+
```
|
47
|
+
|
48
|
+
### Custom Attribute & Method
|
49
|
+
|
50
|
+
And you can custom null_object's attribute or method using DSL:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Post < ActiveRecord::Base
|
54
|
+
include SafeFinder
|
55
|
+
|
56
|
+
safe_attribute :title, "Null"
|
57
|
+
safe_method :some_method do
|
58
|
+
"Do Something"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
### Get NullObject directly
|
64
|
+
|
65
|
+
Just simply use:
|
66
|
+
```ruby
|
67
|
+
Post.null_object => #<NullPost:0x007fa8a4713be0>
|
68
|
+
```
|
69
|
+
|
70
|
+
### Inheritance
|
71
|
+
|
72
|
+
All null_object inherits `SafeFinder::NullObject`, so you can add it in
|
73
|
+
|
74
|
+
```
|
75
|
+
app/models/safe_finder/null_object.rb
|
76
|
+
```
|
77
|
+
|
78
|
+
to define general methods for every null_object
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
module SafeFinder
|
82
|
+
class NullObject
|
83
|
+
def hello
|
84
|
+
"Hello"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
|
91
|
+
## TODOs
|
92
|
+
|
93
|
+
- Add association support, like `user.post` should also returns null_object when it's nil
|
94
|
+
- Add generator for `NullObject` model
|
95
|
+
- More use cases in readme
|
96
|
+
|
97
|
+
## Development
|
98
|
+
|
99
|
+
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.
|
100
|
+
|
101
|
+
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).
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/st0012/SafeFinder.
|
106
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "safe_finder"
|
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,16 @@
|
|
1
|
+
module SafeFinder
|
2
|
+
class ClassWrapper
|
3
|
+
attr_reader :wrapped_class
|
4
|
+
|
5
|
+
def initialize(target_class)
|
6
|
+
@wrapped_class = target_class
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(name, *arguments, &block)
|
10
|
+
result = wrapped_class.send(name, *arguments, &block)
|
11
|
+
result.nil? ? wrapped_class.null_object : result
|
12
|
+
rescue ActiveRecord::RecordNotFound => e
|
13
|
+
wrapped_class.null_object
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module SafeFinder
|
2
|
+
class NullObjectGenerator
|
3
|
+
attr_reader :original_class, :original_class_name, :null_class, :setted_columns
|
4
|
+
|
5
|
+
def initialize(original_class)
|
6
|
+
@original_class = original_class
|
7
|
+
@original_class_name = original_class.to_s
|
8
|
+
@null_class = create_null_class
|
9
|
+
@setted_columns = original_class.null_object_attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate
|
13
|
+
set_attributes
|
14
|
+
set_methods
|
15
|
+
null_class.new
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def create_null_class
|
21
|
+
Object.const_set("Null#{original_class_name}", Class.new(NullObject))
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_attributes
|
25
|
+
original_class.columns.inject({}) do |result, column|
|
26
|
+
result[column.name] = get_value(column)
|
27
|
+
result
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# I want to support default value here.
|
32
|
+
# But column object's default value is string, and needs some mechanism be to converted into ruby's type
|
33
|
+
def get_value(column)
|
34
|
+
if value = setted_columns[column.name.to_sym]
|
35
|
+
value
|
36
|
+
else
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_methods
|
42
|
+
methods = original_class.null_object_methods
|
43
|
+
null_class.class_eval do
|
44
|
+
methods.each do |key, value|
|
45
|
+
define_method key, value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_attributes
|
51
|
+
attributes = get_attributes
|
52
|
+
null_class.class_eval do
|
53
|
+
attributes.each do |key, value|
|
54
|
+
define_method key do
|
55
|
+
value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/safe_finder.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "safe_finder/null_object_generator"
|
2
|
+
require "safe_finder/null_object"
|
3
|
+
require "safe_finder/class_wrapper"
|
4
|
+
require "safe_finder/version"
|
5
|
+
|
6
|
+
module SafeFinder
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
base.singleton_class.class_eval do
|
10
|
+
attr_accessor :null_object_attributes, :null_object_methods
|
11
|
+
end
|
12
|
+
base.class_eval do
|
13
|
+
self.null_object_attributes = {}
|
14
|
+
self.null_object_methods = {}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def safely
|
20
|
+
ClassWrapper.new(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
def null_object
|
24
|
+
@null_object ||= SafeFinder::NullObjectGenerator.new(self).generate
|
25
|
+
end
|
26
|
+
|
27
|
+
def safe_attribute(key, value)
|
28
|
+
null_object_attributes[key.to_sym] = value
|
29
|
+
end
|
30
|
+
|
31
|
+
def safe_method(method_name, &block)
|
32
|
+
null_object_methods[method_name.to_sym] = block
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/safe_finder.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'safe_finder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "safe_finder"
|
8
|
+
spec.version = SafeFinder::VERSION
|
9
|
+
spec.authors = ["Stan Loe"]
|
10
|
+
spec.email = ["stan001212@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{SafeFinder lets you define model's null_object through simple DSL, and returns it when you can't find a result.}
|
13
|
+
spec.description = %q{SafeFinder lets you define model's null_object through simple DSL, and returns it when you can't find a result.}
|
14
|
+
spec.homepage = "https://github.com/st0012/SafeFinder"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
# end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler"
|
30
|
+
spec.add_development_dependency "database_cleaner"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec"
|
33
|
+
spec.add_development_dependency "sqlite3"
|
34
|
+
|
35
|
+
spec.add_dependency "activerecord", ">= 4.0"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: safe_finder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stan Loe
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: database_cleaner
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.0'
|
97
|
+
description: SafeFinder lets you define model's null_object through simple DSL, and
|
98
|
+
returns it when you can't find a result.
|
99
|
+
email:
|
100
|
+
- stan001212@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/safe_finder.rb
|
114
|
+
- lib/safe_finder/class_wrapper.rb
|
115
|
+
- lib/safe_finder/null_object.rb
|
116
|
+
- lib/safe_finder/null_object_generator.rb
|
117
|
+
- lib/safe_finder/version.rb
|
118
|
+
- safe_finder.gemspec
|
119
|
+
homepage: https://github.com/st0012/SafeFinder
|
120
|
+
licenses: []
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.4.5
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: SafeFinder lets you define model's null_object through simple DSL, and returns
|
142
|
+
it when you can't find a result.
|
143
|
+
test_files: []
|
144
|
+
has_rdoc:
|