Module: Doodle::BaseMethods

Overview

the core module of Doodle - however, to get most facilities provided by Doodle without inheriting from Doodle, include Doodle::Core, not this module

Instance Method Summary collapse

Methods included from ConversionHelper

#convert, #from

Methods included from ValidationHelper

#kind, #must, #validate, #validate!

Methods included from GetterSetter

#after_update

Methods included from ModMarshal

#marshal_dump, #marshal_load

Methods included from ToHash

#to_hash, #to_string_hash

Methods included from SmokeAndMirrors

#inspect, #pretty_print

Methods included from Singleton

#sc_eval, #singleton_class

Instance Method Details

#arg_order(*args) ⇒ Object

define order for positional arguments



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/doodle/base.rb', line 120

def arg_order(*args)
  if args.size > 0
    begin
      args = args.uniq
      args.each do |x|
        __doodle__.handle_error :arg_order, ArgumentError, "#{x} not a Symbol", Doodle::Utils.doodle_caller if !(x.class <= Symbol)
        __doodle__.handle_error :arg_order, NameError, "#{x} not an attribute name", Doodle::Utils.doodle_caller if !doodle.attributes.keys.include?(x)
      end
      __doodle__.arg_order = args
    rescue Exception => e
      __doodle__.handle_error :arg_order, InvalidOrderError, e.to_s, Doodle::Utils.doodle_caller
    end
  else
    __doodle__.arg_order + (__doodle__.attributes.keys - __doodle__.arg_order)
  end
end

#assigned?(name) ⇒ Boolean

return true if attribute has been assigned to

Returns:

  • (Boolean)


157
158
159
# File 'lib/doodle/base.rb', line 157

def assigned?(name)
  ivar_defined?(name)
end

#datatypes(*mods) ⇒ Object

set up global datatypes



35
36
37
38
39
# File 'lib/doodle/base.rb', line 35

def datatypes(*mods)
  mods.each do |mod|
    DataTypeHolder.class_eval { include mod }
  end
end

#default?(name) ⇒ Boolean

return true if attribute has default defined and not yet been assigned to (i.e. still has default value)

Returns:

  • (Boolean)


151
152
153
154
# File 'lib/doodle/base.rb', line 151

def default?(name)
  # FIXME: should this be in DoodleInfo or here?
  __doodle__.attributes[name.to_sym].optional? && !ivar_defined?(name)
end

#doc(*args, &block) ⇒ Object Also known as: doc=

doc add docs to doodle class or attribute



57
58
59
60
61
62
63
# File 'lib/doodle/base.rb', line 57

def doc(*args, &block)
  if args.size > 0
    @doc = *args
  else
    @doc
  end
end

#doodle(*mods, &block) ⇒ Object

vector through this method to get to doodle info or enable global datatypes and provide an interface that allows you to add your own datatypes to this declaration



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/doodle/base.rb', line 44

def doodle(*mods, &block)
  if mods.size == 0 && !block_given?
    __doodle__
  else
    dh = Doodle::DataTypeHolder.new(self)
    mods.each do |mod|
      dh.extend(mod)
    end
    dh.instance_eval(&block)
  end
end

#has(*args, &block) ⇒ Object

has is an extended attr_accessor

simple usage - just like attr_accessor:

class Event
  has :date
end

set default value:

class Event
  has :date, :default => Date.today
end

set lazily evaluated default value:

class Event
  has :date do
    default { Date.today }
  end
end


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/doodle/base.rb', line 88

def has(*args, &block)
  Doodle::Debug.d { [:args, self, self.class, args] }
  params = DoodleAttribute.params_from_args(self, *args)
  Doodle::Debug.d { [:params, self, params] }
  # get specialized attribute class or use default
  attribute_class = params.delete(:using) || DoodleAttribute

  # could this be handled in DoodleAttribute?
  # define getter setter before setting up attribute
  define_getter_setter params[:name], params, &block
  #p [:attribute, attribute_class, params]
  attr = __doodle__.local_attributes[params[:name]] = attribute_class.new(params, &block)

  # FIXME: not sure this is really the right place for this (but
  # right now the only place I can get it to work :)
  if from_defined = params[:from]
    from_defined.each do |k, v|
      Doodle::Debug.d { [:defining, self, k, v]}
      attr.instance_eval { from k, &v }
    end
  end

  if must_defined = params[:must]
    must_defined.each do |k, v|
      attr.instance_eval { must k, &v }
    end
  end

  attr
end

#initialize(*args, &block) ⇒ Object

object can be initialized from a mixture of positional arguments, hash of keyword value pairs and a block which is instance_eval’d



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/doodle/base.rb', line 163

def initialize(*args, &block)
  built_in = Doodle::BuiltIns::BUILTINS.select{ |x| self.kind_of?(x) }.first
  if built_in
    super
  end
  __doodle__.validation_on = true
  #p [:doodle_parent, Doodle.parent, caller[-1]]
  Doodle.context.push(self)
  __doodle__.defer_validation do
    __doodle__.update(*args, &block)
  end
  Doodle.context.pop
  #p [:doodle, __doodle__.__inspect__]
  #p [:doodle, __doodle__.attributes]
  #p [:doodle_parent, __doodle__.parent]
end

#ivar_get(name) ⇒ Object

get an instance variable by symbolic name



145
146
147
# File 'lib/doodle/base.rb', line 145

def ivar_get(name)
  instance_variable_get("@#{name}")
end