Module: Dynamord::ClassMethods

Defined in:
lib/dynamord.rb

Instance Method Summary collapse

Instance Method Details

#configure_dependent(name, dep) ⇒ Object

Connection methods from dynamoid to active_record



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dynamord.rb', line 12

def configure_dependent(name, dep)
    if dep
        case dep
            when :destroy, :delete_all
                after_destroy { |record| record.send(name.to_sym).destroy_all ? true : false }
            when :nullify
                after_destroy { |record| record.send(name.to_sym).update_attributes("#{name}".to_sym => nil) ? true : false}
            when :restrict_with_exception, :restrict_with_error
                before_destroy { |record| raise Dynamoid::Errors::RecordNotDestroyed.new(record); false }
        end
    end
end

#dynamoid_has_many(name, options = {}) ⇒ Object



116
117
118
119
# File 'lib/dynamord.rb', line 116

def dynamoid_has_many(name, options = {})
    configure_dependent(name, options[:dependent])
    self.has_many name, options
end

#dynamoid_has_one(name, options = {}) ⇒ Object

def dynamoid_has_one_belongs_to(name, options = {})

before_create do |record|
    object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
    begin
        if object_class.where(:id => record.send("#{name}_ids").first).count < 1
            true
        end
    rescue
        true
    end
    false
end
self.belongs_to name, options

end



134
135
136
137
# File 'lib/dynamord.rb', line 134

def dynamoid_has_one(name, options = {})
    configure_dependent(name, options[:dependent])
    self.has_many name, options
end

#from_active_record_belongs_to(name, options = {}) ⇒ Object

Connection methods from active_record to dynamoid



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dynamord.rb', line 92

def from_active_record_belongs_to(name, options = {})
    object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
    if options[:optional]
        validates "#{name}_id".to_sym, presence: true
    end
    self.instance_eval do
        define_method(name) do |reload = false|
            if reload
                self.instance_variable_set("@#{name}", nil)
            end

            if self.instance_variable_get("@#{name}").blank?
                self.instance_variable_set("@#{name}", object_class.where(:id => self.send("#{name}_id")).first)
            end

            self.instance_variable_get("@#{name}")
        end
        define_method("#{name}=") do |new_instance|
            self.send("#{name}_id=", new_instance.id)
            self.instance_variable_set("@#{name}", nil)
            # self.save
        end
    end
end

#from_active_record_has_many(name, options = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dynamord.rb', line 138

def from_active_record_has_many(name, options = {})
    plural_name = name.to_s.pluralize
    foreign_key = options[:foreign_key] || "#{self.name.underscore}_id".to_sym
    object_class = options[:class] || name.to_s.singularize.titleize.delete(' ').constantize
    configure_dependent(name, options[:dependent])
    self.instance_eval do
        define_method(plural_name) do |reload = false|
            if reload
                self.instance_variable_set("@#{name}", nil)
            end

            if self.instance_variable_get("@#{name}").blank?
                self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id))
            end

            self.instance_variable_get("@#{name}")
        end
    end
end

#from_active_record_has_one(name, options = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dynamord.rb', line 158

def from_active_record_has_one(name, options = {})
    foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
    object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
    configure_dependent(name, options[:dependent])
    self.instance_eval do
        define_method(name) do |reload = false|
            if reload
                self.instance_variable_set("@#{name}", nil)
            end

            if self.instance_variable_get("@#{name}").blank?
                self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id))
            end

            self.instance_variable_get("@#{name}")
        end
    end
end

#to_active_record_belongs_to(name, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dynamord.rb', line 24

def to_active_record_belongs_to(name, options = {})
    field "#{name}_id".to_sym, :integer
    if options[:optional]
        validates "#{name}_id".to_sym, presence: true
    end
    object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
    self.instance_eval do
        define_method(name) do |reload = false|
            if reload
                self.instance_variable_set("@#{name}", nil)
            end

            if self.instance_variable_get("@#{name}").blank?
                self.instance_variable_set("@#{name}", object_class.where(object_class.primary_key => self.send("#{name}_id")).first)
            end

            self.instance_variable_get("@#{name}")
        end
        define_method("#{name}=") do |new_instance|
            self.send("#{name}_id=", new_instance.id)
            self.instance_variable_set("@#{name}", nil)
            # self.save
        end
    end
end

#to_active_record_has_many(name, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dynamord.rb', line 50

def to_active_record_has_many(name, options = {})
    plural_name = name.to_s.pluralize
    foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
    object_class = options[:class] || name.to_s.singularize.titleize.delete(' ').constantize
    configure_dependent(name, options[:dependent])
    self.instance_eval do
        define_method(plural_name) do |reload = false|
            if reload
                self.instance_variable_set("@#{name}", nil)
            end

            if self.instance_variable_get("@#{name}").blank?
                self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id.to_s))
            end

            self.instance_variable_get("@#{name}")
        end
    end
end

#to_active_record_has_one(name, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dynamord.rb', line 70

def to_active_record_has_one(name, options = {})
    foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
    object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
    configure_dependent(name, options[:dependent])
    self.instance_eval do
        define_method(name) do |reload = false|
            if reload
                self.instance_variable_set("@#{name}", nil)
            end

            if self.instance_variable_get("@#{name}").blank?
                self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id.to_s).first)
            end

            self.instance_variable_get("@#{name}")
        end
    end
end