grit_ext 0.0.1 → 0.1.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 CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .Ds_Store
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # GritExt
2
2
 
3
- TODO: Write a gem description
3
+ grit_ext is a grit extension gem.
4
+
5
+ 1. auto transcoding `blob.data` to utf-8 encoding
6
+
7
+ 2. give blob a language method, show the blob's programming language
8
+
9
+ 3. give blob a encoding method, show the blob's text encoding.
4
10
 
5
11
  ## Installation
6
12
 
@@ -18,7 +24,29 @@ Or install it yourself as:
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
27
+ ```ruby
28
+ [1] pry(main)> require'grit'
29
+ => true
30
+ [2] pry(main)> require'grit_ext'
31
+ => true
32
+ [3] pry(main)> repo = Grit::Repo.new("/Users/saito/Develop/grit_ext")
33
+ => #<Grit::Repo "/Users/saito/Develop/grit_ext/.git">
34
+ [4] pry(main)> blob = repo.tree / 'Gemfile'
35
+ => #<Grit::Blob "015bb0f8c189aca154b2a54e511415e6723d104d">
36
+ [5] pry(main)> blob.language
37
+ => "Ruby"
38
+ [6] pry(main)> blob.encoding
39
+ => "ISO-8859-1"
40
+ [7] pry(main)> blob.data
41
+ => "source 'https://rubygems.org'\n\n# Specify your gem's dependencies in grit_ext.gemspec\ngemspec\n"
42
+ [7] pry(main)> blob.text?
43
+ => true
44
+ [7] pry(main)> blob.image?
45
+ => false
46
+ [7] pry(main)> blob.binary?
47
+ => false
48
+ ```
49
+
22
50
 
23
51
  ## Contributing
24
52
 
@@ -0,0 +1,28 @@
1
+ module Grit
2
+ class Blob
3
+ def text?
4
+ !!(mime_type =~ /application|text/ && !binary?)
5
+ end
6
+
7
+ def image?
8
+ !!(mime_type =~ /image/)
9
+ end
10
+
11
+ def binary?
12
+ data[0..1024].include?("\000")
13
+ end
14
+
15
+ def encoding
16
+ if detection = CharlockHolmes::EncodingDetector.detect(data)
17
+ detection[:encoding]
18
+ end
19
+ end
20
+
21
+ def language
22
+ if lang = LanguageSniffer.detect(basename, :content => data).language
23
+ lang.name
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,13 @@
1
+ module Grit
2
+ module GitRuby
3
+ module Internal
4
+ class RawObject
5
+ include GritExt
6
+
7
+ def content
8
+ transcode(@content)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module GritExt
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/grit_ext.rb CHANGED
@@ -1,15 +1,13 @@
1
1
  require "rubygems"
2
2
  require "charlock_holmes"
3
3
  require "language_sniffer"
4
+ require "grit_ext/blob"
4
5
  require "grit_ext/version"
6
+ require "grit_ext/raw_object"
5
7
 
6
- Grit::GitRuby::Internal::RawObject.class_eval do
7
- def content
8
- transcoding(@content)
9
- end
10
-
8
+ module GritExt
11
9
  private
12
- def transcoding(content)
10
+ def transcode(content)
13
11
  detection = CharlockHolmes::EncodingDetector.detect(content)
14
12
  if hash = detection
15
13
  content = CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding]
@@ -18,17 +16,3 @@ Grit::GitRuby::Internal::RawObject.class_eval do
18
16
  end
19
17
  end
20
18
 
21
- Grit::Blob.class_eval do
22
-
23
- def encoding
24
- if detection = CharlockHolmes::EncodingDetector.detect(data)
25
- detection[:encoding]
26
- end
27
- end
28
-
29
- def language
30
- if lang = LanguageSniffer.detect(basename, :content => data).language
31
- lang.name
32
- end
33
- end
34
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grit_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-04 00:00:00.000000000Z
12
+ date: 2012-01-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: charlock_holmes
16
- requirement: &70253813141560 !ruby/object:Gem::Requirement
16
+ requirement: &70258896872560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.6.8
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70253813141560
24
+ version_requirements: *70258896872560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: language_sniffer
27
- requirement: &70253813141060 !ruby/object:Gem::Requirement
27
+ requirement: &70258891864420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.0.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70253813141060
35
+ version_requirements: *70258891864420
36
36
  description: the grit's extension
37
37
  email:
38
38
  - saitowu@gmail.com
@@ -47,6 +47,8 @@ files:
47
47
  - Rakefile
48
48
  - grit_ext.gemspec
49
49
  - lib/grit_ext.rb
50
+ - lib/grit_ext/blob.rb
51
+ - lib/grit_ext/raw_object.rb
50
52
  - lib/grit_ext/version.rb
51
53
  homepage: http://saito.im
52
54
  licenses: []