Module: Puzzlize::ModelInstanceMethods
- Defined in:
- lib/puzzlize/model_instance_methods.rb
Instance Method Summary collapse
- #default_puzzle_image_path ⇒ Object
- #default_puzzle_image_url ⇒ Object
- #determine_parameters ⇒ Object
- #make_all_piece ⇒ Object
- #make_puzzle ⇒ Object
- #make_single_piece(row_x, row_y) ⇒ Object
- #piece_file_name(row_x, row_y, file_location) ⇒ Object
- #puzzle_created? ⇒ Boolean
- #puzzle_images_urls ⇒ Object
- #puzzle_pieces_names ⇒ Object
-
#update_piece_information ⇒ Object
this is isolated because supermodel treats update_attributes the same as save ActiveRecord does not do this.
Instance Method Details
#default_puzzle_image_path ⇒ Object
7 8 9 |
# File 'lib/puzzlize/model_instance_methods.rb', line 7 def default_puzzle_image_path self.send(self.class.get_puzzlize_attribute_name).path(:medium) end |
#default_puzzle_image_url ⇒ Object
3 4 5 |
# File 'lib/puzzlize/model_instance_methods.rb', line 3 def default_puzzle_image_url self.send(self.class.get_puzzlize_attribute_name).url(:medium) end |
#determine_parameters ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/puzzlize/model_instance_methods.rb', line 43 def determine_parameters @file_location = self.default_puzzle_image_path @source_file = Magick::Image.read(@file_location).first @cutter = Puzzlize::Cutter.new @cutter.piece_size = (@source_file.columns * 0.25).to_i @cutter.image_width = @source_file.columns @cutter.image_height = @source_file.rows @source_file = @source_file.crop(*(@cutter.piece_fitting_diamentions + [true])) end |
#make_all_piece ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/puzzlize/model_instance_methods.rb', line 117 def make_all_piece @cutter.vertical_pieces.times do |y_axis| @cutter.horizontal_pieces.times do |x_axis| make_single_piece(x_axis, y_axis) end end end |
#make_puzzle ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/puzzlize/model_instance_methods.rb', line 31 def make_puzzle if puzzle_created? determine_parameters make_all_piece update_piece_information end end |
#make_single_piece(row_x, row_y) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/puzzlize/model_instance_methods.rb', line 65 def make_single_piece(row_x, row_y) # get the source image and crop it to the appropriate size. bg = @source_file.crop(*(@cutter.piece_points(row_x, row_y) + [true])) # This will be the final image output img = Magick::Image.new(bg.columns,bg.rows) img.compression = Magick::LZWCompression # draw the image that is suppose to be invisible. gc = Magick::Draw.new gc.stroke_antialias(false) gc.stroke_width(0) # first start with black box. gc.fill('#000000') gc.rectangle(0,0,bg.columns, bg.rows) # draw rectangles around the edges of the images aka "buffers" # that will later be made transperant gc.fill('#09f700') @cutter.rectangle_locations(row_x, row_y).each do |a_rectangle| gc.rectangle(*a_rectangle) unless a_rectangle.nil? end # draw the up to 4 connectors connector_types = @cutter.connector_types(row_x, row_y) @cutter.connector_locations(row_x, row_y).each_with_index do |connector, index| unless connector.nil? gc.fill('#09f700') if connector_types[index] == -1 gc.fill('#000000') if connector_types[index] == 1 points = connector points << (connector[0] + @cutter.connector_radius) points << connector[1] gc.circle(*points) end end # draw the image and composite the background image (bg) and set the transparency. # Creating a cookie cutter affect. gc.draw(img) img = img.matte_replace((bg.columns / 2).to_i,(bg.rows / 2).to_i) img = bg.composite(img, Magick::CenterGravity, Magick::OverCompositeOp) img = img.transparent('#09f700') Rails.logger.debug { "[puzzle_maker] writing image to #{@file_location.sub(/\..*$/, "-#{row_x}_#{row_y}.gif")}".inspect } if defined? Rails img.write(piece_file_name(row_x, row_y, @file_location)) end |
#piece_file_name(row_x, row_y, file_location) ⇒ Object
113 114 115 |
# File 'lib/puzzlize/model_instance_methods.rb', line 113 def piece_file_name(row_x, row_y, file_location) file_location.sub(/\..*$/, "-#{row_x}_#{row_y}.gif") end |
#puzzle_created? ⇒ Boolean
39 40 41 |
# File 'lib/puzzlize/model_instance_methods.rb', line 39 def puzzle_created? !self.vertical_pieces? end |
#puzzle_images_urls ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/puzzlize/model_instance_methods.rb', line 11 def puzzle_images_urls output = [] self.vertical_pieces.to_i.times do |y_axis| self.horizontal_pieces.to_i.times do |x_axis| output << piece_file_name(x_axis, y_axis, self.default_puzzle_image_url) end end output end |
#puzzle_pieces_names ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/puzzlize/model_instance_methods.rb', line 21 def puzzle_pieces_names output = [] self.vertical_pieces.to_i.times do |y_axis| self.horizontal_pieces.to_i.times do |x_axis| output << "puzzle_#{x_axis}-#{y_axis}" end end output end |
#update_piece_information ⇒ Object
this is isolated because supermodel treats update_attributes the same as save ActiveRecord does not do this.
55 56 57 58 59 60 61 62 63 |
# File 'lib/puzzlize/model_instance_methods.rb', line 55 def update_piece_information self.update_attributes({ :vertical_pieces => @cutter.vertical_pieces, :horizontal_pieces => @cutter.horizontal_pieces, :vertical_piece_size => @cutter.real_piece_size, :horizontal_piece_size => @cutter.real_piece_size, :image_width => @cutter.image_width, :image_height => @cutter.image_height}) end |