Sha256: 703a3ff440de30cf78eac8254292d683106a9158582d5f8a892ac610e1131c4f

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'

describe "Attribute Convertors" do
  with_mongo_mapper
  
  after(:all){remove_constants %w(AsStringSample)}    
    
  before do
    @convertors = MongoMapper::Plugins::AttributeConvertors::ClassMethods::ATTRIBUTE_CONVERTORS
    @convertors[:test_convertor] = {
      from_string: -> s {"from_string: #{s}"},
      to_string:   -> v {"to_string: #{v}"}
    }
  end
  
  it ":line convertor" do      
    v = ['a', 'b']
    str_v = 'a, b'
    @convertors[:line][:from_string].call(str_v).should == v
    @convertors[:line][:to_string].call(v).should == str_v
  end
  
  it ":yaml convertor" do
    v = {'a' => 'b'}
    str_v = v.to_yaml.strip
    
    @convertors[:yaml][:from_string].call(str_v).should == v
    @convertors[:yaml][:to_string].call(v).should == str_v
  end
  
  it ":json convertor" do
    v = {'a' => 'b'}
    str_v = v.to_json.strip
    @convertors[:json][:from_string].call(str_v).should == v
    @convertors[:json][:to_string].call(v).should == str_v
  end
  
  it ":key extension" do
    class ::AsStringSample
      include MongoMapper::Document
      include MongoMapper::Plugins::Micelaneous
      
      key :key, String, as_string: :test_convertor
      key :protected_key, String, as_string: :test_convertor, protected: true
    end
  
    o = AsStringSample.new
    
    # get
    o.key_as_string.should == 'to_string: '
    o.key = 'value'
    o.clear_cache
    o.key_as_string.should == 'to_string: value'
    
    # set
    o.key_as_string = ''
    o.key.should == 'from_string: '
    o.key_as_string = 'value'
    o.key.should == 'from_string: value'
    
    # mass assignment
    o.key = ''
    o.update_attributes key_as_string: 'value'
    o.key.should == 'from_string: value'
    
    # protection
    o.protected_key = ''
    o.update_attributes protected_key_as_string: 'value'
    o.protected_key.should == ''
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo_mapper_ext-0.2.3 spec/attribute_convertors_spec.rb