class RailsZen::ModelAction

Attributes

is_class_action[W]

Public Class Methods

new(name, is_class_action, model) click to toggle source
# File lib/rails_zen/model_action.rb, line 9
def initialize(name, is_class_action, model)
  @name = name
  @model = model
  @is_class_action = is_class_action
  @arg_names = []
  @args = []
end

Public Instance Methods

write!() click to toggle source
# File lib/rails_zen/model_action.rb, line 17
def write!
  get_necessary_info

  m = RailsZen::WriteToModel.new
  m.model_name = @model
  m.adding_to_file!(action_string)

  s = RailsZen::WriteToSpec.new
  s.model_name = @model

  unless File.foreach("spec/models/#{@model}_spec.rb").grep(factory_girl_match).any? #check for factory
    s.adding_to_file!(factory_method)
  end
  s.adding_to_file!(action_spec_string)
end

Private Instance Methods

action_spec_string() click to toggle source
# File lib/rails_zen/model_action.rb, line 65
def action_spec_string
  if @is_class_action
  %{
  it { is_expected.to respond_to :#{@name}}

  describe ".#{@name}" do
    it "#{@functionality}" do
      expect(#{@model.capitalize}.#{@name}(#{sample_arguments})).to eq \"#{@expected}\"
    end
  end

  }
  else
    %{
    describe "##{@name}" do
      it "#{@functionality}" do
        expect(#{@model}.#{@name}(#{sample_arguments})).to eq \"#{@expected}\"
      end
    end

    }
  end
end
action_string() click to toggle source
# File lib/rails_zen/model_action.rb, line 58
def action_string
  %{
  def #{method_with_args}
  end
  }
end
arguments() click to toggle source
# File lib/rails_zen/model_action.rb, line 99
def arguments
  @arg_names.join(", ")
end
factory_girl_match() click to toggle source
# File lib/rails_zen/model_action.rb, line 35
def factory_girl_match
  Regexp.new("FactoryGirl.create(:#{@model})")
end
factory_method() click to toggle source
# File lib/rails_zen/model_action.rb, line 102
def factory_method
  "let(:#{@model}) { FactoryGirl.create(:#{@model}) }"
end
get_necessary_info() click to toggle source
# File lib/rails_zen/model_action.rb, line 39
def get_necessary_info
  say "What does your method do? (Please don't use 'it' or 'should' in your definition)\n"
  @functionality = ask("Eg: returns sum of 2 numbers\n")

  say "\n\n------------ARGUMENTS-----------------\n"

  say "\nWhat name would you give your arguments for the #{@name} method? eg: num1,num2\n"

  @arg_names = ask("Enter (comma sep) list", lambda { |str| str.split(/,\s*/) })

  if @arg_names.any?
    say "\nGive example arguments inputs\n"
    @args =  ask("Enter (comma sep list). Eg: 1,2 ", lambda { |str| str.split(/,\s*/) })
  end


  @expected = ask("Enter the expected output for the previously entered arguments. eg: 3")
end
method_with_args() click to toggle source
# File lib/rails_zen/model_action.rb, line 88
def method_with_args
  if @is_class_action
    "self.#{@name}(#{arguments})"
  else
    "#{@name}(#{arguments})"
  end
end
sample_arguments() click to toggle source
# File lib/rails_zen/model_action.rb, line 96
def sample_arguments
  @args.join(", ")
end