Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simple separation of conditional logic into objects

  • Loading branch information...
commit 2edf53945153d1a96983398ca5d8a50e5fb830f8 1 parent 5163421
@markburns authored
View
106 lib/kaboom/platform.rb
@@ -1,99 +1,31 @@
-# coding: utf-8
+require 'kaboom/platform/base'
+require 'kaboom/platform/darwin'
+require 'kaboom/platform/windows'
+require 'kaboom/platform/other'
-# Platform is a centralized point to shell out platform specific functionality
-# like clipboard access or commands to open URLs.
-#
-#
-# Clipboard is a centralized point to shell out to each individual platform's
-# clipboard, pasteboard, or whatever they decide to call it.
-#
module Boom
class Platform
class << self
- # Public: tests if currently running on darwin.
- #
- # Returns true if running on darwin (MacOS X), else false
- def darwin?
- !!(RUBY_PLATFORM =~ /darwin/)
- end
-
- # Public: tests if currently running on windows.
- #
- # Apparently Windows RUBY_PLATFORM can be 'win32' or 'mingw32'
- #
- # Returns true if running on windows (win32/mingw32), else false
- def windows?
- !!(RUBY_PLATFORM =~ /mswin|mingw/)
- end
-
- # Public: returns the command used to open a file or URL
- # for the current platform.
- #
- # Currently only supports MacOS X and Linux with `xdg-open`.
- #
- # Returns a String with the bin
- def open_command
- if darwin?
- 'open'
- elsif windows?
- 'start'
- else
- 'xdg-open'
- end
- end
-
- # Public: opens a given Item's value in the browser. This
- # method is designed to handle multiple platforms.
- #
- # Returns a String of the Item value.
- def open(item)
- if windows?
- system("#{open_command} #{item.url.gsub("\'","'\\\\''")}")
- else
- system("#{open_command} '#{item.url.gsub("\'","'\\\\''")}'")
- end
-
- item.value
- end
-
- # Public: returns the command used to copy a given Item's value to the
- # clipboard for the current platform.
- #
- # Returns a String with the bin
- def copy_command
- if darwin?
- 'pbcopy'
- elsif windows?
- 'clip'
- else
- 'xclip -selection clipboard'
- end
+ def platform
+ @platform ||=
+ begin
+ if !!(RUBY_PLATFORM =~ /darwin/)
+ Darwin.new
+ elsif !!(RUBY_PLATFORM =~ /mswin|mingw/)
+ Windows.new
+ else
+ Other.new
+ end
+ end
end
- # Public: copies a given Item's value to the clipboard. This method is
- # designed to handle multiple platforms.
- #
- # Returns the String value of the Item.
- def copy(item)
- IO.popen(copy_command,"w") {|cc| cc.write(item.value)}
- item.value
- end
+ delegate :open, :copy, :to => :platform
- # Public: opens the JSON file in an editor for you to edit. Uses the
- # $EDITOR environment variable, or %EDITOR% on Windows for editing.
- # This method is designed to handle multiple platforms.
- # If $EDITOR is nil, try to open using the open_command.
- #
- # Returns a String with a helpful message.
- def edit(json_file)
+ def edit json_file
if $EDITOR
- if windows?
- system("start %EDITOR% #{json_file}")
- else
- system("`echo $EDITOR` #{json_file} &")
- end
+ platform.edit json_file
else
- system("#{open_command} #{json_file}")
+ platform.open json_file
end
"Make your edits, and do be sure to save."
View
25 lib/kaboom/platform/base.rb
@@ -0,0 +1,25 @@
+class Boom::Platform
+ class Base
+ def edit json_file
+ system("#{yield} #{json_file}")
+ end
+
+ def copy item
+ IO.popen(yield,"w") {|cc| cc.write(item.value)}
+ item.value
+ end
+
+ def open item
+ system(yield item)
+ item.respond_to?(:value) ? item.value : item
+ end
+
+ def url_from item
+ if item.respond_to?(:url)
+ item.url
+ else
+ item
+ end
+ end
+ end
+end
View
16 lib/kaboom/platform/darwin.rb
@@ -0,0 +1,16 @@
+class Boom::Platform
+ class Darwin < Base
+ def edit json_file
+ system "`echo $EDITOR` #{json_file} &"
+ end
+
+ def copy item
+ super {'pbcopy'}
+ end
+
+ def open item
+ system("open '#{url_from(item).gsub("\'","'\\\\''")}'")
+ item.value
+ end
+ end
+end
View
11 lib/kaboom/platform/linux.rb
@@ -0,0 +1,11 @@
+class Boom::Platform
+ class Other < Base
+ def copy item
+ super{'xclip -selection clipboard'}
+ end
+
+ def open item
+ super{|i| "xdg-open '#{i.url.gsub("\'","'\\\\''")}'" }
+ end
+ end
+end
View
16 lib/kaboom/platform/windows.rb
@@ -0,0 +1,16 @@
+class Boom::Platform
+ class Windows
+ def edit json_file
+ super{"start %EDITOR$ "}
+ end
+
+ def copy item
+ super {'clip'}
+ end
+
+ def open item
+ system("start #{url_from(item).gsub("\'","'\\\\''")}")
+ item.respond_to?(:value) ? item.value : item
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.