Skip to content

Instantly share code, notes, and snippets.

@ipoval
Last active December 20, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipoval/6204150 to your computer and use it in GitHub Desktop.
Save ipoval/6204150 to your computer and use it in GitHub Desktop.
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