Instantly share code, notes, and snippets.

@ipoval /README.md
Last active Dec 20, 2015

Embed
What would you like to do?
Add a custom method definition to Test Blueprints in Machinist::Mongoid. It is very handy in the tests to have extra helper methods directly on the Test Blueprints without defining those in the corresponding Model class.

INSTALLATION

echo "gem 'machinist_blueprint_with_method',\
      git: 'git://gist.github.com/6204150.git',\
      group: 'test'" >> Gemfile

EXAMPLE OF USAGE

./spec/fixtures/blueprints.rb

Upload.blueprint do
  index 0
  relative_path "/"
  bytes 100.bytes
  asset { {} }
  ##
  # Can declare a method for the object created with this blueprint
  #
  with_method(:upload_complete!) { update_attribute :remaining_bytes, 0 }
end

./spec/upload_spec.rb

let(:complete_upload) { Upload.make(user: peter.id, remaining_bytes: 100.bytes).upload_complete! }
Gem::Specification.new do |s|
s.name = 'machinist_blueprint_with_method'
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.author = '@ipoval'
s.email = 'ipoval@ya.ru'
s.summary = 'Blueprint With Method for Machinist::Mongoid'
s.description = 'Blueprint With Method for Machinist::Mongoid'
s.files = ['machinist_blueprint_with_method.rb']
s.require_path = '.'
end
if defined?(Machinist::Lathe)
module Machinist
module BlueprintWithMethod
def with_method(name, &block)
object.singleton_class.class_eval do
define_method(name, &block)
end
end
end
end
Machinist::Lathe.module_eval {
include Machinist::BlueprintWithMethod
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment