2

I just stumbled across this Parser gem.

What I want to do is extract a specific method from a long file.

One example is this has_many method from Rails:

 def has_many(name, scope = nil, options = {}, &extension)
    reflection = Builder::HasMany.build(self, name, scope, options, &extension)
    Reflection.add_reflection self, name, reflection
  end

The only issue is, I need to be able to also extract another version of that same method (from an earlier time), that is more verbose and complex. That version looks like this:

def has_many(association_id, options = {})
    validate_options([ :foreign_key, :class_name, :exclusively_dependent, :dependent, :conditions, :order, :finder_sql ], options.keys)
    association_name, association_class_name, association_class_primary_key_name =
          associate_identification(association_id, options[:class_name], options[:foreign_key])

    require_association_class(association_class_name)

    if options[:dependent] and options[:exclusively_dependent]
      raise ArgumentError, ':dependent and :exclusively_dependent are mutually exclusive options.  You may specify one or the other.' # ' ruby-mode
    elsif options[:dependent]
      module_eval "before_destroy '#{association_name}.each { |o| o.destroy }'"
    elsif options[:exclusively_dependent]
      module_eval "before_destroy { |record| #{association_class_name}.delete_all(%(#{association_class_primary_key_name} = '\#{record.id}')) }"
    end

    define_method(association_name) do |*params|
      force_reload = params.first unless params.empty?
      association = instance_variable_get("@#{association_name}")
      if association.nil?
        association = HasManyAssociation.new(self,
          association_name, association_class_name,
          association_class_primary_key_name, options)
        instance_variable_set("@#{association_name}", association)
      end
      association.reload if force_reload
      association
    end

    # deprecated api
    deprecated_collection_count_method(association_name)
    deprecated_add_association_relation(association_name)
    deprecated_remove_association_relation(association_name)
    deprecated_has_collection_method(association_name)
    deprecated_find_in_collection_method(association_name)
    deprecated_find_all_in_collection_method(association_name)
    deprecated_create_method(association_name)
    deprecated_build_method(association_name)
  end

So how do I successfully extract both of those methods, using that gem?

The ReadMe for the Gem is not very friendly to complete parsing-noobs.

marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • Is it the same question as http://stackoverflow.com/questions/38628521/how-do-i-extract-just-a-specific-portion-of-a-code-snippet-from-multiple-files ? – Wiktor Stribiżew Jul 28 '16 at 08:28
  • It is similar, but not identical. In that other question, I didn't know that I could use this parser. This question is tailored specifically to that gem - as opposed to the other question is much more general. – marcamillion Jul 28 '16 at 08:47

0 Answers0