Skip to content
Browse files

injecting info into backtraces at field methods so much nicer

  • Loading branch information...
1 parent 6768b78 commit 0ddf6723143d796a02ce12137ae6358e80867d68 Philip (flip) Kromer committed May 19, 2012
View
48 lib/gorillib/builder.rb
@@ -23,11 +23,10 @@ def receive!(*args, &block)
def getset(field, *args, &block)
ArgumentError.check_arity!(args, 0..1)
if args.empty?
- val = read_attribute(field.name)
+ read_attribute(field.name)
else
- val = write_attribute(field.name, args.first)
+ write_attribute(field.name, args.first)
end
- val
end
def getset_member(field, *args, &block)
@@ -94,27 +93,58 @@ def simple_field(field_name, type, options={})
protected
def define_attribute_getset(field)
- define_meta_module_method(field.name, field.visibility(:reader)) do |*args, &block|
- getset(field, *args, &block)
+ field_name = field.name; type = field.type
+ define_meta_module_method(field_name, field.visibility(:reader)) do |*args, &block|
+ begin
+ getset(field, *args, &block)
+ rescue StandardError => err
+ err.backtrace.
+ detect{|l| l.include?(__FILE__) && l.include?("in define_attribute_getset'") }.
+ gsub!(/define_attribute_getset'/, "define_attribute_getset for #{self.class}.#{field_name} type #{type} on #{args}'"[0..300]) rescue nil
+ raise
+ end
end
end
def define_member_getset(field)
- define_meta_module_method(field.name, field.visibility(:reader)) do |*args, &block|
- getset_member(field, *args, &block)
+ field_name = field.name; type = field.type
+ define_meta_module_method(field_name, field.visibility(:reader)) do |*args, &block|
+ begin
+ getset_member(field, *args, &block)
+ rescue StandardError => err
+ err.backtrace.
+ detect{|l| l.include?(__FILE__) && l.include?("in define_member_getset'") }.
+ gsub!(/define_member_getset'/, "define_member_getset for #{self.class}.#{field_name} type #{type} on #{args}'"[0..300]) rescue nil
+ raise
+ end
end
end
def define_collection_getset(field)
+ field_name = field.name; item_type = field.item_type
define_meta_module_method(field.singular_name, field.visibility(:collection_getset)) do |*args, &block|
- getset_collection_item(field, *args, &block)
+ begin
+ getset_collection_item(field, *args, &block)
+ rescue StandardError => err
+ err.backtrace.
+ detect{|l| l.include?(__FILE__) && l.include?("in define_collection_getset'") }.
+ gsub!(/define_collection_getset'/, "define_collection_getset for #{self.class}.#{field_name} c[#{item_type}] on #{args}'"[0..300]) rescue nil
+ raise
+ end
end
end
def define_collection_tester(field)
plural_name = field.plural_name
define_meta_module_method("has_#{field.singular_name}?", field.visibility(:collection_tester)) do |item_key|
- collection_of(plural_name).include?(item_key)
+ begin
+ collection_of(plural_name).include?(item_key)
+ rescue StandardError => err
+ err.backtrace.
+ detect{|l| l.include?(__FILE__) && l.include?("in define_collection_tester'") }.
+ gsub!(/define_collection_tester'/, "define_collection_tester for #{self.class}.#{field_name} type #{type}'") rescue nil
+ raise
+ end
end
end
View
14 lib/gorillib/model.rb
@@ -213,6 +213,7 @@ def typename
def receive(attrs={}, &block)
return nil if attrs.nil?
return attrs if attrs.is_a?(self)
+ Gorillib::Model::Validate.hashlike!("attributes for #{self}", attrs)
klass = attrs.has_key?(:_type) ? Gorillib::Factory(attrs[:_type]) : self
warn "factory #{self} doesn't match type specified in #{attrs}" unless klass <= self
obj = klass.new
@@ -306,9 +307,16 @@ def define_attribute_receiver(field)
field_name = field.name
type = field.type
define_meta_module_method("receive_#{field_name}", field.visibility(:receiver)) do |val|
- val = type.receive(val)
- write_attribute(field_name, val)
- self
+ begin
+ val = type.receive(val)
+ write_attribute(field_name, val)
+ self
+ rescue StandardError => err
+ err.backtrace.
+ detect{|l| l.include?(__FILE__) && l.include?("in define_attribute_receiver'") }.
+ gsub!(/define_attribute_receiver'/, "define_attribute_receiver for #{self.class}.#{field_name} type #{type} on #{val}'"[0..300]) rescue nil
+ raise
+ end
end
end
View
3 lib/gorillib/model/field.rb
@@ -56,10 +56,9 @@ def to_s
name.to_s
end
- def self.factory_for(type)
+ def factory_for(type)
Gorillib::Factory(type)
end
- def factory_for(type) ; self.class.factory_for(type) ; end
# @return [String] Human-readable presentation of the field definition
def inspect
View
8 lib/gorillib/model/validate.rb
@@ -5,17 +5,17 @@ module Validate
VALID_NAME_RE = /\A[A-Za-z_][A-Za-z0-9_]+\z/
def identifier!(name)
- raise TypeError, "can't convert #{name.class} into Symbol" unless name.respond_to? :to_sym
- raise ArgumentError, "Name must start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]" unless name =~ VALID_NAME_RE
+ raise TypeError, "can't convert #{name.class} into Symbol", caller unless name.respond_to? :to_sym
+ raise ArgumentError, "Name must start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]", caller unless name =~ VALID_NAME_RE
end
def hashlike!(desc, val)
return true if val.respond_to?(:[]) && val.respond_to?(:has_key?)
- raise ArgumentError, "#{desc} should be something that behaves like a hash: {#{val.inspect}}"
+ raise ArgumentError, "#{desc} should be something that behaves like a hash: {#{val.inspect}}", caller
end
def included_in!(desc, val, colxn)
- raise ArgumentError, "#{desc} must be one of #{colxn.inspect}: got #{val.inspect}" unless colxn.include?(val)
+ raise ArgumentError, "#{desc} must be one of #{colxn.inspect}: got #{val.inspect}", caller unless colxn.include?(val)
end
end
end
View
4 spec/support/hashlike_helper.rb
@@ -69,8 +69,8 @@ module HashlikeHelper
# :cycle, :partition,
# ]
#
- # p Enumerable.public_instance_methods.map(&:to_sym) - ENUMERABLE_METHODS
- # p ENUMERABLE_METHODS - Enumerable.public_instance_methods.map(&:to_sym)
+ # Enumerable.public_instance_methods.map(&:to_sym) - ENUMERABLE_METHODS
+ # ENUMERABLE_METHODS - Enumerable.public_instance_methods.map(&:to_sym)
# extra: [:member?, :enum_slice, :reject, :select, :include?, :enum_cons, :enum_with_index]
# missing: [:each_entry, :each_with_object, :collect_concat, :flat_map, :chunk, :slice_before]
end

0 comments on commit 0ddf672

Please sign in to comment.
Something went wrong with that request. Please try again.