Module: MLS::Model
- Defined in:
- lib/mls/model.rb
Class Method Summary collapse
-
.extended(model) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #collection_root_element ⇒ Object
-
#create(attributes = {}) {|model| ... } ⇒ Object
Creates an object and saves it to the MLS.
- #create_reader_for(property) ⇒ Object
- #create_writer_for(property) ⇒ Object
- #exclude_from_comparison(*properties) ⇒ Object
-
#model_name ⇒ Object
for rails form stuff.
- #param_key ⇒ Object
- #properties ⇒ Object
- #properties_excluded_from_comparison ⇒ Object
-
#property(name, type, options = {}) ⇒ Object
Properties ===================================================================================================.
- #property_module ⇒ Object
- #root_element ⇒ Object
-
#root_element_string ⇒ Object
used for parser.
Class Method Details
.extended(model) ⇒ Object
:nodoc:
3 4 5 6 |
# File 'lib/mls/model.rb', line 3 def self.extended(model) #:nodoc: model.instance_variable_set(:@properties, {}) model.instance_variable_set(:@associations, {}) end |
Instance Method Details
#collection_root_element ⇒ Object
121 122 123 |
# File 'lib/mls/model.rb', line 121 def collection_root_element @collection_root_element ||= root_element_string.pluralize.to_sym end |
#create(attributes = {}) {|model| ... } ⇒ Object
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
20 21 22 23 24 25 |
# File 'lib/mls/model.rb', line 20 def create(attributes={}, &block) # TODO: testme model = self.new(attributes) yield(model) if block_given? model.save model end |
#create_reader_for(property) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mls/model.rb', line 63 def create_reader_for(property) reader_name = property.name.to_s boolean_reader_name = "#{reader_name}?" reader_visibility = property.reader_visibility instance_variable_name = property.instance_variable_name property_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1 #{reader_visibility} def #{reader_name} return #{instance_variable_name} if defined?(#{instance_variable_name}) property = properties[:#{reader_name}] #{instance_variable_name} = property ? property.default : nil end RUBY if property.kind_of?(MLS::Property::Boolean) property_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1 #{reader_visibility} def #{boolean_reader_name} #{reader_name} end RUBY end end |
#create_writer_for(property) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mls/model.rb', line 88 def create_writer_for(property) name = property.name writer_name = "#{name}=" writer_visibility = property.writer_visibility instance_variable_name = property.instance_variable_name property_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1 #{writer_visibility} def #{writer_name}(value) property = self.class.properties[:#{name}] #{instance_variable_name} = property.load(value) end RUBY end |
#exclude_from_comparison(*properties) ⇒ Object
41 42 43 |
# File 'lib/mls/model.rb', line 41 def exclude_from_comparison(*properties) @properties_excluded_from_comparison |= properties end |
#model_name ⇒ Object
for rails form stuff
104 105 106 |
# File 'lib/mls/model.rb', line 104 def model_name self end |
#param_key ⇒ Object
108 109 110 |
# File 'lib/mls/model.rb', line 108 def param_key root_element.to_s end |
#properties ⇒ Object
49 50 51 |
# File 'lib/mls/model.rb', line 49 def properties @properties end |
#properties_excluded_from_comparison ⇒ Object
45 46 47 |
# File 'lib/mls/model.rb', line 45 def properties_excluded_from_comparison @properties_excluded_from_comparison end |
#property(name, type, options = {}) ⇒ Object
Properties ===================================================================================================
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mls/model.rb', line 29 def property(name, type, = {}) klass = MLS::Property.determine_class(type) raise NotImplementedError, "#{type} is not supported" unless klass property = klass.new(name, ) @properties[property.name] = property @properties_excluded_from_comparison = [] create_reader_for(property) create_writer_for(property) end |
#property_module ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/mls/model.rb', line 53 def property_module @property_module ||= begin mod = Module.new class_eval do include mod end mod end end |
#root_element ⇒ Object
117 118 119 |
# File 'lib/mls/model.rb', line 117 def root_element @root_element ||= root_element_string.to_sym end |
#root_element_string ⇒ Object
used for parser
113 114 115 |
# File 'lib/mls/model.rb', line 113 def root_element_string ActiveSupport::Inflector.demodulize(self).underscore end |