active-record-binder 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/active_record_binder.rb +34 -1
  2. metadata +6 -5
@@ -1,12 +1,37 @@
1
1
  require 'active_record'
2
2
  require 'active_support/core_ext/string'
3
+ require 'forwardable'
4
+
5
+ # Private: A simple module that delegates classes methods when needed, keeping the calls in memory.
6
+ module DifferedDelegator
7
+ def register_delegators *args
8
+ args.each do |delegator|
9
+ delegator = delegator.to_s
10
+ module_eval %Q{
11
+ def self.#{delegator} *parameters
12
+ @delegators ||= []
13
+ @delegators << { name: :#{delegator}, params: parameters }
14
+ end
15
+ }
16
+ end
17
+ end
18
+
19
+ def delegate_to klass_or_object
20
+ @delegators.each do |data|
21
+ unless data.empty?
22
+ name = data[:name]
23
+ args = data[:params]
24
+ klass_or_object.send(name, *args)
25
+ end
26
+ end
27
+ end
28
+ end
3
29
 
4
30
  class MigrationVersionError < Exception; end
5
31
 
6
32
  # Public: Namespace containing classes to create binder to.
7
33
  # A binder is simply a tool to use for Databases plugs or adaptors building.
8
34
  module Binder
9
-
10
35
  # Public: Active Record Binder class. You need to inherit from it to create your Plug or Adaptor.
11
36
  #
12
37
  # Examples
@@ -48,7 +73,10 @@ module Binder
48
73
  # ARMySqlPlug::connection # => { :user => 'Foo', :password => 'Bar', :host => 'localhost' }
49
74
  #
50
75
  class AR
76
+ extend DifferedDelegator
77
+
51
78
  attr_reader :table_name, :table
79
+ register_delegators :has_many, :has_one, :has_and_belongs_to_many, :belongs_to
52
80
 
53
81
  # Public: Returns a new instance of the binder's class.
54
82
  # It also automaticaly establish a connection with the database throught the specified adapter,
@@ -67,7 +95,12 @@ module Binder
67
95
  @table_name = table_name
68
96
  this = self.class
69
97
 
98
+ # Retrieves or Create the ActiveRecord::Base subclass that will match the table.
70
99
  table = meta_def_ar_class(this.database, this.adapter, this.connection)
100
+ # Handle ActiveRecord::Base delegation, to ensure painless associations
101
+ self.class.delegate_to table
102
+
103
+ # Establishes a connection to the database
71
104
  table.connect unless table.connected?
72
105
  end
73
106
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active-record-binder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-19 00:00:00.000000000 Z
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Ruby library for interfacing with ActiveRecord and create easy elegant
15
- migrations.
14
+ description: Ruby library to ease the process of interfacing with ActiveRecord. Allows
15
+ to create simple and elegant migrations.
16
16
  email: dehan.gabriel@gmail.com
17
17
  executables: []
18
18
  extensions: []
@@ -42,6 +42,7 @@ rubyforge_project:
42
42
  rubygems_version: 1.8.15
43
43
  signing_key:
44
44
  specification_version: 3
45
- summary: Ruby library for interfacing with ActiveRecord and create easy elegant migrations.
45
+ summary: Ruby library to ease the process of interfacing with ActiveRecord. Allows
46
+ to create simple and elegant migrations.
46
47
  test_files: []
47
48
  has_rdoc: