Module: PreInitialize::InstanceMethods

Defined in:
lib/shattered_support/pre_initialize.rb

Instance Method Summary collapse

Instance Method Details

#attr_accessor(*args) ⇒ Object

attr helpers. These are instance level attr_* functions.

attr_accessor accepts a value as the se puts modcond argument



82
83
84
85
# File 'lib/shattered_support/pre_initialize.rb', line 82

def attr_accessor(*args)
  name, value = args
  attr_define(:accessor, name, value)
end

#attr_reader(*args) ⇒ Object

attr helpers. These are instance level attr_* functions.



68
69
70
71
# File 'lib/shattered_support/pre_initialize.rb', line 68

def attr_reader(*args)
  name, value = args
  attr_define(:reader, name, value)
end

#attr_writer(*args) ⇒ Object

attr helpers. These are instance level attr_* functions.

attr_writer accepts a value as the second argument



75
76
77
78
# File 'lib/shattered_support/pre_initialize.rb', line 75

def attr_writer(*args)
  name, value = args
  attr_define(:writer, name, value)
end

#call_object_function(actor, action, params, evaluate_symbols = true) ⇒ Object

TODO: This is bad - this method needs to be refactored and/or nuked.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/shattered_support/pre_initialize.rb', line 125

def call_object_function(actor, action, params, evaluate_symbols=true)
  begin
    if params.is_a?(Symbol) && evaluate_symbols 
      params = eval(params.to_s) 
    end
  rescue NameError
    puts "It is not advisable to pass #{params.inspect} to #{action.inspect} for #{actor.inspect}."
    puts "  It will try to be evaluated.  Use a string instead."
  end
  begin
  	if(actor.is_a?(String) || actor.is_a?(Symbol))
    	sendee = eval(actor.to_s)
  	else
  		sendee = actor
		end
    if(params.is_a? Array)
      sendee.send( action.to_sym, *params )
    else
      sendee.send( action.to_sym, params )
    end
  rescue NoMethodError, ArgumentError => bang
    message="Error upon #{actor.to_s}.send(#{action.to_sym.inspect}, #{params.inspect}):\n\r  #{bang.class}: \n\r\t #{bang.message}\n\r #{bang.backtrace[0]}\n\r"
    bang = Shattered::MetaMethodError.new(bang,message)
    raise bang
  end
end

#call_object_function_for_each_key(actor, options, evaluate_symbols = true) ⇒ Object



118
119
120
121
122
# File 'lib/shattered_support/pre_initialize.rb', line 118

def call_object_function_for_each_key( actor, options, evaluate_symbols=true )
  options.each_pair do |action, params|
    call_object_function actor, "#{action}=", params, evaluate_symbols
  end
end

#each_init_value(name) ⇒ Object

Used in pre_initialize



96
97
98
99
100
101
102
# File 'lib/shattered_support/pre_initialize.rb', line 96

def each_init_value(name) #:nodoc:
  startup_attributes = self.class.read_inheritable_attribute( name ) || []
  startup_attributes.each do |actor, options|
    next if options.nil?
    yield actor, options
  end
end

#pre_initializeObject

See Shattered::ClassMethods#before_init_call for usage.



90
91
92
93
# File 'lib/shattered_support/pre_initialize.rb', line 90

def pre_initialize #:nodoc:
  pre_initialize_set
  pre_initialize_call
end

#pre_initialize_callObject

Used in pre_initialize for before_init_call



112
113
114
115
116
# File 'lib/shattered_support/pre_initialize.rb', line 112

def pre_initialize_call #:nodoc:
    each_init_value(BEFORE_INIT_CALL_VALUES) do |function, args|
      call_object_function( :self, function, args )  
    end
end

#pre_initialize_setObject

Used in pre_initialize for before_init_set



105
106
107
108
109
# File 'lib/shattered_support/pre_initialize.rb', line 105

def pre_initialize_set #:nodoc:
  each_init_value(BEFORE_INIT_SET_VALUES) do |variable, options|
    call_object_function_for_each_key( variable, options )  
  end
end