findable 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -8
- data/Gemfile +1 -1
- data/README.md +66 -14
- data/Rakefile +6 -1
- data/findable.gemspec +3 -2
- data/lib/findable.rb +2 -3
- data/lib/findable/associations.rb +52 -0
- data/lib/findable/associations/active_record_ext.rb +87 -0
- data/lib/findable/associations/utils.rb +25 -0
- data/lib/findable/base.rb +140 -80
- data/lib/findable/configuration.rb +3 -1
- data/lib/findable/errors.rb +8 -2
- data/lib/findable/query.rb +91 -0
- data/lib/findable/query/connection.rb +26 -0
- data/lib/findable/query/namespace.rb +34 -0
- data/lib/findable/railtie.rb +1 -1
- data/lib/findable/seed.rb +34 -38
- data/lib/findable/version.rb +1 -1
- data/lib/generators/findable/install_generator.rb +5 -0
- data/lib/generators/findable/templates/findable.rb +5 -2
- data/lib/generators/findable/templates/seeds.rb +1 -4
- data/spec/spec_helper.rb +1 -13
- data/spec/support/initialize_redis.rb +2 -0
- metadata +13 -10
- data/lib/findable/association.rb +0 -71
- data/lib/findable/connection.rb +0 -25
- data/lib/findable/namespace.rb +0 -23
- data/lib/findable/recordable.rb +0 -93
- data/lib/findable/serializer.rb +0 -12
data/lib/findable/association.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
module Findable
|
2
|
-
module Association
|
3
|
-
def has_many(name, scope = nil, options = {})
|
4
|
-
super unless _define_association_methods(:has_many, name, options)
|
5
|
-
end
|
6
|
-
|
7
|
-
def has_one(name, scope = nil, options = {})
|
8
|
-
super unless _define_association_methods(:has_one, name, options)
|
9
|
-
end
|
10
|
-
|
11
|
-
def belongs_to(name, scope = nil, options = {})
|
12
|
-
super unless _define_association_methods(:belongs_to, name, options)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
def _define_association_methods(association_type, name, options)
|
17
|
-
model = _model_for(name, options, true)
|
18
|
-
if _findable?(model) || _findable?(self)
|
19
|
-
self.send("_define_findable_#{association_type}", name, options, model)
|
20
|
-
else
|
21
|
-
false
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def _model_for(name, options, _raise = false)
|
26
|
-
class_name = _class_name_for(name, options)
|
27
|
-
begin
|
28
|
-
class_name.constantize
|
29
|
-
rescue => e
|
30
|
-
_raise ? (raise Findable::ModelNotFound.new(class_name)) : nil
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def _class_name_for(name, options)
|
35
|
-
options[:class].presence || name.to_s.classify
|
36
|
-
end
|
37
|
-
|
38
|
-
def _foreign_key_for(name, options)
|
39
|
-
options[:foreign_key].presence || name.to_s.foreign_key
|
40
|
-
end
|
41
|
-
|
42
|
-
def _findable?(model)
|
43
|
-
model.ancestors.include?(Findable::Base)
|
44
|
-
end
|
45
|
-
|
46
|
-
def _define_findable_has_many(name, options, model)
|
47
|
-
foreign_key = _foreign_key_for(self.model_name.name, options)
|
48
|
-
|
49
|
-
define_method name do
|
50
|
-
model.where(foreign_key.to_sym => id)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def _define_findable_has_one(name, options, model)
|
55
|
-
foreign_key = _foreign_key_for(self.model_name.name, options)
|
56
|
-
|
57
|
-
define_method name do
|
58
|
-
model.find_by(foreign_key.to_sym => id)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def _define_findable_belongs_to(name, options, model)
|
63
|
-
foreign_key = _foreign_key_for(name, options)
|
64
|
-
|
65
|
-
define_method name do
|
66
|
-
model.find(self.send(foreign_key))
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
data/lib/findable/connection.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Findable
|
2
|
-
module Connection
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def redis
|
7
|
-
@_redis ||= generate_redis_connection!
|
8
|
-
end
|
9
|
-
|
10
|
-
private
|
11
|
-
def generate_redis_connection!
|
12
|
-
redis_options ? Redis.new(*redis_options) : Redis.current
|
13
|
-
end
|
14
|
-
|
15
|
-
def redis_options
|
16
|
-
Findable.config.redis_options.presence
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def redis
|
21
|
-
self.class.redis
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
data/lib/findable/namespace.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Findable
|
2
|
-
module Namespace
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def info_key
|
7
|
-
namespace[:info]
|
8
|
-
end
|
9
|
-
|
10
|
-
def data_key
|
11
|
-
namespace[:data]
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
def namespace
|
16
|
-
%i(info data).each_with_object({}) do |name, obj|
|
17
|
-
obj[name] = [self.model_name.plural, name].join(":")
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
data/lib/findable/recordable.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
module Findable
|
2
|
-
module Recordable
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
include Serializer
|
5
|
-
|
6
|
-
included do
|
7
|
-
include ActiveModel::AttributeMethods
|
8
|
-
|
9
|
-
attribute_method_suffix "="
|
10
|
-
attribute_method_suffix "?"
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
def field(attr, options={})
|
15
|
-
options.symbolize_keys!
|
16
|
-
define_accessor(attr.to_sym, options)
|
17
|
-
end
|
18
|
-
|
19
|
-
def fields(*args)
|
20
|
-
options = args.extract_options!
|
21
|
-
args.each {|arg| field(arg, options) }
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
def define_accessor(attr, options)
|
26
|
-
unless public_method_defined?(attr)
|
27
|
-
define_attribute_methods attr
|
28
|
-
define_method attr do
|
29
|
-
attributes[attr]
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def initialize(params={})
|
36
|
-
params = deserialize(params) if params.is_a?(String)
|
37
|
-
params.symbolize_keys!
|
38
|
-
params.keys.each {|attr| self.class.field(attr) }
|
39
|
-
@_attributes = params
|
40
|
-
end
|
41
|
-
|
42
|
-
def id
|
43
|
-
attributes[:id].presence || nil
|
44
|
-
end
|
45
|
-
# alias_method :quoted_id, :id
|
46
|
-
|
47
|
-
def id=(_id)
|
48
|
-
attributes[:id] = _id
|
49
|
-
end
|
50
|
-
|
51
|
-
def save
|
52
|
-
self.class.insert(self)
|
53
|
-
end
|
54
|
-
alias_method :save!, :save
|
55
|
-
|
56
|
-
def delete
|
57
|
-
self.class.delete(id)
|
58
|
-
end
|
59
|
-
alias_method :destroy, :delete
|
60
|
-
|
61
|
-
def new_record?
|
62
|
-
id ? !self.class.exists?(self) : true
|
63
|
-
end
|
64
|
-
|
65
|
-
def persisted?
|
66
|
-
!new_record?
|
67
|
-
end
|
68
|
-
|
69
|
-
def to_json(methods: nil)
|
70
|
-
_attrs = attributes.dup
|
71
|
-
_attrs.merge!(methods.to_sym => self.send(methods)) if methods
|
72
|
-
serialize(_attrs)
|
73
|
-
end
|
74
|
-
|
75
|
-
def hash
|
76
|
-
id.hash
|
77
|
-
end
|
78
|
-
|
79
|
-
def attributes
|
80
|
-
@_attributes ||= {}
|
81
|
-
end
|
82
|
-
|
83
|
-
private
|
84
|
-
def attribute=(attr, val)
|
85
|
-
attributes[attr.to_sym] = val
|
86
|
-
end
|
87
|
-
|
88
|
-
def attribute?(attr)
|
89
|
-
attributes[attr.to_sym].present?
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|