dynamord 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5839249966297fbc355e39fdfdf569f75d9dfc0e
4
+ data.tar.gz: 7f7985804ac779b6ec357e43059fbbb2566c70bc
5
+ SHA512:
6
+ metadata.gz: f4d9731facc4a4043b5ead01fa9be46f6872bfbb8e973510a26696a8d3850e808f02fe35318a82405764c1feef451c183ab23b78a59b703f0e9523fac5964431
7
+ data.tar.gz: 3b72fa88f29b8bacc37bc3d63ca7df6752c9991d0e9be476bfe1b4ba4476790b1f4493ca285268e62dc3e0b13e3b8ae89db022bf0b901594a0b498c398a7c434
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dynamord.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 vibhanshu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Dynamord
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dynamord'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dynamord
18
+
19
+ ## Usage
20
+
21
+ Add Dynamord to your models (both Active record and dynamoid)
22
+
23
+ class User < ActiveRecord::Base
24
+ include Dynamord
25
+
26
+ end
27
+
28
+ and dynamoid:
29
+
30
+ class Picture
31
+ include Dynamoid::Document
32
+ include Dynamord
33
+ end
34
+
35
+ on the dynamo side you can use:
36
+ * to_active_record_belongs_to
37
+ * to_active_record_has_many
38
+ * to_active_record_has_one
39
+
40
+ and on the ActiveRecord side
41
+
42
+ * from_active_record_belongs_to
43
+ * from_active_record_has_many
44
+ * from_active_record_has_one
45
+
46
+ you'll need to specify the association class and sometimes the foreign key (in `has_*` associations)
47
+
48
+ class Picture
49
+ include Dynamoid::Document
50
+ include Dynamord
51
+
52
+ to_active_record_belongs_to :user, :class => User
53
+ end
54
+
55
+ class User < ActiveRecord::Base
56
+ include Dynamord
57
+
58
+ from_active_record_has_many :pictures, :class => Picture, :foreign_key => "user_id"
59
+ end
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dynamord/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dynamord"
8
+ spec.version = Dynamord::VERSION
9
+ spec.date = '2018-03-16'
10
+ spec.authors = ["vibhanshu"]
11
+ spec.email = ["vibhanshu909@gmail.com"]
12
+ spec.description = %q{A dynamoid to active_record and vice versa association manager}
13
+ spec.summary = %q{Allows you to create associations between dynamoid based models and activerecord based models}
14
+ spec.homepage = "https://github.com/vibhanshu-github/Dynamord"
15
+ spec.metadata = { "source_code_uri" => "https://github.com/vibhanshu-github/Dynamord" }
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake", "~> 0"
25
+ spec.add_dependency "dynamoid", "~> 2"
26
+ end
@@ -0,0 +1,136 @@
1
+ require "dynamord/version"
2
+ module Dynamord
3
+ def self.included(base)
4
+ base.class_eval do
5
+ extend ClassMethods
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ ##################################################################################
11
+ # Connection methods from dynamoid to active_record
12
+ def to_active_record_belongs_to(name, options = {})
13
+ # field "#{name}_id", :string
14
+ field "#{name}_id".to_sym, :integer
15
+ validates "#{name}_id".to_sym, presence: true
16
+ object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
17
+ self.instance_eval do
18
+ define_method(name) do |reload = false|
19
+ if reload
20
+ self.instance_variable_set("@#{name}", nil)
21
+ end
22
+
23
+ if self.instance_variable_get("@#{name}").blank?
24
+ self.instance_variable_set("@#{name}", object_class.where(object_class.primary_key => self.send("#{name}_id")).first)
25
+ end
26
+
27
+ self.instance_variable_get("@#{name}")
28
+ end
29
+ attr_accessor name.to_sym
30
+ define_method("#{name}=(new_instance)") do
31
+ self.send("#{name}_id=", new_instance.id)
32
+ self.instance_variable_set("@#{name}", nil)
33
+ end
34
+ end
35
+ end
36
+
37
+ def to_active_record_has_many(name, options = {})
38
+ plural_name = name.to_s.pluralize
39
+ foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
40
+ object_class = options[:class] || name.to_s.singularize.titleize.delete(' ').constantize
41
+ self.instance_eval do
42
+ define_method(plural_name) do |reload = false|
43
+ if reload
44
+ self.instance_variable_set("@#{name}", nil)
45
+ end
46
+
47
+ if self.instance_variable_get("@#{name}").blank?
48
+ self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id.to_s))
49
+ end
50
+
51
+ self.instance_variable_get("@#{name}")
52
+ end
53
+ end
54
+ end
55
+
56
+ def to_active_record_has_one(name, options = {})
57
+ foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
58
+ object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
59
+ self.instance_eval do
60
+ define_method(name) do |reload = false|
61
+ if reload
62
+ self.instance_variable_set("@#{name}", nil)
63
+ end
64
+
65
+ if self.instance_variable_get("@#{name}").blank?
66
+ self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id.to_s).first)
67
+ end
68
+
69
+ self.instance_variable_get("@#{name}")
70
+ end
71
+ end
72
+ end
73
+ ##################################################################################
74
+
75
+ ##################################################################################
76
+ # Connection methods from active_record to dynamoid
77
+ def from_active_record_belongs_to(name, options = {})
78
+ object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
79
+ self.instance_eval do
80
+ define_method(name) do |reload = false|
81
+ if reload
82
+ self.instance_variable_set("@#{name}", nil)
83
+ end
84
+
85
+ if self.instance_variable_get("@#{name}").blank?
86
+ self.instance_variable_set("@#{name}", object_class.where(:id => self.send("#{name}_id")).first)
87
+ end
88
+
89
+ self.instance_variable_get("@#{name}")
90
+ end
91
+ attr_accessor name.to_sym
92
+ define_method("#{name}=(new_instance)") do
93
+ self.send("#{name}_id=", new_instance.id)
94
+ self.instance_variable_set("@#{name}", nil)
95
+ end
96
+ end
97
+ end
98
+
99
+ def from_active_record_has_many(name, options = {})
100
+ plural_name = name.to_s.pluralize
101
+ foreign_key = options[:foreign_key] || "#{self.name.underscore}_id".to_sym
102
+ object_class = options[:class] || name.to_s.singularize.titleize.delete(' ').constantize
103
+ self.instance_eval do
104
+ define_method(plural_name) do |reload = false|
105
+ if reload
106
+ self.instance_variable_set("@#{name}", nil)
107
+ end
108
+
109
+ if self.instance_variable_get("@#{name}").blank?
110
+ self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id))
111
+ end
112
+
113
+ self.instance_variable_get("@#{name}")
114
+ end
115
+ end
116
+ end
117
+
118
+ def from_active_record_has_one(name, options = {})
119
+ foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
120
+ object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
121
+ self.instance_eval do
122
+ define_method(name) do |reload = false|
123
+ if reload
124
+ self.instance_variable_set("@#{name}", nil)
125
+ end
126
+
127
+ if self.instance_variable_get("@#{name}").blank?
128
+ self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id))
129
+ end
130
+
131
+ self.instance_variable_get("@#{name}")
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,3 @@
1
+ module Dynamord
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ module Dynamord
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+ desc "Creates Dynamord initializer for your application"
6
+
7
+ def copy_initializer
8
+ template "gem_initializer.rb", "config/initializers/dynamord.rb"
9
+ template "dynamord_chain.rb", "app/models/concerns/dynamord_chain.rb"
10
+ puts "Install complete!"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ module Dynamord
2
+ module Chain
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include ClassMethods
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ def method_missing(method_name,*params,&block)
11
+ if block.present?
12
+ block = ",&#{block}"
13
+ else
14
+ block = ""
15
+ end
16
+ params = nil if not params.present?
17
+ class_name = self.source
18
+ q = self.query
19
+ class_name.send(method_name,q.merge(params[0]))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ require './app/models/concerns/dynamord_chain'
2
+ Dynamoid::Criteria::Chain.include Dynamord::Chain
3
+
4
+ module ActiveRecord::ConnectionAdapters
5
+ class TableDefinition
6
+ def from_active_record_belongs_to (*args)
7
+ options = args.extract_options!
8
+ column_names = args
9
+ column_names.each { |name| column("#{name}_id".to_sym, 'string', options) }
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynamord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - vibhanshu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-16 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: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: dynamoid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ description: A dynamoid to active_record and vice versa association manager
56
+ email:
57
+ - vibhanshu909@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - dynamord.gemspec
68
+ - lib/dynamord.rb
69
+ - lib/dynamord/version.rb
70
+ - lib/generators/dynamord/install_generator.rb
71
+ - lib/generators/templates/dynamord_chain.rb
72
+ - lib/generators/templates/gem_initializer.rb
73
+ homepage: https://github.com/vibhanshu-github/Dynamord
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ source_code_uri: https://github.com/vibhanshu-github/Dynamord
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Allows you to create associations between dynamoid based models and activerecord
98
+ based models
99
+ test_files: []