Class: JOffice::Redis::ModelFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/joffice_redis/model_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, logger) {|_self| ... } ⇒ ModelFactory

Returns a new instance of ModelFactory.

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/joffice_redis/model_factory.rb', line 34

def initialize(parent, logger)
  @parent=parent      
  @logger=logger
  @consts=[]
  @static_methods=[]
  @instance_methods=[]
  @indexes={}
  @composite_indexes={}
  @static_alias=[]
  @index_by_field={}
  @attributes,@attributes_hash,@attribute_lists,@unique_indexes = {},{},{},{}
  
  yield self if block_given?
  
  #@class_name="CS#{Digest::SHA1.hexdigest(parent.name)}"      

  @class_name="CS#{class_table}_#{prefix}"      
  @class_module_name="JOffice::Redis::Metadata::#{@class_name}::ClassMethods"
  @instance_module_name="JOffice::Redis::Metadata::#{@class_name}::InstanceMethods"
      
  generate
  
  
  #@buffer=["", "", "module JOffice::Redis","module Metadata","# Metadata for class #{parent.name}", "module #{@class_name}"]

  #@buffer << "module ClassMethods"

  @buffer = ["class << self"]
  @buffer << "include JOffice::Redis::MarshalValue"      
  @buffer+=@consts
  @buffer << ""
  @buffer+=@static_methods.map{|v| "      #{v}"}
  @buffer<< @static_alias.join("\n")
  @buffer << "end"
  
  #@buffer << "module InstanceMethods"      

  @buffer << ""
  @buffer+=@instance_methods.map{|v| "   #{v}"}
  @buffer << ""
  #@buffer << "end"

  #@buffer << "#{parent.name}.send(:include, InstanceMethods)"

  ####@buffer << "#{parent.name}.send(:extend, JOffice::Redis::CacheModelClassMethods)"

  #@buffer << "#{parent.name}.send(:extend, ClassMethods)"

  #@buffer+=["end","end","end"]

  parent.class_eval @buffer.join("\n")
  #save_and_include      

end

Instance Attribute Details

#attribute_listsObject (readonly)

Returns the value of attribute attribute_lists.



27
28
29
# File 'lib/joffice_redis/model_factory.rb', line 27

def attribute_lists
  @attribute_lists
end

#attributesObject (readonly)

Returns the value of attribute attributes.



25
26
27
# File 'lib/joffice_redis/model_factory.rb', line 25

def attributes
  @attributes
end

#attributes_hashObject (readonly)

Returns the value of attribute attributes_hash.



26
27
28
# File 'lib/joffice_redis/model_factory.rb', line 26

def attributes_hash
  @attributes_hash
end

#class_module_nameObject (readonly)

Returns the value of attribute class_module_name.



31
32
33
# File 'lib/joffice_redis/model_factory.rb', line 31

def class_module_name
  @class_module_name
end

#instance_module_nameObject (readonly)

Returns the value of attribute instance_module_name.



32
33
34
# File 'lib/joffice_redis/model_factory.rb', line 32

def instance_module_name
  @instance_module_name
end

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/joffice_redis/model_factory.rb', line 30

def logger
  @logger
end

#parentObject (readonly)

Returns the value of attribute parent.



24
25
26
# File 'lib/joffice_redis/model_factory.rb', line 24

def parent
  @parent
end

#prefixObject

Returns the value of attribute prefix.



29
30
31
# File 'lib/joffice_redis/model_factory.rb', line 29

def prefix
  @prefix
end

#unique_indexesObject (readonly)

Returns the value of attribute unique_indexes.



28
29
30
# File 'lib/joffice_redis/model_factory.rb', line 28

def unique_indexes
  @unique_indexes
end

Instance Method Details

#attribute(name, options = {}) ⇒ Object



90
91
92
93
94
95
# File 'lib/joffice_redis/model_factory.rb', line 90

def attribute(name, options={})
  unique=options[:unique]
  key_name=(options[:key_name] || name)
  attributes[name.to_sym]=[options[:type] || :object, unique, key_name]       
  unique_indexes[name]=key_name if unique
end

#attribute_list(name, options = {}) ⇒ Object



97
98
99
# File 'lib/joffice_redis/model_factory.rb', line 97

def attribute_list(name, options={})
  attribute_lists[name]=options       
end

#composite_index(name, *fields) ⇒ Object



110
111
112
113
# File 'lib/joffice_redis/model_factory.rb', line 110

def composite_index(name, *fields)
  options=fields.extract_options!
  @composite_indexes[name]=[fields.flatten, options]
end

#hash(name, options = {}) ⇒ Object



101
102
103
# File 'lib/joffice_redis/model_factory.rb', line 101

def hash(name, options={})        
  attributes_hash[name]=options      
end

#index(name, field) ⇒ Object



105
106
107
108
# File 'lib/joffice_redis/model_factory.rb', line 105

def index(name, field)
 (@indexes[name]||=[]) << field
  @index_by_field[field]=name      
end

#save_and_includeObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/joffice_redis/model_factory.rb', line 79

def save_and_include
  dir=File.join(File.absolute_path(File.dirname(__FILE__)),'metadata')
  FileUtils.mkdir_p(dir)
  
  path=File.join(dir, "#{@class_name}.rb")
  f=File.open(path,'w')
  f.write(@buffer.join("\n"))
  f.close
  #require path      

end

#set_table_name(name) ⇒ Object



115
116
117
# File 'lib/joffice_redis/model_factory.rb', line 115

def set_table_name(name)
  @class_table=name      
end