lib/mls/model.rb in mls-0.2.2 vs lib/mls/model.rb in mls-0.2.3

- old
+ new

@@ -1,25 +1,53 @@ module MLS::Model - def self.extended(model) + def self.extended(model) #:nodoc: model.instance_variable_set(:@properties, {}) model.instance_variable_set(:@associations, {}) end + # Creates an object and saves it to the MLS. The resulting object is returned + # whether or no the object was saved successfully to the MLS or not. + # + # ==== Examples + # #!ruby + # # Create a single new object + # User.create(:first_name => 'Jamie') + # + # # Create a single object and pass it into a block to set other attributes. + # User.create(:first_name => 'Jamie') do |u| + # u.is_admin = false + # end + def create(attributes={}, &block) # TODO: testme + model = self.new(attributes) + yield(model) if block_given? + model.save + model + end + # Properties =================================================================================================== - + def property(name, type, options = {}) klass = MLS::Property.determine_class(type) raise NotImplementedError, "#{type} is not supported" unless klass property = klass.new(name, options) @properties[property.name] = property + @properties_excluded_from_comparison = [] create_reader_for(property) create_writer_for(property) end + def exclude_from_comparison(*properties) + @properties_excluded_from_comparison |= properties + end + + def properties_excluded_from_comparison + @properties_excluded_from_comparison + end + def properties @properties end def property_module @@ -80,15 +108,18 @@ def param_key root_element.to_s end # used for parser - + def root_element_string + ActiveSupport::Inflector.demodulize(self).underscore + end + def root_element - @root_element ||= ActiveSupport::Inflector.demodulize(self).downcase.to_sym + @root_element ||= root_element_string.to_sym end def collection_root_element - @collection_root_element ||= ActiveSupport::Inflector.demodulize(self).downcase.pluralize.to_sym + @collection_root_element ||= root_element_string.pluralize.to_sym end end