fspath-mac 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/ext/fspath/mac/extconf.rb +5 -0
- data/ext/fspath/mac/finder_label_number.c +87 -0
- data/fspath-mac.gemspec +2 -1
- data/lib/fspath/mac.rb +8 -6
- data/spec/fspath/mac_spec.rb +23 -29
- data/spec/spec_helper.rb +9 -0
- metadata +9 -7
@@ -0,0 +1,87 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include <CoreServices/CoreServices.h>
|
3
|
+
|
4
|
+
CFURLRef url_for_path(VALUE fsPath){
|
5
|
+
VALUE path = rb_iv_get(fsPath, "@path");
|
6
|
+
char *pathStr = StringValueCStr(path);
|
7
|
+
|
8
|
+
CFStringRef pathRef = CFStringCreateWithCString(NULL, pathStr, kCFStringEncodingUTF8);
|
9
|
+
if (!pathRef) {
|
10
|
+
rb_raise(rb_eRuntimeError, "Can't convert path");
|
11
|
+
}
|
12
|
+
|
13
|
+
CFURLRef urlRef = CFURLCreateWithFileSystemPath(NULL, pathRef, kCFURLPOSIXPathStyle, false);
|
14
|
+
CFRelease(pathRef);
|
15
|
+
if (!urlRef) {
|
16
|
+
rb_raise(rb_eRuntimeError, "Can't initialize url for path");
|
17
|
+
}
|
18
|
+
|
19
|
+
return urlRef;
|
20
|
+
}
|
21
|
+
|
22
|
+
void raise_cf_error(CFErrorRef errorRef){
|
23
|
+
CFStringRef errorDescriptionRef = CFErrorCopyDescription(errorRef);
|
24
|
+
|
25
|
+
const char *errorDescriptionC = CFStringGetCStringPtr(errorDescriptionRef, kCFStringEncodingUTF8);
|
26
|
+
if (errorDescriptionC) {
|
27
|
+
rb_raise(rb_eRuntimeError, errorDescriptionC);
|
28
|
+
} else {
|
29
|
+
CFIndex length = CFStringGetLength(errorDescriptionRef);
|
30
|
+
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
|
31
|
+
char *errorDescription = (char *) malloc(maxSize);
|
32
|
+
CFStringGetCString(errorDescriptionRef, errorDescription, maxSize, kCFStringEncodingUTF8);
|
33
|
+
rb_raise(rb_eRuntimeError, errorDescription);
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
static VALUE finder_label_number_get(VALUE self){
|
38
|
+
CFURLRef urlRef = url_for_path(self);
|
39
|
+
|
40
|
+
CFErrorRef errorRef;
|
41
|
+
CFNumberRef labelNumberRef;
|
42
|
+
bool ok = CFURLCopyResourcePropertyForKey(urlRef, kCFURLLabelNumberKey, &labelNumberRef, &errorRef);
|
43
|
+
CFRelease(urlRef);
|
44
|
+
|
45
|
+
if (!ok) {
|
46
|
+
raise_cf_error(errorRef);
|
47
|
+
}
|
48
|
+
|
49
|
+
SInt32 labelNumberValue;
|
50
|
+
CFNumberGetValue(labelNumberRef, kCFNumberSInt32Type, &labelNumberValue);
|
51
|
+
CFRelease(labelNumberRef);
|
52
|
+
|
53
|
+
return INT2NUM(labelNumberValue);
|
54
|
+
}
|
55
|
+
|
56
|
+
static VALUE finder_label_number_set(VALUE self, VALUE labelNumber){
|
57
|
+
if (TYPE(labelNumber) != T_FIXNUM) {
|
58
|
+
rb_raise(rb_eTypeError, "invalid type for labelNumber");
|
59
|
+
}
|
60
|
+
|
61
|
+
SInt32 labelNumberValue = NUM2INT(labelNumber);
|
62
|
+
if (labelNumberValue < 0 || labelNumberValue > 7) {
|
63
|
+
rb_raise(rb_eArgError, "label number can be in range 0..7");
|
64
|
+
}
|
65
|
+
|
66
|
+
CFURLRef urlRef = url_for_path(self);
|
67
|
+
|
68
|
+
CFNumberRef labelNumberRef = CFNumberCreate(NULL, kCFNumberSInt32Type, &labelNumberValue);
|
69
|
+
|
70
|
+
CFErrorRef errorRef;
|
71
|
+
bool ok = CFURLSetResourcePropertyForKey(urlRef, kCFURLLabelNumberKey, labelNumberRef, &errorRef);
|
72
|
+
CFRelease(labelNumberRef);
|
73
|
+
CFRelease(urlRef);
|
74
|
+
|
75
|
+
if (!ok) {
|
76
|
+
raise_cf_error(errorRef);
|
77
|
+
}
|
78
|
+
|
79
|
+
return Qnil;
|
80
|
+
}
|
81
|
+
|
82
|
+
void Init_finder_label_number() {
|
83
|
+
VALUE cFSPath = rb_const_get(rb_cObject, rb_intern("FSPath"));
|
84
|
+
VALUE mMac = rb_const_get(cFSPath, rb_intern("Mac"));
|
85
|
+
rb_define_private_method(mMac, "finder_label_number", finder_label_number_get, 0);
|
86
|
+
rb_define_private_method(mMac, "finder_label_number=", finder_label_number_set, 1);
|
87
|
+
}
|
data/fspath-mac.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'fspath-mac'
|
5
|
-
s.version = '
|
5
|
+
s.version = '2.0.0'
|
6
6
|
s.summary = %q{FSPath methods for mac (move_to_trash, color labeling, spotlight comments, …)}
|
7
7
|
s.homepage = "http://github.com/toy/#{s.name}"
|
8
8
|
s.authors = ['Ivan Kuchin']
|
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.files = `git ls-files`.split("\n")
|
14
14
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
15
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.extensions = `git ls-files -- ext/**/extconf.rb`.split("\n")
|
16
17
|
s.require_paths = %w[lib]
|
17
18
|
|
18
19
|
s.add_dependency 'fspath', '~> 2.0.0'
|
data/lib/fspath/mac.rb
CHANGED
@@ -8,18 +8,18 @@ class FSPath
|
|
8
8
|
mac_finder_alias.delete
|
9
9
|
end
|
10
10
|
|
11
|
-
FINDER_LABEL_COLORS = [
|
12
|
-
FINDER_LABEL_COLOR_ALIASES = {:
|
13
|
-
# Get finder label (one of
|
11
|
+
FINDER_LABEL_COLORS = [nil, :grey, :green, :purple, :blue, :yellow, :red, :orange].freeze
|
12
|
+
FINDER_LABEL_COLOR_ALIASES = {:gray => :grey}.freeze
|
13
|
+
# Get finder label (one of nil, :orange, :red, :yellow, :blue, :purple, :green and :grey)
|
14
14
|
def finder_label
|
15
|
-
FINDER_LABEL_COLORS[
|
15
|
+
FINDER_LABEL_COLORS[finder_label_number]
|
16
16
|
end
|
17
17
|
# Set finder label (:grey is same as :gray, nil or false as :none)
|
18
18
|
def finder_label=(color)
|
19
|
-
color = FINDER_LABEL_COLOR_ALIASES[color] || color
|
19
|
+
color = FINDER_LABEL_COLOR_ALIASES[color] || color
|
20
20
|
index = FINDER_LABEL_COLORS.index(color)
|
21
21
|
raise "Unknown label #{color.inspect}" unless index
|
22
|
-
|
22
|
+
self.finder_label_number = index
|
23
23
|
end
|
24
24
|
|
25
25
|
# Get spotlight comment
|
@@ -55,3 +55,5 @@ class FSPath
|
|
55
55
|
|
56
56
|
include Mac
|
57
57
|
end
|
58
|
+
|
59
|
+
require 'fspath/mac/finder_label_number'
|
data/spec/fspath/mac_spec.rb
CHANGED
@@ -17,42 +17,29 @@ describe FSPath::Mac do
|
|
17
17
|
|
18
18
|
describe "finder labels" do
|
19
19
|
describe "getting" do
|
20
|
-
it "should call
|
20
|
+
it "should call finder_label_number" do
|
21
21
|
@path = FSPath('to_label')
|
22
|
-
@finder_alias = mock(:finder_alias)
|
23
|
-
@label_index = mock(:label_index)
|
24
22
|
|
25
|
-
@path.should_receive(:
|
26
|
-
@finder_alias.should_receive(:label_index).and_return(@label_index)
|
27
|
-
@label_index.should_receive(:get).and_return(0)
|
23
|
+
@path.should_receive(:finder_label_number).and_return(0)
|
28
24
|
|
29
25
|
@path.finder_label
|
30
26
|
end
|
31
27
|
|
32
28
|
it "should return apporitate label" do
|
33
29
|
@path = FSPath('to_label')
|
34
|
-
@finder_alias = mock(:finder_alias)
|
35
|
-
@label_index = mock(:label_index)
|
36
|
-
|
37
|
-
@path.stub!(:mac_finder_alias).and_return(@finder_alias)
|
38
|
-
@finder_alias.stub!(:label_index).and_return(@label_index)
|
39
30
|
|
40
31
|
FSPath::FINDER_LABEL_COLORS.each_with_index do |label, index|
|
41
|
-
@
|
32
|
+
@path.stub!(:finder_label_number).and_return(index)
|
42
33
|
@path.finder_label.should == label
|
43
34
|
end
|
44
35
|
end
|
45
36
|
end
|
46
37
|
|
47
38
|
describe "setting" do
|
48
|
-
it "should call
|
39
|
+
it "should call finder_label_number=" do
|
49
40
|
@path = FSPath('to_label')
|
50
|
-
@finder_alias = mock(:finder_alias)
|
51
|
-
@label_index = mock(:label_index)
|
52
41
|
|
53
|
-
@path.should_receive(:
|
54
|
-
@finder_alias.should_receive(:label_index).and_return(@label_index)
|
55
|
-
@label_index.should_receive(:set)
|
42
|
+
@path.should_receive(:finder_label_number=)
|
56
43
|
|
57
44
|
@path.finder_label = nil
|
58
45
|
end
|
@@ -60,16 +47,11 @@ describe FSPath::Mac do
|
|
60
47
|
describe "index" do
|
61
48
|
before do
|
62
49
|
@path = FSPath('to_label')
|
63
|
-
@finder_alias = mock(:finder_alias)
|
64
|
-
@label_index = mock(:label_index)
|
65
|
-
|
66
|
-
@path.stub!(:mac_finder_alias).and_return(@finder_alias)
|
67
|
-
@finder_alias.stub!(:label_index).and_return(@label_index)
|
68
50
|
end
|
69
51
|
|
70
52
|
it "should call label_index.set with apporitate index" do
|
71
53
|
FSPath::FINDER_LABEL_COLORS.each_with_index do |label, index|
|
72
|
-
@
|
54
|
+
@path.should_receive(:finder_label_number=).with(index).ordered
|
73
55
|
@path.finder_label = label
|
74
56
|
end
|
75
57
|
end
|
@@ -77,16 +59,14 @@ describe FSPath::Mac do
|
|
77
59
|
it "should accept aliases" do
|
78
60
|
FSPath::FINDER_LABEL_COLOR_ALIASES.each do |label_alias, label|
|
79
61
|
index = FSPath::FINDER_LABEL_COLORS.index(label)
|
80
|
-
@
|
62
|
+
@path.should_receive(:finder_label_number=).with(index).ordered
|
81
63
|
@path.finder_label = label_alias
|
82
64
|
end
|
83
65
|
end
|
84
66
|
|
85
67
|
it "should set to none when called with nil or false" do
|
86
|
-
|
87
|
-
|
88
|
-
@path.finder_label = label
|
89
|
-
end
|
68
|
+
@path.should_receive(:finder_label_number=).with(0).ordered
|
69
|
+
@path.finder_label = nil
|
90
70
|
end
|
91
71
|
|
92
72
|
it "should raise when called with something else" do
|
@@ -98,6 +78,20 @@ describe FSPath::Mac do
|
|
98
78
|
end
|
99
79
|
end
|
100
80
|
end
|
81
|
+
|
82
|
+
describe "number" do
|
83
|
+
it "should set label" do
|
84
|
+
@path = FSPath.temp_file_path
|
85
|
+
|
86
|
+
mac_finder_alias_colors = [nil, :orange, :red, :yellow, :blue, :purple, :green, :grey]
|
87
|
+
8.times do |label_number|
|
88
|
+
@path.send(:finder_label_number=, label_number)
|
89
|
+
@path.send(:finder_label_number).should == label_number
|
90
|
+
color = mac_finder_alias_colors[@path.mac_finder_alias.label_index.get]
|
91
|
+
FSPath::Mac::FINDER_LABEL_COLORS.index(color).should == label_number
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
101
95
|
end
|
102
96
|
|
103
97
|
describe "spotlight comments" do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,11 @@
|
|
1
|
+
Dir.chdir File.join(File.dirname(__FILE__), '..') do
|
2
|
+
Dir['ext/**/extconf.rb'].each do |extconf|
|
3
|
+
Dir.chdir(File.dirname(extconf)) do
|
4
|
+
system('ruby extconf.rb') && system('make') or abort "failed compiling #{extconf}"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
1
9
|
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
10
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'ext')
|
2
11
|
require 'rspec'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fspath-mac
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
9
|
- 0
|
10
|
-
version:
|
10
|
+
version: 2.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ivan Kuchin
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-11-03 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: fspath
|
@@ -65,14 +65,16 @@ description:
|
|
65
65
|
email:
|
66
66
|
executables: []
|
67
67
|
|
68
|
-
extensions:
|
69
|
-
|
68
|
+
extensions:
|
69
|
+
- ext/fspath/mac/extconf.rb
|
70
70
|
extra_rdoc_files: []
|
71
71
|
|
72
72
|
files:
|
73
73
|
- .gitignore
|
74
74
|
- LICENSE.txt
|
75
75
|
- README.markdown
|
76
|
+
- ext/fspath/mac/extconf.rb
|
77
|
+
- ext/fspath/mac/finder_label_number.c
|
76
78
|
- fspath-mac.gemspec
|
77
79
|
- lib/fspath/mac.rb
|
78
80
|
- spec/fspath/mac_spec.rb
|
@@ -106,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
108
|
requirements: []
|
107
109
|
|
108
110
|
rubyforge_project: fspath-mac
|
109
|
-
rubygems_version: 1.8.
|
111
|
+
rubygems_version: 1.8.24
|
110
112
|
signing_key:
|
111
113
|
specification_version: 3
|
112
114
|
summary: "FSPath methods for mac (move_to_trash, color labeling, spotlight comments, \xE2\x80\xA6)"
|