typograf_ru 0.0.1
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 +5 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Rakefile +5 -0
- data/lib/typograf_ru.rb +34 -0
- data/lib/typograf_ru/manager.rb +40 -0
- data/lib/typograf_ru/version.rb +3 -0
- data/spec/lib/manager_spec.rb +115 -0
- data/spec/spec_helper.rb +8 -0
- data/typograf_ru.gemspec +25 -0
- metadata +118 -0
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--colour -fs
|
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/typograf_ru.rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require "typograf_ru/version"
|
|
2
|
+
require "typograf_ru/manager"
|
|
3
|
+
require "rest_client"
|
|
4
|
+
|
|
5
|
+
module TypografRu
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend(ClassMethods)
|
|
8
|
+
base.send(:include, InstanceMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
|
|
13
|
+
def typografy(attr, options={})
|
|
14
|
+
include InstanceMethods
|
|
15
|
+
|
|
16
|
+
options ||= {}
|
|
17
|
+
Manager.register(self, attr, options)
|
|
18
|
+
|
|
19
|
+
before_save :typografy_before_save
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def disable_typografy!
|
|
23
|
+
Manager.clear(self)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
module InstanceMethods
|
|
28
|
+
def typografy_before_save
|
|
29
|
+
Manager.exec_for(self)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
ActiveRecord::Base.send(:include, TypografRu) if defined?(ActiveRecord)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require "singleton"
|
|
3
|
+
module TypografRu
|
|
4
|
+
class Manager
|
|
5
|
+
include Singleton
|
|
6
|
+
extend SingleForwardable
|
|
7
|
+
|
|
8
|
+
def_delegators :instance, :register, :exec_for, :clear
|
|
9
|
+
|
|
10
|
+
def register(klass, attr, options = {})
|
|
11
|
+
mapping[klass]||= {}
|
|
12
|
+
mapping[klass][attr] = options
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def exec_for(object)
|
|
16
|
+
return unless mapping[object.class].is_a?(Hash)
|
|
17
|
+
|
|
18
|
+
mapping[object.class].each do |attr, options|
|
|
19
|
+
next if options[:if].is_a?(Proc) && !options[:if].call(self)
|
|
20
|
+
text = object.send(attr)
|
|
21
|
+
if !text.nil? && !text.empty? && (options[:no_check] || object.send("#{attr}_changed?"))
|
|
22
|
+
res = RestClient.post('http://typograf.ru/webservice/', :text => text, :chr => 'UTF-8')
|
|
23
|
+
object.send("#{attr}=", res)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def clear(klass = nil)
|
|
29
|
+
if klass.nil?
|
|
30
|
+
mapping.clear
|
|
31
|
+
else
|
|
32
|
+
mapping.delete(klass)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def mapping
|
|
37
|
+
@mapping ||= {}
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe TypografRu::Manager do
|
|
4
|
+
|
|
5
|
+
subject{ described_class.instance }
|
|
6
|
+
|
|
7
|
+
before(:each){ subject.clear }
|
|
8
|
+
|
|
9
|
+
describe "#register" do
|
|
10
|
+
it "should add new record to mapping" do
|
|
11
|
+
subject.mapping.should be_empty
|
|
12
|
+
subject.register(String, :to_s, :some_option => 10)
|
|
13
|
+
subject.mapping.should == { String => { :to_s => { :some_option => 10 } } }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "#clear" do
|
|
18
|
+
it "should clear mapping for given class" do
|
|
19
|
+
subject.register(String, :to_s, :some => :conf)
|
|
20
|
+
subject.register(Fixnum, :to_s)
|
|
21
|
+
subject.mapping.should == {
|
|
22
|
+
String => { :to_s => { :some => :conf } },
|
|
23
|
+
Fixnum => { :to_s => {} }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
subject.clear(Fixnum)
|
|
27
|
+
subject.mapping.should == { String => { :to_s => { :some => :conf } } }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "should clear all if nil given as first arg" do
|
|
31
|
+
subject.register(String, :to_s, :some => :conf)
|
|
32
|
+
subject.register(Fixnum, :to_s)
|
|
33
|
+
subject.mapping.should == {
|
|
34
|
+
String => { :to_s => { :some => :conf } },
|
|
35
|
+
Fixnum => { :to_s => {} }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
subject.clear
|
|
39
|
+
subject.mapping.should == { }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
describe "#exec_for" do
|
|
44
|
+
it "should do nothing if object's class not registered" do
|
|
45
|
+
RestClient.should_not_receive(:post)
|
|
46
|
+
subject.exec_for(35)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "should do nothing if attr's value nil or empty" do
|
|
50
|
+
object = mock('SomeClass')
|
|
51
|
+
object.stub(:attr_1)
|
|
52
|
+
object.stub(:attr_2){ '' }
|
|
53
|
+
object.stub(:attr_1_changed?){ true }
|
|
54
|
+
object.stub(:attr_2_changed?){ true }
|
|
55
|
+
subject.register(object.class, :attr_1)
|
|
56
|
+
subject.register(object.class, :attr_2)
|
|
57
|
+
RestClient.should_not_receive(:post)
|
|
58
|
+
subject.exec_for(object)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
context "no options were given for attr" do
|
|
62
|
+
it "should raise error when object doesn't respond to :attr_changed?" do
|
|
63
|
+
object = mock('SomeClass')
|
|
64
|
+
object.stub(:title){ 'some text' }
|
|
65
|
+
object.should_not respond_to(:title_changed?)
|
|
66
|
+
subject.register(object.class, :title)
|
|
67
|
+
expect{ subject.exec_for(object) }.should raise_error
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "should change attribute value from http://typograf.ru" do
|
|
71
|
+
object = mock('SomeClass')
|
|
72
|
+
object.stub(:title){ 'some text' }
|
|
73
|
+
object.stub(:title_changed?){ true }
|
|
74
|
+
object.stub(:title=)
|
|
75
|
+
|
|
76
|
+
subject.register(object.class, :title)
|
|
77
|
+
RestClient.should_receive(:post).with('http://typograf.ru/webservice/', :text => object.title, :chr => 'UTF-8')
|
|
78
|
+
|
|
79
|
+
subject.exec_for(object)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "should not call attr_changed? when :no_check => true option is given" do
|
|
84
|
+
object = mock('SomeClass')
|
|
85
|
+
object.stub(:title){ 'some text' }
|
|
86
|
+
object.stub(:title=)
|
|
87
|
+
|
|
88
|
+
object.should_not_receive(:title_changed?)
|
|
89
|
+
subject.register(object.class, :title, :no_check => true)
|
|
90
|
+
RestClient.should_receive(:post).with('http://typograf.ru/webservice/', :text => object.title, :chr => 'UTF-8')
|
|
91
|
+
|
|
92
|
+
subject.exec_for(object)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it "should do nothing if option :if is proc and it returns false" do
|
|
96
|
+
object = mock('SomeClass')
|
|
97
|
+
subject.register(object.class, :title, :if => proc{ |r| false } )
|
|
98
|
+
RestClient.should_not_receive(:post)
|
|
99
|
+
subject.exec_for(object)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it "should call service if option :if is proc and it returns true" do
|
|
103
|
+
object = mock('SomeClass')
|
|
104
|
+
object.stub(:title){ 'some text' }
|
|
105
|
+
object.stub(:title_changed?){ true }
|
|
106
|
+
object.stub(:title=)
|
|
107
|
+
|
|
108
|
+
subject.register(object.class, :title, :if => proc{ |r| true } )
|
|
109
|
+
RestClient.should_receive(:post).with('http://typograf.ru/webservice/', :text => object.title, :chr => 'UTF-8')
|
|
110
|
+
|
|
111
|
+
subject.exec_for(object)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/typograf_ru.gemspec
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
require "typograf_ru/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = "typograf_ru"
|
|
7
|
+
s.version = TypografRu::VERSION
|
|
8
|
+
s.authors = ["Maxim Dorofienko"]
|
|
9
|
+
s.email = ["dorofienko@gmail.com"]
|
|
10
|
+
s.homepage = ""
|
|
11
|
+
s.summary = "Gem adds ability to format russian text by http://typograf.ru for AcitveRecord attributes."
|
|
12
|
+
s.description = "Gem adds ability to format russian text by http://typograf.ru for AcitveRecord attributes."
|
|
13
|
+
|
|
14
|
+
s.rubyforge_project = "typograf_ru"
|
|
15
|
+
|
|
16
|
+
s.files = `git ls-files`.split("\n")
|
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
19
|
+
s.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
# specify any dependencies here; for example:
|
|
22
|
+
s.add_development_dependency("rake")
|
|
23
|
+
s.add_development_dependency("rspec")
|
|
24
|
+
s.add_runtime_dependency("rest-client")
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: typograf_ru
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 29
|
|
5
|
+
prerelease:
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 0
|
|
9
|
+
- 1
|
|
10
|
+
version: 0.0.1
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Maxim Dorofienko
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2012-03-27 00:00:00 Z
|
|
19
|
+
dependencies:
|
|
20
|
+
- !ruby/object:Gem::Dependency
|
|
21
|
+
name: rake
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
24
|
+
none: false
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
hash: 3
|
|
29
|
+
segments:
|
|
30
|
+
- 0
|
|
31
|
+
version: "0"
|
|
32
|
+
type: :development
|
|
33
|
+
version_requirements: *id001
|
|
34
|
+
- !ruby/object:Gem::Dependency
|
|
35
|
+
name: rspec
|
|
36
|
+
prerelease: false
|
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
38
|
+
none: false
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
hash: 3
|
|
43
|
+
segments:
|
|
44
|
+
- 0
|
|
45
|
+
version: "0"
|
|
46
|
+
type: :development
|
|
47
|
+
version_requirements: *id002
|
|
48
|
+
- !ruby/object:Gem::Dependency
|
|
49
|
+
name: rest-client
|
|
50
|
+
prerelease: false
|
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
52
|
+
none: false
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
hash: 3
|
|
57
|
+
segments:
|
|
58
|
+
- 0
|
|
59
|
+
version: "0"
|
|
60
|
+
type: :runtime
|
|
61
|
+
version_requirements: *id003
|
|
62
|
+
description: Gem adds ability to format russian text by http://typograf.ru for AcitveRecord attributes.
|
|
63
|
+
email:
|
|
64
|
+
- dorofienko@gmail.com
|
|
65
|
+
executables: []
|
|
66
|
+
|
|
67
|
+
extensions: []
|
|
68
|
+
|
|
69
|
+
extra_rdoc_files: []
|
|
70
|
+
|
|
71
|
+
files:
|
|
72
|
+
- .gitignore
|
|
73
|
+
- .rspec
|
|
74
|
+
- Gemfile
|
|
75
|
+
- Rakefile
|
|
76
|
+
- lib/typograf_ru.rb
|
|
77
|
+
- lib/typograf_ru/manager.rb
|
|
78
|
+
- lib/typograf_ru/version.rb
|
|
79
|
+
- spec/lib/manager_spec.rb
|
|
80
|
+
- spec/spec_helper.rb
|
|
81
|
+
- typograf_ru.gemspec
|
|
82
|
+
homepage: ""
|
|
83
|
+
licenses: []
|
|
84
|
+
|
|
85
|
+
post_install_message:
|
|
86
|
+
rdoc_options: []
|
|
87
|
+
|
|
88
|
+
require_paths:
|
|
89
|
+
- lib
|
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
|
+
none: false
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
hash: 3
|
|
96
|
+
segments:
|
|
97
|
+
- 0
|
|
98
|
+
version: "0"
|
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
|
+
none: false
|
|
101
|
+
requirements:
|
|
102
|
+
- - ">="
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
hash: 3
|
|
105
|
+
segments:
|
|
106
|
+
- 0
|
|
107
|
+
version: "0"
|
|
108
|
+
requirements: []
|
|
109
|
+
|
|
110
|
+
rubyforge_project: typograf_ru
|
|
111
|
+
rubygems_version: 1.8.10
|
|
112
|
+
signing_key:
|
|
113
|
+
specification_version: 3
|
|
114
|
+
summary: Gem adds ability to format russian text by http://typograf.ru for AcitveRecord attributes.
|
|
115
|
+
test_files:
|
|
116
|
+
- spec/lib/manager_spec.rb
|
|
117
|
+
- spec/spec_helper.rb
|
|
118
|
+
has_rdoc:
|