Class: TypografRu::Manager

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Includes:
Singleton
Defined in:
lib/typograf_ru/manager.rb

Instance Method Summary collapse

Instance Method Details

#clear(klass = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/typograf_ru/manager.rb', line 28

def clear(klass = nil)
  if klass.nil?
    mapping.clear
  else
    mapping.delete(klass)
  end
end

#exec_for(object) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/typograf_ru/manager.rb', line 15

def exec_for(object)
  return unless mapping[object.class].is_a?(Hash)

  mapping[object.class].each do |attr, options|
    next if options[:if].is_a?(Proc) && !options[:if].call(self)
    text = object.send(attr)
    if !text.nil? && !text.empty? && (options[:no_check] || object.send("#{attr}_changed?"))
      res = RestClient.post('http://typograf.ru/webservice/', :text => text, :chr => 'UTF-8')
      object.send("#{attr}=", res.force_encoding("UTF-8"))
    end
  end
end

#mappingObject



36
37
38
# File 'lib/typograf_ru/manager.rb', line 36

def mapping
  @mapping ||= {}
end

#register(klass, attr, options = {}) ⇒ Object



10
11
12
13
# File 'lib/typograf_ru/manager.rb', line 10

def register(klass, attr, options = {})
  mapping[klass]||= {}
  mapping[klass][attr] = options
end