lib/lazy_resource/attributes.rb in lazy_resource-0.4.0 vs lib/lazy_resource/attributes.rb in lazy_resource-0.5.0

- old
+ new

@@ -15,11 +15,11 @@ define_attribute_methods [name] end def fetch_all self.resource_queue.send_to_request_queue! if self.respond_to?(:resource_queue) - self.request_queue.run if self.respond_to?(:request_queue) + self.request_queue.run if self.respond_to?(:request_queue) && self.request_queue.items_queued? end def attributes @attributes ||= {} end @@ -53,51 +53,60 @@ end RUBY end def create_getter(name, type, options={}) + line = __LINE__ + 2 method = <<-RUBY def #{name} self.class.fetch_all if !fetched + raise self.request_error if self.request_error.present? RUBY + route = options[:using] || options[:route] + if options[:using] + LazyResource.deprecate("Attribute option :using is deprecated. Please use :route instead.", __FILE__, __LINE__) + end + + if route.is_a?(Proc) + route_method_name = "_#{name}_route".to_sym + define_method(route_method_name, route) + protected(route_method_name) + route = route_method_name + end + if type.is_a?(Array) && type.first.include?(LazyResource::Resource) - if options[:using].nil? + if route.nil? method << <<-RUBY if @#{name}.nil? @#{name} = #{type.first}.where(:"\#{self.class.element_name}_id" => self.primary_key) end RUBY else method << <<-RUBY - return [] if self.#{options[:using]}.nil? - if @#{name}.nil? - @#{name} = LazyResource::Relation.new(#{type.first}, :fetched => true) - request = LazyResource::Request.new(self.#{options[:using]}, @#{name}, :headers => @#{name}.headers) - @#{name}.fetched = false - self.class.request_queue.queue(request) + route = self.respond_to?("#{route}") ? self.send("#{route}") : "#{route}" + route = route.is_a?(Proc) ? route.call : route + @#{name} = #{type.first}.where(:"\#{self.class.element_name}_id" => self.primary_key, :_route => route) end @#{name} RUBY end elsif type.include?(LazyResource::Resource) - if options[:using].nil? + if route.nil? method << <<-RUBY if @#{name}.nil? @#{name} = #{type}.where(:"\#{self.class.element_name}_id" => self.primary_key) end RUBY else method << <<-RUBY - return [] if self.#{options[:using]}.nil? - if @#{name}.nil? - @#{name} = #{type}.new - request = LazyResource::Request.new(self.#{options[:using]}, @#{name}) - self.class.request_queue.queue(request) + route = self.respond_to?("#{route}") ? self.send("#{route}") : "#{route}" + route = route.is_a?(Proc) ? route.call : route + @#{name} = #{type}.where(:"\#{self.class.element_name}_id" => self.primary_key, :_route => route) end @#{name} RUBY end @@ -106,10 +115,10 @@ method << <<-RUBY @#{name} end RUBY - class_eval method, __FILE__, __LINE__ + 1 + class_eval method, __FILE__, line end def create_question(name, type, options={}) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name}?