Sha256: 2101cb3501763c0272b469fd0c458e04f8adfcc7c73cfa3b09340c62c22bf5fa

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# 访问LightCloud的加载模块
module AntLightCloud

	def self.included(base)
		require 'lightcloud'
		base.send "configurations=", YAML.load_file(base.configurations) if base.configurations.is_a?(String)
		base.configurations.each do |key,value|
		lookup_nodes, storage_nodes = LightCloud.generate_nodes(value)
			LightCloud.init(lookup_nodes, storage_nodes,key)
		end

		base.extend ClassMethods
		base.send :include, InstanceMethods
	end

	module ClassMethods
		def delete(key)
    	LightCloud.delete(key,self.lightcloud_system)
    end

		# 指定类名,默认是class定义的名字
		def class_name=(value)
			write_inheritable_attribute :class_name,value
		end

		def class_name
			read_inheritable_attribute(:class_name) || self.to_s
		end

		# LightCloud系统名,用于存储时指定LightCloud系统
		def lightcloud_system
			class_name.downcase
		end

		private
      def load(key)
				raw_record = LightCloud.get(key,self.lightcloud_system)
				raise AntMapper::ObjectNotFound unless raw_record
				Marshal.load(raw_record)
      end
	end

	module InstanceMethods
		private
			def set
				record = {}
				self.class.attributes.each do |attribute|
				  record.merge!({attribute=>(self.send attribute)})
				end
				begin
				 	LightCloud.set(@object_key,Marshal.dump(record),self.class.lightcloud_system)
					true
				rescue
					false
				end
			end
	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ant-mapper-0.0.2 ./lib/ant_mapper/adapters/light_cloud.rb