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

Direct Known Subclasses

AttributeCollector

Constant Summary

Constants inherited from Doodle

Helper

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Doodle

context, 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



939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/doodle.rb', line 939

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", (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", (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_name]
      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

#default_defined?Boolean

has default been defined?

Returns:

  • (Boolean)


1015
1016
1017
# File 'lib/doodle.rb', line 1015

def default_defined?
  ivar_defined?(:default)
end

#init_defined?Boolean

has default been defined?

Returns:

  • (Boolean)


1020
1021
1022
# File 'lib/doodle.rb', line 1020

def init_defined?
  ivar_defined?(:init)
end

#optional?Boolean

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

Returns:

  • (Boolean)


1025
1026
1027
# File 'lib/doodle.rb', line 1025

def optional?
  default_defined? or init_defined?
end

#required?Boolean

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

Returns:

  • (Boolean)


1030
1031
1032
1033
# File 'lib/doodle.rb', line 1030

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



1011
1012
# File 'lib/doodle.rb', line 1011

def validate!(all = true)
end