Module: SimpleRecord::Attributes::ClassMethods
- Included in:
- Base
- Defined in:
- lib/simple_record/attributes.rb
Instance Method Summary collapse
- #are_booleans(*args) ⇒ Object
- #are_dates(*args) ⇒ Object
- #are_floats(*args) ⇒ Object
- #are_ints(*args) ⇒ Object
-
#belongs_to(association_id, options = {}) ⇒ Object
One belongs_to association per call.
- #define_dirty_methods(arg_s) ⇒ Object
- #defined_attributes ⇒ Object
- #get_sr_config ⇒ Object
- #has_attributes(*args) ⇒ Object
- #has_attributes2(args, options_for_all = {}) ⇒ Object
- #has_booleans(*args) ⇒ Object
- #has_clobs(*args) ⇒ Object
- #has_dates(*args) ⇒ Object
- #has_floats(*args) ⇒ Object
- #has_ints(*args) ⇒ Object
- #has_many(*args) ⇒ Object
- #has_one(*args) ⇒ Object
- #has_strings(*args) ⇒ Object
- #has_virtuals(*args) ⇒ Object
-
#sr_config(options = {}) ⇒ Object
Add configuration to this particular class.
Instance Method Details
#are_booleans(*args) ⇒ Object
139 140 141 142 143 |
# File 'lib/simple_record/attributes.rb', line 139 def are_booleans(*args) args.each do |arg| defined_attributes[arg].type = :boolean end end |
#are_dates(*args) ⇒ Object
133 134 135 136 137 |
# File 'lib/simple_record/attributes.rb', line 133 def are_dates(*args) args.each do |arg| defined_attributes[arg].type = :date end end |
#are_floats(*args) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/simple_record/attributes.rb', line 126 def are_floats(*args) # puts 'calling are_ints: ' + args.inspect args.each do |arg| defined_attributes[arg].type = :float end end |
#are_ints(*args) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/simple_record/attributes.rb', line 119 def are_ints(*args) # puts 'calling are_ints: ' + args.inspect args.each do |arg| defined_attributes[arg].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
165 166 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 |
# File 'lib/simple_record/attributes.rb', line 165 def belongs_to(association_id, = {}) arg = association_id arg_s = arg.to_s arg_id = arg.to_s + '_id' attribute = Attribute.new(:belongs_to, ) 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/simple_record/attributes.rb', line 75 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_attributes ⇒ Object
36 37 38 39 |
# File 'lib/simple_record/attributes.rb', line 36 def defined_attributes @attributes ||= {} @attributes end |
#get_sr_config ⇒ Object
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 |
# File 'lib/simple_record/attributes.rb', line 45 def has_attributes2(args, ={}) # puts 'args=' + args.inspect # puts 'options_for_all = ' + options_for_all.inspect args.each do |arg| = {} if arg.is_a?(Hash) # then attribute may have extra options = arg arg = [:name].to_sym end type = [:type] || :string attr = Attribute.new(type, ) 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
114 115 116 117 |
# File 'lib/simple_record/attributes.rb', line 114 def has_booleans(*args) has_attributes(*args) are_booleans(*args) end |
#has_clobs(*args) ⇒ Object
145 146 147 148 |
# File 'lib/simple_record/attributes.rb', line 145 def has_clobs(*args) has_attributes2(args, :type=>:clob) end |
#has_dates(*args) ⇒ Object
109 110 111 112 |
# File 'lib/simple_record/attributes.rb', line 109 def has_dates(*args) has_attributes(*args) are_dates(*args) end |
#has_floats(*args) ⇒ Object
104 105 106 107 |
# File 'lib/simple_record/attributes.rb', line 104 def has_floats(*args) has_attributes(*args) are_floats(*args) end |
#has_ints(*args) ⇒ Object
99 100 101 102 |
# File 'lib/simple_record/attributes.rb', line 99 def has_ints(*args) has_attributes(*args) are_ints(*args) end |
#has_many(*args) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/simple_record/attributes.rb', line 215 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
231 232 233 |
# File 'lib/simple_record/attributes.rb', line 231 def has_one(*args) end |
#has_strings(*args) ⇒ Object
95 96 97 |
# File 'lib/simple_record/attributes.rb', line 95 def has_strings(*args) has_attributes(*args) end |
#has_virtuals(*args) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/simple_record/attributes.rb', line 150 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(={}) get_sr_config @sr_config.merge!() end |