Module: Datoki::Def_Field
- Defined in:
- lib/datoki.rb
Overview
class self ===
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#ons ⇒ Object
readonly
Returns the value of attribute ons.
Instance Method Summary collapse
- #create(h = {}) ⇒ Object
- #disable(*props) ⇒ Object
-
#enable(*props) ⇒ Object
def.
- #equal_to(*args) ⇒ Object
- #field(*args) ⇒ Object
- #field?(*args) ⇒ Boolean
- #href(*args) ⇒ Object
- #html_escape ⇒ Object
- #included_in(arr) ⇒ Object
- #initialize_def_field ⇒ Object
- #inspect_field?(target, name, *args) ⇒ Boolean
- #match(*args) ⇒ Object
- #not_match(*args) ⇒ Object
- #on(action, meth_name_sym) ⇒ Object
- #primary_key ⇒ Object
- #record_errors ⇒ Object
- #record_errors? ⇒ Boolean
- #schema(*args) ⇒ Object
-
#schema_match(target = :current) ⇒ Object
def field.
- #schema_match? ⇒ Boolean
- #set_to(*args) ⇒ Object
- #table(name) ⇒ Object
- #text(*args) ⇒ Object
- #type(name, *args) ⇒ Object
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
47 48 49 |
# File 'lib/datoki.rb', line 47 def fields @fields end |
#ons ⇒ Object (readonly)
Returns the value of attribute ons.
47 48 49 |
# File 'lib/datoki.rb', line 47 def ons @ons end |
Instance Method Details
#create(h = {}) ⇒ Object
439 440 441 442 |
# File 'lib/datoki.rb', line 439 def create h = {} r = new r.create h end |
#disable(*props) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/datoki.rb', line 385 def disable *props props.each { |prop| case prop when :min, :max field.delete prop when :strip, :null field[:allow][prop] = false else field[:cleaners][prop] = false end } end |
#enable(*props) ⇒ Object
def
374 375 376 377 378 379 380 381 382 383 |
# File 'lib/datoki.rb', line 374 def enable *props props.each { |prop| case prop when :strip, :null field[:allow][prop] = true else field[:cleaners][prop] = true end } end |
#equal_to(*args) ⇒ Object
403 404 405 406 |
# File 'lib/datoki.rb', line 403 def equal_to *args field[:cleaners][:equal_to] ||= [] field[:cleaners][:equal_to].concat args end |
#field(*args) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/datoki.rb', line 140 def field *args return fields[@current_field] if args.empty? return fields[args.first] unless block_given? name = args.first fail "#{name.inspect} already defined." if fields[name] fields[name] = { :name => name, :type => :unknown, :english_name => name.to_s.freeze, :allow => {:null => false}, :disable => {}, :cleaners => {}, :on => {} } @current_field = name if field? :chars field[:allow][:strip] = true end if schema[name] if schema[name].has_key? :max_length fields[name][:max] = schema[name][:max_length] end end yield fail("Type not specified for #{name.inspect}") if field[:type] == :unknown # === check :allow_null and :min are not both set. if field?(:chars) && field[:allow][:null] && field.has_key?(:min) && field[:min] < 1 fail "#{field[:type].inspect} can't be both: allow :null && :min = #{field[:min]}" end # === Ensure schema matches with field definition: schema_match field[:html_escape] = case when field[:html_escape] field[:html_escape] when field?(:numeric) :number when field?(:chars) :string else fail "Unknown html_escape for: #{field[:name].inspect}" end @current_field = nil end |
#field?(*args) ⇒ Boolean
136 137 138 |
# File 'lib/datoki.rb', line 136 def field? *args inspect_field?(:type, field[:name], *args) end |
#href(*args) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/datoki.rb', line 303 def href *args field[:html_escape] = :href case args.map(&:class) when [] varchar 0, 255 when [NilClass] varchar nil, 1, (schema[field[:name]] ? schema[field[:name]][:max_length] : 255) else varchar *args end end |
#html_escape ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/datoki.rb', line 97 def html_escape @html_escape ||= begin fields.inject({}) { |memo, (name, )| memo[name] = [:html_escape] memo } end end |
#included_in(arr) ⇒ Object
408 409 410 411 |
# File 'lib/datoki.rb', line 408 def included_in arr field[:cleaners][:included_in] ||= [] field[:cleaners][:included_in].concat arr end |
#initialize_def_field ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/datoki.rb', line 49 def initialize_def_field @record_errors = false @ons = {} @fields = {} @current_field = nil @schema = {} @schema_match = false @table_name = nil name = self.to_s.downcase.to_sym table(name) if Datoki.db.tables.include?(name) end |
#inspect_field?(target, name, *args) ⇒ Boolean
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/datoki.rb', line 123 def inspect_field? target, name, *args case target when :type = fields[name] fail "Unknown field: #{name.inspect}" unless return true if args.include?([:type]) return true if args.include?(:chars) && Char_Types.include?([:type]) args.include?(:numeric) && Numeric_Types.include?([:type]) else fail "Unknown arg: #{target.inspect}" end end |
#match(*args) ⇒ Object
426 427 428 429 430 |
# File 'lib/datoki.rb', line 426 def match *args fail "Not allowed for #{field[:type].inspect}" unless field?(:chars) field[:cleaners][:match] ||= [] field[:cleaners][:match] << args end |
#not_match(*args) ⇒ Object
432 433 434 435 436 437 |
# File 'lib/datoki.rb', line 432 def not_match *args fail "Not allowed for #{field[:type].inspect}" unless field?(:chars) field[:cleaners][:not_match] ||= [] field[:cleaners][:not_match] << args self end |
#on(action, meth_name_sym) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/datoki.rb', line 274 def on action, meth_name_sym fail "Invalid action: #{action.inspect}" unless Actions.include? action if field field[:on][action] ||= {} field[:on][action][meth_name_sym] = true else @ons[action] ||= {} @ons[action][meth_name_sym] = true end self end |
#primary_key ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/datoki.rb', line 286 def primary_key field[:primary_key] = true if field?(:unknown) if schema[field[:name]] type schema[field[:name]][:type] else type :integer end end true end |
#record_errors ⇒ Object
69 70 71 |
# File 'lib/datoki.rb', line 69 def record_errors @record_errors = true end |
#record_errors? ⇒ Boolean
65 66 67 |
# File 'lib/datoki.rb', line 65 def record_errors? @record_errors end |
#schema(*args) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/datoki.rb', line 106 def schema *args case args.size when 0 @schema when 1 result = @schema[args.first] fail "Unknown field: #{args.first.inspect}" unless result result else fail "Unknown args: #{args.inspect}" end end |
#schema_match(target = :current) ⇒ Object
def field
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/datoki.rb', line 196 def schema_match target = :current return true if !@table_name return true if schema_match? if target == :all # === do a schema match on entire table schema.each { |name, db_schema| orig_field = @current_field @current_field = name schema_match @current_field = orig_field } @schema_match = true return true end # === if target name = @current_field db_schema = schema[@current_field] if db_schema && !field && db_schema[:type] != :datetime fail Schema_Conflict, "#{name}: #{name.inspect} has not been defined." end return true if field[:schema_match] if db_schema[:allow_null] != field[:allow][:null] fail Schema_Conflict, "#{name}: :allow_null: #{db_schema[:allow_null].inspect} != #{field[:allow][:null].inspect}" end if field?(:chars) if !field[:min].is_a?(Numeric) || field[:min] < 0 fail ":min not properly defined for #{name.inspect}: #{field[:min].inspect}" end if !field[:max].is_a?(Numeric) fail ":max not properly defined for #{name.inspect}: #{field[:max].inspect}" end end if db_schema.has_key?(:max_length) if field[:max] != db_schema[:max_length] fail Schema_Conflict, "#{name}: :max: #{db_schema[:max_length].inspect} != #{field[:max].inspect}" end end if !!db_schema[:primary_key] != !!field[:primary_key] fail Schema_Conflict, "#{name}: :primary_key: #{db_schema[:primary_key].inspect} != #{field[:primary_key].inspect}" end # === match :type db_type = Datoki.db_type_to_ruby db_schema[:db_type], db_schema[:type] type = field[:type] if db_type != type fail Schema_Conflict, "#{name}: :type: #{db_type.inspect} != #{type.inspect}" end # === match :max_length db_max = db_schema[:max_length] max = field[:max] if !db_max.nil? && db_max != max fail Schema_Conflict, "#{name}: :max_length: #{db_max.inspect} != #{max.inspect}" end # === match :min_length db_min = db_schema[:min_length] min = field[:min] if !db_min.nil? && db_min != min fail Schema_Conflict, "#{name}: :min_length: #{db_min.inspect} != #{min.inspect}" end # === match :allow_null if db_schema[:allow_null] != field[:allow][:null] fail Schema_Conflict, "#{name}: :allow_null: #{db_schema[:allow_null].inspect} != #{field[:allow][:null].inspect}" end field[:schema_match] = true end |
#schema_match? ⇒ Boolean
61 62 63 |
# File 'lib/datoki.rb', line 61 def schema_match? @schema_match end |
#set_to(*args) ⇒ Object
398 399 400 401 |
# File 'lib/datoki.rb', line 398 def set_to *args field[:cleaners][:set_to] ||= [] field[:cleaners][:set_to].concat args end |
#table(name) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/datoki.rb', line 73 def table name if !@schema.empty? || @table_name fail "Schema/table already defined: #{@table_name.inspect}" end db_schema = Datoki.db.schema(name) if !db_schema fail "Schema not found for: #{name.inspect}" end @table_name = name db_schema.each { |pair| @schema[pair.first] = pair.last } if @schema.empty? @schema_match = true end schema end |
#text(*args) ⇒ Object
299 300 301 |
# File 'lib/datoki.rb', line 299 def text *args type :text, *args end |
#type(name, *args) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/datoki.rb', line 323 def type name, *args field[:type] = name if field? :chars enable :strip if field?(:text) field[:max] ||= 4000 else field[:max] ||= 255 end if schema[name] && !schema[name][:allow_null] field[:min] = 1 end end # === if field? :chars case args.map(&:class) when [] # do nothing when [Array] field[:options] = args.first enable(:null) if field[:options].include? nil disable :min, :max when [NilClass] if field?(:chars) fail "A :min and :max is required for String fields." end enable :null when [NilClass, Fixnum, Fixnum] field[:allow][:null] = true field[:min] = args[-2] field[:max] = args.last when [Fixnum, Fixnum] field[:min], field[:max] = args else fail "Unknown args: #{args.inspect}" end # === case end |