Sha256: 198f5d457404795c2d02185e90f05d0df446384bcf10f033d14df0477f164e74

Contents?: true

Size: 945 Bytes

Versions: 1

Compression:

Stored size: 945 Bytes

Contents

module LazyModel
	
	class LazyModel

		attr_accessor :model, :attribute, :enumerables, :custom_finders, :column
	
		def initialize(model, attribute, enumerables = nil, custom_finders = {})
			self.model 			= model
			self.attribute 		= attribute
			self.enumerables 	= format_enumerables(enumerables)
			self.custom_finders = custom_finders
		end

		def define_methods
			#make sure the column exists
			unless self.column = model.columns_hash[attribute.to_s]
				raise "\'#{attribute}\' is not an attribute for \'#{model.to_s}\' model"
			end

			#pass to lazy class id supported
			begin
				klass = "LazyModel::Lazy#{column.type.to_s.camelize}".constantize
				klass.new(self).define_methods
			rescue NameError
				warn " attribute type \'#{column.type}\'' on \'#{attribute}\' is not supported "				
			end
		end

		private

		def format_enumerables(enumerables)
			Array(enumerables).map{|enumerable| enumerable.underscore}
		end

	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lazy_model-0.0.3 lib/lazy_model/lazy_model.rb