lib/goaloc/goal.rb in mattknox-goaloc-0.4.4 vs lib/goaloc/goal.rb in mattknox-goaloc-0.4.5

- old
+ new

@@ -5,18 +5,21 @@ # customize the output. class Goal attr_reader :name attr_accessor :associations, :validations, :fields, :options, :routes, :foreign_keys - def initialize(name, route = []) + def initialize(name, route = nil) @name = name.underscore.singularize # TODO: support renaming models self.associations = HashWithIndifferentAccess.new self.validations = [] self.fields = HashWithIndifferentAccess.new self.foreign_keys = HashWithIndifferentAccess.new self.options = { } self.routes = [] # of the form [:classname, [:otherclass, :classname], ...] + if Object.const_defined? self.cs + Object.send(:remove_const, self.cs) + end Object.const_set self.cs, self end # === here are a list of name-ish methods # This returns the name of the foreign key used to refer to this goal. @@ -27,9 +30,17 @@ def s; self.name; end def p; self.name.pluralize; end def cs; self.name.camelize.singularize; end def cp; self.name.camelize.pluralize; end + # ensure that this goal has the given route. + def ensure_route(route) + unless self.routes.member?(route) or route.blank? + self.routes << route + end + end + + # === stuff used to introspect on the goal # thanks to Josh Ladieu for this: it's the array of things needed to get to an instance of this class, if there is a unique set. def resource_tuple # this returns the minimal route to this goal, or nothing, if there is no unambiguous minimal route routelist = self.routes.sort { |x, y| x.length <=> y.length } if routelist.length == 1 #TODO: maybe should deal with a case where there's a simplest route that all the others contain.