Module: SimpleRecord::Attributes
- Included in:
- Base
- Defined in:
- lib/simple_record/attributes.rb
Defined Under Namespace
Classes: Attribute
Constant Summary collapse
- @@virtuals =
[]
Class Method Summary collapse
Instance Method Summary collapse
- #are_booleans(*args) ⇒ Object
- #are_dates(*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
- #has_attributes(*args) ⇒ Object
- #has_booleans(*args) ⇒ Object
- #has_dates(*args) ⇒ Object
- #has_ints(*args) ⇒ Object
- #has_many(*args) ⇒ Object
- #has_one(*args) ⇒ Object
- #has_strings(*args) ⇒ Object
- #has_virtuals(*args) ⇒ Object
Class Method Details
.handle_virtuals(attrs) ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/simple_record/attributes.rb', line 196 def self.handle_virtuals(attrs) @@virtuals.each do |virtual| #we first copy the information for the virtual to an instance variable of the same name eval("@#{virtual}=attrs['#{virtual}']") #and then remove the parameter before it is passed to initialize, so that it is NOT sent to SimpleDB eval("attrs.delete('#{virtual}')") end end |
.included(base) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/simple_record/attributes.rb', line 6 def self.included(base) #puts 'Callbacks included in ' + base.inspect =begin instance_eval <<-endofeval def self.defined_attributes #puts 'class defined_attributes' @attributes ||= {} @attributes endendofeval endofeval =end end |
Instance Method Details
#are_booleans(*args) ⇒ Object
105 106 107 108 109 |
# File 'lib/simple_record/attributes.rb', line 105 def are_booleans(*args) args.each do |arg| defined_attributes[arg].type = :boolean end end |
#are_dates(*args) ⇒ Object
99 100 101 102 103 |
# File 'lib/simple_record/attributes.rb', line 99 def are_dates(*args) args.each do |arg| defined_attributes[arg].type = :date end end |
#are_ints(*args) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/simple_record/attributes.rb', line 92 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.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/simple_record/attributes.rb', line 125 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/simple_record/attributes.rb', line 53 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
21 22 23 24 |
# File 'lib/simple_record/attributes.rb', line 21 def defined_attributes @attributes ||= {} @attributes end |
#has_attributes(*args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/simple_record/attributes.rb', line 26 def has_attributes(*args) args.each do |arg| = nil if arg.is_a?(Hash) # then attribute may have extra options = arg arg = [:name].to_sym end attr = Attribute.new(:string, ) 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
87 88 89 90 |
# File 'lib/simple_record/attributes.rb', line 87 def has_booleans(*args) has_attributes(*args) are_booleans(*args) end |
#has_dates(*args) ⇒ Object
82 83 84 85 |
# File 'lib/simple_record/attributes.rb', line 82 def has_dates(*args) has_attributes(*args) are_dates(*args) end |
#has_ints(*args) ⇒ Object
77 78 79 80 |
# File 'lib/simple_record/attributes.rb', line 77 def has_ints(*args) has_attributes(*args) are_ints(*args) end |
#has_many(*args) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/simple_record/attributes.rb', line 175 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
191 192 193 |
# File 'lib/simple_record/attributes.rb', line 191 def has_one(*args) end |
#has_strings(*args) ⇒ Object
73 74 75 |
# File 'lib/simple_record/attributes.rb', line 73 def has_strings(*args) has_attributes(*args) end |
#has_virtuals(*args) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/simple_record/attributes.rb', line 113 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 |