Module: SimpleRecord::Attributes::ClassMethods

Included in:
Base
Defined in:
lib/simple_record/attributes.rb

Instance Method Summary collapse

Instance Method Details

#are_booleans(*args) ⇒ Object



141
142
143
144
145
# File 'lib/simple_record/attributes.rb', line 141

def are_booleans(*args)
    args.each do |arg|
        defined_attributes[arg.to_sym].type = :boolean
    end
end

#are_dates(*args) ⇒ Object



135
136
137
138
139
# File 'lib/simple_record/attributes.rb', line 135

def are_dates(*args)
    args.each do |arg|
        defined_attributes[arg.to_sym].type = :date
    end
end

#are_floats(*args) ⇒ Object



128
129
130
131
132
133
# File 'lib/simple_record/attributes.rb', line 128

def are_floats(*args)
    #    puts 'calling are_ints: ' + args.inspect
    args.each do |arg|
        defined_attributes[arg.to_sym].type = :float
    end
end

#are_ints(*args) ⇒ Object



121
122
123
124
125
126
# File 'lib/simple_record/attributes.rb', line 121

def are_ints(*args)
    #    puts 'calling are_ints: ' + args.inspect
    args.each do |arg|
        defined_attributes[arg.to_sym].type = :int
    end
end

#belongs_to(association_id, options = {}) ⇒ Object

One belongs_to association per call. Call multiple times if there are more than one.

This method will also create an {association)_id method that will return the ID of the foreign object without actually materializing it.

options:

:class_name=>"User" - to change the default class to use


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/simple_record/attributes.rb', line 167

def belongs_to(association_id, options = {})
    arg                     = association_id
    arg_s                   = arg.to_s
    arg_id                  = arg.to_s + '_id'
    attribute               = Attribute.new(:belongs_to, options)
    defined_attributes[arg] = attribute

    # todo: should also handle foreign_key http://74.125.95.132/search?q=cache:KqLkxuXiBBQJ:wiki.rubyonrails.org/rails/show/belongs_to+rails+belongs_to&hl=en&ct=clnk&cd=1&gl=us
    #    puts "arg_id=#{arg}_id"
    #        puts "is defined? " + eval("(defined? #{arg}_id)").to_s
    #        puts 'atts=' + @attributes.inspect

    # Define reader method
    send(:define_method, arg) do
        return get_attribute(arg)
    end


    # Define writer method
    send(:define_method, arg.to_s + "=") do |value|
        set(arg, value)
    end


    # Define ID reader method for reading the associated objects id without getting the entire object
    send(:define_method, arg_id) do
        get_attribute_sdb(arg_s)
    end

    # Define writer method for setting the _id directly without the associated object
    send(:define_method, arg_id + "=") do |value|
#                rb_att_name = arg_s # n2 = name.to_s[0, name.length-3]
        set(arg_id, value)
#                if value.nil?
#                    self[arg_id] = nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save
#                else
#                    self[arg_id] = value
#                end
    end

    send(:define_method, "create_"+arg.to_s) do |*params|
        newsubrecord=eval(arg.to_s.classify).new(*params)
        newsubrecord.save
        arg_id      = arg.to_s + '_id'
        self[arg_id]=newsubrecord.id
    end

    define_dirty_methods(arg_s)
end

#define_dirty_methods(arg_s) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/simple_record/attributes.rb', line 77

def define_dirty_methods(arg_s)
    # Now for dirty methods: http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html
    # define changed? method
    send(:define_method, arg_s + "_changed?") do
        @dirty.has_key?(sdb_att_name(arg_s))
    end

    # define change method
    send(:define_method, arg_s + "_change") do
        old_val = @dirty[sdb_att_name(arg_s)]
        [old_val, get_attribute(arg_s)]
    end

    # define was method
    send(:define_method, arg_s + "_was") do
        old_val = @dirty[sdb_att_name(arg_s)]
        old_val
    end
end

#defined_attributesObject



36
37
38
39
# File 'lib/simple_record/attributes.rb', line 36

def defined_attributes
    @attributes ||= {}
    @attributes
end

#get_sr_configObject



32
33
34
# File 'lib/simple_record/attributes.rb', line 32

def get_sr_config
    @sr_config ||= {}
end

#has_attributes(*args) ⇒ Object



41
42
43
# File 'lib/simple_record/attributes.rb', line 41

def has_attributes(*args)
    has_attributes2(args)
end

#has_attributes2(args, options_for_all = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/simple_record/attributes.rb', line 45

def has_attributes2(args, options_for_all={})
#            puts 'args=' + args.inspect
#            puts 'options_for_all = ' + options_for_all.inspect
    args.each do |arg|
        arg_options = {}
        if arg.is_a?(Hash)
            # then attribute may have extra options
            arg_options = arg
            arg         = arg_options[:name].to_sym
        else
            arg = arg.to_sym
        end
        type = options_for_all[:type] || :string
        attr = Attribute.new(type, arg_options)
        defined_attributes[arg] = attr if defined_attributes[arg].nil?

        # define reader method
        arg_s = arg.to_s # to get rid of all the to_s calls
        send(:define_method, arg) do
            ret = get_attribute(arg)
            return ret
        end

        # define writer method
        send(:define_method, arg_s+"=") do |value|
            set(arg, value)
        end

        define_dirty_methods(arg_s)
    end
end

#has_booleans(*args) ⇒ Object



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

def has_booleans(*args)
    has_attributes(*args)
    are_booleans(*args)
end

#has_clobs(*args) ⇒ Object



147
148
149
150
# File 'lib/simple_record/attributes.rb', line 147

def has_clobs(*args)
    has_attributes2(args, :type=>:clob)

end

#has_dates(*args) ⇒ Object



111
112
113
114
# File 'lib/simple_record/attributes.rb', line 111

def has_dates(*args)
    has_attributes(*args)
    are_dates(*args)
end

#has_floats(*args) ⇒ Object



106
107
108
109
# File 'lib/simple_record/attributes.rb', line 106

def has_floats(*args)
    has_attributes(*args)
    are_floats(*args)
end

#has_ints(*args) ⇒ Object



101
102
103
104
# File 'lib/simple_record/attributes.rb', line 101

def has_ints(*args)
    has_attributes(*args)
    are_ints(*args)
end

#has_many(*args) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/simple_record/attributes.rb', line 217

def has_many(*args)
    args.each do |arg|
        #okay, this creates an instance method with the pluralized name of the symbol passed to belongs_to
        send(:define_method, arg) do
            #when called, the method creates a new, very temporary instance of the Activerecordtosdb_subrecord class
            #It is passed the three initializers it needs:
            #note the first parameter is just a string by time new gets it, like "user"
            #the second and third parameters are still a variable when new gets it, like user_id
            return eval(%{Activerecordtosdb_subrecord_array.new('#{arg}', self.class.name ,id)})
        end
    end
    #Disclaimer: this whole funciton just seems crazy to me, and a bit inefficient. But it was the clearest way I could think to do it code wise.
    #It's bad programming form (imo) to have a class method require something that isn't passed to it through it's variables.
    #I couldn't pass the id when calling find, since the original find doesn't work that way, so I was left with this.
end

#has_one(*args) ⇒ Object



233
234
235
# File 'lib/simple_record/attributes.rb', line 233

def has_one(*args)

end

#has_strings(*args) ⇒ Object



97
98
99
# File 'lib/simple_record/attributes.rb', line 97

def has_strings(*args)
    has_attributes(*args)
end

#has_virtuals(*args) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/simple_record/attributes.rb', line 152

def has_virtuals(*args)
    @@virtuals = args
    args.each do |arg|
        #we just create the accessor functions here, the actual instance variable is created during initialize
        attr_accessor(arg)
    end
end

#sr_config(options = {}) ⇒ Object

Add configuration to this particular class.

:single_clob=> true/false. If true will store all clobs as a single object in s3. Default is false.


27
28
29
30
# File 'lib/simple_record/attributes.rb', line 27

def sr_config(options={})
    get_sr_config
    @sr_config.merge!(options)
end