Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/n/db/managed.rb,
lib/n/properties.rb

Overview

module

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__metaObject

Metadata A hash of module metadata.



63
64
65
# File 'lib/n/properties.rb', line 63

def __meta
  @__meta
end

#__propsObject

Properties An array is used to enforce order.



59
60
61
# File 'lib/n/properties.rb', line 59

def __props
  @__props
end

Instance Method Details

#add_meta(key, val) ⇒ Object

Add the metadata TODO: Optimize this



147
148
149
150
151
152
153
# File 'lib/n/properties.rb', line 147

def add_meta(key, val)
	@__meta[key] = [] unless @__meta[key]

	# guard against duplicates, no need to keep order.
	@__meta[key].delete_if { |v| val == v }
	@__meta[key] << val			
end

#add_prop(prop) ⇒ Object

Add the property Optimize this



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/n/properties.rb', line 97

def add_prop(prop)		
	if idx = @__props.index(prop)
		# override in case of duplicates. Keep the order of the props.
		@__props[idx] = prop
	else
		@__props << prop
	end
	
	# Precompile the property read/write methods

	s, klass = prop.symbol, prop.klass 
	
	module_eval %{
		def #{s}
			return @#{s}
		end
		
		def #{s}=(val)
			@#{s} = val
		end
		
		def __force_#{s}(val)
				@#{s} = } + case klass.name
											when Fixnum.name
												"val.to_i()"
											when String.name
												"val.to_s()"
											when Float.name
												"val.to_f()"
											when Time.name
												"Time.parse(val.to_s())"
											when TrueClass.name, FalseClass.name
												"val.to_i() > 0"
											else
												"val"
										end + %{ 
		end
	}
end

#include(*modules) ⇒ Object

A new version of include that includes the properties too.



185
186
187
188
189
190
191
# File 'lib/n/properties.rb', line 185

def include(*modules)
	old_include(*modules)
	
	for m in modules
		inherit_meta(m)
	end
end

#inherit_meta(mod = superclass) ⇒ Object

This method is typically called before including other modules to preserve properties order.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/n/properties.rb', line 158

def inherit_meta(mod = superclass)
	# concat props.
	if mod.__props
		@__props = N::SafeArray.new unless @__props
		
		mod.__props.each { |p|
			add_prop(p)
		} 
	end
	
	# concat metadata
	if mod.__meta
		mod.__meta.each { |k, val|
			val.each { |v|
				meta(k, v)
			} if val
		}
	end
end

#manage(&block) ⇒ Object

Wrap prop/meta declarations.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/n/db/managed.rb', line 35

def manage(&block)
	inherit_meta(superclass) if superclass
	
	yield
	
	N::Managed.eval_dbseq(self)
	N::Managed.eval_dbtable(self)
# 	gmosx: this is called from DbConnection
#		N::Managed.eval_db_read_row(self)
	N::Managed.eval_db_insert(self)
	N::Managed.eval_db_update(self)
end

#meta(key, val) ⇒ Object

Attach metadata



139
140
141
142
# File 'lib/n/properties.rb', line 139

def meta(key, val)
	@__meta = N::SafeHash.new unless @__meta
	add_meta(key, val)
end

#old_includeObject

Override the default include method to also include the metadata. WARNING: can cause infinite loop if overriden again!



181
# File 'lib/n/properties.rb', line 181

alias_method :old_include, :include

#prop_accessor(*params) ⇒ Object Also known as: prop

Define a property (== typed attribute) based on draks property redesign!

ex: prop_accessor String, :title, :body prop_accessor String, “char(10) NOT NULL”, :title, :body



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/n/properties.rb', line 72

def prop_accessor(*params)
	klass = params.shift

	# if the next param is a String, it is 
	# extra sql metadata.
	if params.first.is_a?(String) 
		sql = params.shift
	else
		sql = nil
	end
	
	# the rest of params are symbols

	@__props = N::SafeArray.new() unless @__props
	
	params.each { |s|
		add_prop(N::Property.new(s, klass, sql))			
	}
end

#sql_index(symbols, pre_sql = nil, post_sql = nil) ⇒ Object

Create an sql index. Wrapper arrount meta :sql_index. This creates ONE sql index. The input parameter is a String (for many symbols) or a Symvol



26
27
28
29
30
31
# File 'lib/n/db/managed.rb', line 26

def	sql_index(symbols, pre_sql = nil, post_sql = nil)
	# gmosx, TODO: meta[:sql_index] should store the full sql clause
	# to allow for custom indices (for example partial indices,
	# hash indices and more)
	meta(:sql_index, [symbols.to_s, pre_sql, post_sql])
end