findable 0.0.1 → 0.0.2

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: cd0c4843a980a02496e5e1b55e9466bd8755e968
4
- data.tar.gz: e29742dc9478c09cccc03f868632b87abb367f66
3
+ metadata.gz: deb377ce8ee9a4cdd4f24eb61de229ca8f8df2fa
4
+ data.tar.gz: aafb0d2d0754688fa4622c948b0e2d9ab424ca8b
5
5
  SHA512:
6
- metadata.gz: 36948d3d3c45ff724706eeda3a35a368b83de3ebfe864c373b757ddadbae7467b4adce1ee2b55268ef2dd45b9f6db34d46a1472488ce4b5c3f40219f64deee11
7
- data.tar.gz: 9d6d5d7e35b5cac170372ea0f549a82223aaec5cd33ffc02d74248766ca9ef5ecab66962255408e051ba7944a76489a9ede08d85015c4a5ec8451dfee8ebf548
6
+ metadata.gz: b51c0a458b8b46f64217d51e51d689925b9248859a242531ff8a4a90206f545d9c71443bb23e4f8de3489c4bd23f233177203e0eff98a939e8a126510f3dda41
7
+ data.tar.gz: 7b44e407f4e839086b1f3c828f9237415d7199219604164c9515f79db6a5a04df81e768039fe4613dcce3bd8e4cc2ed2a4f6832bab59dcc9b3e211592846810b
@@ -0,0 +1,2 @@
1
+ repo_token: fbWiPxpNWcR8fOyueZ7IWw1pMbl20ZvRN
2
+
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.2.0
5
+ branches:
6
+ only:
7
+ - master
8
+ gemfile:
9
+ - Gemfile
10
+ script: bundle exec rake spec
11
+ notifications:
12
+ mails:
13
+ - i2bskn@gmail.com
14
+
data/README.md CHANGED
@@ -1,26 +1,45 @@
1
1
  # Findable
2
2
 
3
- TODO: Write a gem description
3
+ [![Gem Version](https://badge.fury.io/rb/findable.svg)](http://badge.fury.io/rb/findable)
4
+ [![Build Status](https://travis-ci.org/i2bskn/findable.svg)](https://travis-ci.org/i2bskn/findable)
5
+ [![Coverage Status](https://img.shields.io/coveralls/i2bskn/findable.svg)](https://coveralls.io/r/i2bskn/findable)
6
+ [![Code Climate](https://codeclimate.com/github/i2bskn/findable/badges/gpa.svg)](https://codeclimate.com/github/i2bskn/findable)
7
+
8
+ Redis wrapper with API like ActiveRecord. (While creating...)
4
9
 
5
10
  ## Installation
6
11
 
7
12
  Add this line to your application's Gemfile:
8
13
 
9
14
  ```ruby
10
- gem 'findable'
15
+ gem "findable"
11
16
  ```
12
17
 
13
18
  And then execute:
14
19
 
15
20
  $ bundle
16
21
 
17
- Or install it yourself as:
18
-
19
- $ gem install findable
20
-
21
22
  ## Usage
22
23
 
23
- TODO: Write usage instructions here
24
+ ```ruby
25
+ class Company < ActiveRecord::Base
26
+ has_many :person
27
+ end
28
+
29
+ class Person < Findable::Base
30
+ fields :name, :email, :gender, :company_id
31
+ belongs_to :company
32
+ end
33
+
34
+ person = Person.new(name: "Ken Iiboshi", gender: "male")
35
+ person.email = "i2bskn@example.com"
36
+ person.save
37
+
38
+ people = Person.where(gender: "male")
39
+ people.each do |person|
40
+ puts person.name
41
+ end
42
+ ```
24
43
 
25
44
  ## Contributing
26
45
 
@@ -7,8 +7,8 @@ Gem::Specification.new do |spec|
7
7
  spec.version = Findable::VERSION
8
8
  spec.authors = ["i2bskn"]
9
9
  spec.email = ["i2bskn@gmail.com"]
10
- spec.summary = %q{Static model to behave like ActiveRecord.}
11
- spec.description = %q{Static model to behave like ActiveRecord.}
10
+ spec.summary = %q{Redis wrapper with API like ActiveRecord.}
11
+ spec.description = %q{Redis wrapper with API like ActiveRecord.}
12
12
  spec.homepage = "https://github.com/i2bskn/findable"
13
13
  spec.license = "MIT"
14
14
 
@@ -22,8 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "redis"
23
23
  spec.add_dependency "oj"
24
24
 
25
- spec.add_development_dependency "bundler", "~> 1.7"
26
- spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "rspec-rails"
28
28
  spec.add_development_dependency "simplecov"
29
29
  spec.add_development_dependency "coveralls"
@@ -7,6 +7,7 @@ require "findable/version"
7
7
  require "findable/errors"
8
8
  require "findable/configuration"
9
9
  require "findable/serializer"
10
+ require "findable/association"
10
11
  require "findable/base"
11
12
  require "findable/railtie" if defined?(Rails)
12
13
 
@@ -0,0 +1,71 @@
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
+
@@ -0,0 +1,5 @@
1
+ module Findable
2
+ module Association
3
+ end
4
+ end
5
+
@@ -1,5 +1,11 @@
1
1
  module Findable
2
2
  class FindableError < StandardError; end
3
3
  class RecordNotFound < FindableError; end
4
+
5
+ class ModelNotFound < FindableError
6
+ def initialize(model_name)
7
+ super("#{model_name} not found!")
8
+ end
9
+ end
4
10
  end
5
11
 
@@ -1,5 +1,11 @@
1
1
  module Findable
2
2
  class Railtie < ::Rails::Railtie
3
+ initializer "findable" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ ::ActiveRecord::Base.send(:extend, Findable::Association)
6
+ end
7
+ end
8
+
3
9
  rake_tasks do
4
10
  load "tasks/findable.rake"
5
11
  end
@@ -0,0 +1,61 @@
1
+ module Findable
2
+ class Seed
3
+ attr_reader :full_path, :namespaced, :ext
4
+
5
+ def initialize(path, seed_dir)
6
+ @_seed_dir = seed_dir
7
+ self.full_path = path
8
+ end
9
+
10
+ def full_path=(path)
11
+ @full_path = path
12
+ _path = path.gsub(@_seed_dir, "")
13
+ @namespaced = /^\// =~ _path ? _path.from(1) : _path
14
+ @ext = @namespaced[/^.*(?<ext>\.[^\.]+)$/, :ext] || (raise ArgumentError)
15
+ end
16
+
17
+ def base_name
18
+ @_base ||= @namespaced.sub(@ext, "")
19
+ end
20
+
21
+ def model
22
+ base_name.split("/").reverse.map.with_index {|n,i|
23
+ i.zero? ? n.singularize : n
24
+ }.reverse.map(&:camelize).join("::").constantize
25
+ end
26
+
27
+ def load_file
28
+ case @ext
29
+ when ".yml"
30
+ YAML.load_file(@full_path).values
31
+ else
32
+ raise UnexpectedFormat
33
+ end
34
+ end
35
+
36
+ def bootstrap!
37
+ model.transaction do
38
+ model.delete_all
39
+ records = load_file.map {|data| model.new(data) }
40
+ model.import records
41
+ end
42
+ end
43
+
44
+ class << self
45
+ def target_files(seed_dir, tables = nil)
46
+ Dir.glob(patterns(seed_dir)).map {|full_path|
47
+ Seed.new(full_path, seed_dir)
48
+ }.select {|seed|
49
+ tables ? tables.include?(seed.base_name) : true
50
+ }
51
+ end
52
+
53
+ def patterns(seed_dir)
54
+ %w(yml).map {|format|
55
+ Rails.root.join("#{seed_dir}/**/*.#{format}").to_s
56
+ }
57
+ end
58
+ end
59
+ end
60
+ end
61
+
@@ -1,3 +1,3 @@
1
1
  module Findable
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,2 +1,17 @@
1
1
  # Seed file of Findable
2
2
 
3
+ # Require seed support module
4
+ require "findable/seed"
5
+
6
+ # Path to the reading of the seed.
7
+ seed_dir = File.expand_path("../findable_seeds", __FILE__)
8
+
9
+ # Target seed files.
10
+ # Run all in the case of `nil`.
11
+ # Example of if you want to run some only.
12
+ # seed_files = ["products", "customers"] #=> Only products.yml and customers.yml
13
+ seed_files = nil
14
+
15
+ # Execute
16
+ Findable::Seed.target_files(seed_dir, seed_files).each {|seed| seed.bootstrap! }
17
+
@@ -1,8 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Findable::Configuration do
4
- let(:storage) { :memory }
5
4
  let(:redis) { {host: "localhost", port: 6379, db: 15} }
5
+ let(:seeds) { "/path/to/seeds.rb" }
6
6
 
7
7
  describe "#initialize" do
8
8
  subject { Findable.config }
@@ -13,21 +13,21 @@ describe Findable::Configuration do
13
13
  end
14
14
 
15
15
  # default settings
16
- it { is_expected.to have_attributes(default_storage: :redis) }
17
16
  it { is_expected.to have_attributes(redis_options: nil) }
17
+ it { is_expected.to have_attributes(seed_file: nil) }
18
18
  end
19
19
 
20
20
  describe "#merge" do
21
- subject { Findable.config.merge(default_storage: storage) }
21
+ subject { Findable.config.merge(seed_file: seeds) }
22
22
 
23
- it { is_expected.to have_attributes(default_storage: storage) }
23
+ it { is_expected.to have_attributes(seed_file: seeds) }
24
24
  it { is_expected.to be_kind_of(Findable::Configuration) }
25
25
  end
26
26
 
27
27
  describe "#merge!" do
28
- subject { Findable.config.merge!(default_storage: storage) }
28
+ subject { Findable.config.merge!(seed_file: seeds) }
29
29
 
30
- it { is_expected.to have_attributes(default_storage: storage) }
30
+ it { is_expected.to have_attributes(seed_file: seeds) }
31
31
  it { is_expected.to be_kind_of(Findable::Configuration) }
32
32
  end
33
33
 
@@ -42,14 +42,14 @@ describe Findable::Configuration do
42
42
  describe "#configure" do
43
43
  before do
44
44
  Findable.configure do |config|
45
- config.default_storage = storage
46
45
  config.redis_options = redis
46
+ config.seed_file = seeds
47
47
  end
48
48
  end
49
49
 
50
50
  subject { Findable.config }
51
51
 
52
- it { is_expected.to have_attributes(default_storage: storage) }
52
+ it { is_expected.to have_attributes(seed_file: seeds) }
53
53
  it { is_expected.to have_attributes(redis_options: redis) }
54
54
 
55
55
  it {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: findable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-12 00:00:00.000000000 Z
11
+ date: 2015-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -70,30 +70,30 @@ dependencies:
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '1.7'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '1.7'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '10.0'
89
+ version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '10.0'
96
+ version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec-rails
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -136,20 +136,24 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
- description: Static model to behave like ActiveRecord.
139
+ description: Redis wrapper with API like ActiveRecord.
140
140
  email:
141
141
  - i2bskn@gmail.com
142
142
  executables: []
143
143
  extensions: []
144
144
  extra_rdoc_files: []
145
145
  files:
146
+ - ".coveralls.yml"
146
147
  - ".gitignore"
148
+ - ".travis.yml"
147
149
  - Gemfile
148
150
  - LICENSE.txt
149
151
  - README.md
150
152
  - Rakefile
151
153
  - findable.gemspec
152
154
  - lib/findable.rb
155
+ - lib/findable/association.rb
156
+ - lib/findable/association/reflector.rb
153
157
  - lib/findable/base.rb
154
158
  - lib/findable/configuration.rb
155
159
  - lib/findable/connection.rb
@@ -157,6 +161,7 @@ files:
157
161
  - lib/findable/namespace.rb
158
162
  - lib/findable/railtie.rb
159
163
  - lib/findable/recordable.rb
164
+ - lib/findable/seed.rb
160
165
  - lib/findable/serializer.rb
161
166
  - lib/findable/version.rb
162
167
  - lib/generators/findable/install_generator.rb
@@ -191,7 +196,7 @@ rubyforge_project:
191
196
  rubygems_version: 2.2.2
192
197
  signing_key:
193
198
  specification_version: 4
194
- summary: Static model to behave like ActiveRecord.
199
+ summary: Redis wrapper with API like ActiveRecord.
195
200
  test_files:
196
201
  - spec/findable/.keep
197
202
  - spec/findable/base_spec.rb