lib/ronin/ui/cli/model_command.rb in ronin-1.2.0 vs lib/ronin/ui/cli/model_command.rb in ronin-1.3.0

- old
+ new

@@ -30,26 +30,10 @@ class_option :database, :type => :string, :aliases => '-D' # - # The model to query. - # - # @return [DataMapper::Resource] - # The model that will be queried. - # - # @since 1.1.0 - # - # @api semipublic - # - def self.query_model - @query_model ||= if self.superclass < ModelCommand - self.superclass.query_model - end - end - - # # The query options for the command. # # @return [Array<Symbol>] # The query options and their query method names. # @@ -86,24 +70,30 @@ end protected # - # Sets the model to query. + # Sets or gets the model to query. # - # @param [DataMapper::Resource] model + # @param [DataMapper::Resource, nil] model # The model class. # # @return [DataMapper::Resource] # The model that will be queried. # # @since 1.1.0 # # @api semipublic # - def self.model(model) - @query_model = model + def self.model(model=nil) + if model + @model = model + else + @model ||= if superclass < ModelCommand + superclass.model + end + end end # # Defines a query option for the command. # @@ -117,11 +107,11 @@ # # @api semipublic # def self.query_option(name,options={}) query_options << name - class_option(name,options) + class_option name, options end # # Sets up the {Database}. # @@ -152,36 +142,36 @@ # @since 1.0.0 # # @api semipublic # def query - unless self.class.query_model + unless self.class.model raise("query model not defined for #{self.class}") end - query = self.class.query_model.all + query = self.class.model.all self.class.each_query_option do |name| value = options[name] # skip unset options next if value.nil? - if query_model.properties.named?(name) + if model.properties.named?(name) query = query.all(name => value) - elsif query_model.respond_to?(name) - query_method = query_model.method(name) + elsif model.respond_to?(name) + query_method = model.method(name) query = case query_method.arity when 0 query.send(name) when 1 query.send(name,value) else query.send(name,*value) end else - raise("unknown query method or property #{name} for #{query_model}") + raise("unknown query method or property #{name} for #{model}") end end return query end