Monday, February 23, 2009
Monday, December 8, 2008
require 'rubygems'
require "inline"
class SMS
class << self
inline do |builder|
builder.add_compile_flags '-x objective-c', '-framework IOKit'
builder.include "<IOKit/IOKitLib.h>"
builder.c %q{
VALUE values(){
struct data {
unsigned short x;
unsigned short y;
unsigned short z;
char pad[34];
};
kern_return_t result;
mach_port_t masterPort;
IOMasterPort(MACH_PORT_NULL, &masterPort);
CFMutableDictionaryRef matchingDictionary = IOServiceMatching("SMCMotionSensor");
io_iterator_t iterator;
result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
if(result != KERN_SUCCESS) {
return rb_str_new2("Error");
}
io_object_t device = IOIteratorNext(iterator);
IOObjectRelease(iterator);
if(device == 0){
return rb_str_new2("Error");
}
io_connect_t dataPort;
result = IOServiceOpen(device, mach_task_self(), 0, &dataPort);
IOObjectRelease(device);
if(result != KERN_SUCCESS) {
return rb_str_new2("Error");
}
IOItemCount structureInputSize;
IOByteCount structureOutputSize;
struct data inputStructure;
struct data outputStructure;
structureInputSize = sizeof(struct data);
structureOutputSize = sizeof(struct data);
memset(&inputStructure, 1, sizeof(inputStructure));
memset(&outputStructure, 0, sizeof(outputStructure));
result = IOConnectMethodStructureIStructureO(
dataPort,
5,
structureInputSize,
&structureOutputSize,
&inputStructure,
&outputStructure
);
if(result != KERN_SUCCESS) {
return rb_str_new2("Error");
}
IOServiceClose(dataPort);
VALUE coords = rb_ary_new2(3);
rb_ary_store(coords, 0, INT2FIX(outputStructure.x));
rb_ary_store(coords, 1, INT2FIX(outputStructure.y));
rb_ary_store(coords, 2, INT2FIX(outputStructure.z));
return coords;
}
}
end
end
end
loop do
x,y,z = SMS.values
puts "#{x} #{y} #{z}"
end
Thursday, June 5, 2008
Yes, i know!
Helpers are modules that provide methods which are automatically usable in your view.But somtimes you want to use it in controllers ;)
This is my solution. Helpers are available in module named 'Helpers', so they don't brake anything. All rails helpers and named routes are included by default.
This is simple usage:
And method to add to ApplicationController class.
class ExampleController < ApplicationController
include_helper ApplicationHelper, PicturesHelper
def show
...
flash[:notice] = Helpers::flash_with_picture('Hello from helper')
...
end
end
class ApplicationController < ActionController::Base
def self.include_helper(*args)
require 'action_controller/integration'
class_eval do
helpers = const_defined?('Helpers') ? const_get('Helpers') : Module.new do
@@controller = ActionController::Integration::Session.new
def self.method_missing(method, *args, &block)
if @@controller && method.to_s =~ /_path$|_url$/
@@controller.send(method, *args, &block)
else
raise NoMethodError, "undefined method `#{method}' for #{self}"
end
end
end
ActionView::Helpers.constants.each do |constant|
helpers.extend ActionView::Helpers.const_get(constant) if ActionView::Helpers.const_get(constant).instance_of?(Module)
end
if args.instance_of?(Array)
args.each do |helper|
helpers.extend helper if helper.instance_of?(Module)
end
elsif args.instance_of?(Module)
helpers.extend args
end
const_set( 'Helpers', helpers ) unless const_defined?('Helpers')
end
end
end
Tuesday, April 29, 2008
GEdit is nice developer editor for GNOME. Textmate like snippets, file browser and embedded terminal. Last one is a nice feature, but default plugin gives you only one terminal. But wait, plugins are written in python. This is my third attempt to modify python code... It was not so bad, after few print(), dir(), and id() I can add and remove terminal windows within gedit :)
How to install ? Copy all files to ~/.gnome2/gedit/plugins, disable terminal plugin, restart gedit and enable mterminal plugin.
How to use it ? To add new terminal right-click on terminal window and choose 'New Terminal'. If you want to remove it, right-click and choose 'Remove Terminal'.
Plugin/Sources are located here.
I hope you like it.