Module: Mongo::Model::TextProcessor::ClassMethods

Defined in:
lib/kit/mongodb_model/text_processor.rb

Instance Method Summary collapse

Instance Method Details

#available_as_markup(attr_name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kit/mongodb_model/text_processor.rb', line 3

def available_as_markup attr_name
  original_attr_name = :"original_#{attr_name}"

  raise "attribute #{attr_name} not defined!" unless method_defined? attr_name
  attr_reader original_attr_name              unless method_defined? original_attr_name

  iv_name, original_iv_name = :"@#{attr_name}", :"@#{original_attr_name}"

  define_method :"#{attr_name}=" do |value|
    instance_variable_set iv_name, value
    instance_variable_set original_iv_name, value
  end

  define_method "#{original_attr_name}=" do |value|
    instance_variable_set iv_name, TextUtils.markup(value)
    instance_variable_set original_iv_name, value
  end

  define_method "#{attr_name}_as_text" do
    value = instance_variable_get iv_name
    return "" if value.blank?
    Nokogiri::XML(value).content
  end

  after_validate do |model|
    model.errors.add original_attr_name, model.errors[attr_name] if model.errors.include?(attr_name)
  end
end