Class: Doodle::DoodleAttribute

Inherits:
Doodle
  • Object
show all
Defined in:
lib/doodle.rb

Overview

Attribute is itself a Doodle object that is created by #has and added to the #attributes collection in an object’s DoodleInfo

It is used to provide a context for defining #must and #from rules

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Doodle

context, parent, raise_exception_on_error, raise_exception_on_error=

Methods included from Core

included

Class Method Details

.params_from_args(owner, *args) ⇒ Object

rewrite rules for the argument list to #has



1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
# File 'lib/doodle.rb', line 1249

def params_from_args(owner, *args)
  key_values, positional_args = args.partition{ |x| x.kind_of?(Hash)}
  params = { }
  if positional_args.size > 0
    name = positional_args.shift
    case name
      # has Person --> has :person, :kind => Person
    when Class
      params[:name] = Utils.snake_case(name.to_s.split(/::/).last)
      params[:kind] = name
    else
      params[:name] = name.to_s.to_sym
    end
  end
  params = key_values.inject(params){ |acc, item| acc.merge(item)}
  #DBG: Doodle::Debug.d { [:has, self, self.class, params] }
  if !params.key?(:name)
    __doodle__.handle_error name, ArgumentError, "#{self.class} must have a name", Doodle::Utils.doodle_caller
    params[:name] = :__ERROR_missing_name__
  else
    # ensure that :name is a symbol
    params[:name] = params[:name].to_sym
  end
  name = params[:name]
  __doodle__.handle_error name, ArgumentError, "#{self.class} has too many arguments", Doodle::Utils.doodle_caller if positional_args.size > 0

  if collector = params.delete(:collect)
    if !params.key?(:using)
      if params.key?(:key)
        params[:using] = KeyedAttribute
      else
        params[:using] = AppendableAttribute
      end
    end
    # this in generic CollectorAttribute class
    # collector from(Hash)
    if collector.kind_of?(Hash)
      collector_name, collector_class = collector.to_a[0]
    else
      # if Capitalized word given, treat as classname
      # and create collector for specific class
      collector_class = collector.to_s
      #p [:collector_klass, collector_klass]
      collector_name = Utils.snake_case(collector_class.split(/::/).last)
      #p [:collector_name, collector_class, collector_name]
      # FIXME: sanitize class name (make this a Utils function)
      collector_class = collector_class.gsub(/#<Class:0x[a-fA-F0-9]+>::/, '')
      if collector_class !~ /^[A-Z]/
        collector_class = nil
      end
      #!p [:collector_klass, collector_klass, params[:init]]
    end
    params[:collector_class] = collector_class
    params[:collector_name] = collector_name
  end
  params[:doodle_owner] = owner
  #p [:params, owner, params]
  params
end

Instance Method Details

#abstractObject

temporarily fake existence of abstract attribute - later has :abstract overrides this



1352
1353
1354
# File 'lib/doodle.rb', line 1352

def abstract
  @abstract = false
end

#default_defined?Boolean

has default been defined?

Returns:

  • (Boolean)


1327
1328
1329
# File 'lib/doodle.rb', line 1327

def default_defined?
  ivar_defined?(:default)
end

#init_defined?Boolean

has default been defined?

Returns:

  • (Boolean)


1332
1333
1334
# File 'lib/doodle.rb', line 1332

def init_defined?
  ivar_defined?(:init)
end

#optional?Boolean

is this attribute optional? true if it has a default defined for it

Returns:

  • (Boolean)


1337
1338
1339
# File 'lib/doodle.rb', line 1337

def optional?
  default_defined? or init_defined?
end

#readonlyObject



1355
1356
1357
# File 'lib/doodle.rb', line 1355

def readonly
  false
end

#required?Boolean

an attribute is required if it has no default or initial value defined for it

Returns:

  • (Boolean)


1342
1343
1344
1345
# File 'lib/doodle.rb', line 1342

def required?
  # d { [:default?, self.class, self.name, instance_variable_defined?("@default"), @default] }
  !optional?
end

#validate!(all = true) ⇒ Object

hack: bump off validate! for Attributes - maybe better way of doing this however, without this, tries to validate Attribute to :kind specified, e.g. if you have

has :date, :kind => Date

it will fail because Attribute is not a kind of Date - obviously, I have to think about this some more :S

at least, I could hand roll a custom validate! method for Attribute



1323
1324
# File 'lib/doodle.rb', line 1323

def validate!(all = true)
end