json_serialize 1.0.0 → 2.0.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/Gemfile +7 -7
- data/Gemfile.lock +27 -31
- data/README.textile +3 -3
- data/Rakefile +12 -10
- data/VERSION +1 -1
- data/json_serialize.gemspec +38 -24
- data/lib/json_serialize.rb +54 -12
- data/spec/json_serialize_spec.rb +29 -25
- data/spec/spec_helper.rb +14 -1
- metadata +77 -21
- data/.gitignore +0 -25
    
        data/Gemfile
    CHANGED
    
    | @@ -4,10 +4,10 @@ source 'http://rubygems.org' | |
| 4 4 | 
             
            gem 'activerecord', require: 'active_record'
         | 
| 5 5 | 
             
            gem 'activesupport', require: 'active_support/core_ext/object/try'
         | 
| 6 6 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
            gem 'jeweler'
         | 
| 9 | 
            -
            gem 'yard'
         | 
| 10 | 
            -
            gem 'RedCloth', require: 'redcloth'
         | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 7 | 
            +
            group :development do
         | 
| 8 | 
            +
              gem 'jeweler'
         | 
| 9 | 
            +
              gem 'yard'
         | 
| 10 | 
            +
              gem 'RedCloth', require: 'redcloth'
         | 
| 11 | 
            +
              gem 'rspec'
         | 
| 12 | 
            +
              gem 'sqlite3'
         | 
| 13 | 
            +
            end
         | 
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,43 +1,38 @@ | |
| 1 1 | 
             
            GEM
         | 
| 2 2 | 
             
              remote: http://rubygems.org/
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                RedCloth (4.2. | 
| 5 | 
            -
                activemodel (3.0. | 
| 6 | 
            -
                  activesupport (= 3.0. | 
| 4 | 
            +
                RedCloth (4.2.7)
         | 
| 5 | 
            +
                activemodel (3.0.4)
         | 
| 6 | 
            +
                  activesupport (= 3.0.4)
         | 
| 7 7 | 
             
                  builder (~> 2.1.2)
         | 
| 8 | 
            -
                  i18n (~> 0.4 | 
| 9 | 
            -
                activerecord (3.0. | 
| 10 | 
            -
                  activemodel (= 3.0. | 
| 11 | 
            -
                  activesupport (= 3.0. | 
| 12 | 
            -
                  arel (~>  | 
| 8 | 
            +
                  i18n (~> 0.4)
         | 
| 9 | 
            +
                activerecord (3.0.4)
         | 
| 10 | 
            +
                  activemodel (= 3.0.4)
         | 
| 11 | 
            +
                  activesupport (= 3.0.4)
         | 
| 12 | 
            +
                  arel (~> 2.0.2)
         | 
| 13 13 | 
             
                  tzinfo (~> 0.3.23)
         | 
| 14 | 
            -
                activesupport (3.0. | 
| 15 | 
            -
                arel ( | 
| 16 | 
            -
                  activesupport (~> 3.0.0)
         | 
| 14 | 
            +
                activesupport (3.0.4)
         | 
| 15 | 
            +
                arel (2.0.8)
         | 
| 17 16 | 
             
                builder (2.1.2)
         | 
| 18 17 | 
             
                diff-lcs (1.1.2)
         | 
| 19 | 
            -
                gemcutter (0.6.1)
         | 
| 20 18 | 
             
                git (1.2.5)
         | 
| 21 | 
            -
                i18n (0. | 
| 22 | 
            -
                jeweler (1. | 
| 23 | 
            -
                   | 
| 19 | 
            +
                i18n (0.5.0)
         | 
| 20 | 
            +
                jeweler (1.5.2)
         | 
| 21 | 
            +
                  bundler (~> 1.0.0)
         | 
| 24 22 | 
             
                  git (>= 1.2.5)
         | 
| 25 | 
            -
                   | 
| 26 | 
            -
                 | 
| 27 | 
            -
                rspec (2.0 | 
| 28 | 
            -
                  rspec-core (~> 2.0 | 
| 29 | 
            -
                  rspec-expectations (~> 2.0 | 
| 30 | 
            -
                  rspec-mocks (~> 2.0 | 
| 31 | 
            -
                rspec-core (2. | 
| 32 | 
            -
                rspec-expectations (2.0 | 
| 33 | 
            -
                  diff-lcs ( | 
| 34 | 
            -
                rspec-mocks (2.0 | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
                 | 
| 38 | 
            -
                  json_pure (>= 1.1.7)
         | 
| 39 | 
            -
                tzinfo (0.3.23)
         | 
| 40 | 
            -
                yard (0.6.1)
         | 
| 23 | 
            +
                  rake
         | 
| 24 | 
            +
                rake (0.8.7)
         | 
| 25 | 
            +
                rspec (2.5.0)
         | 
| 26 | 
            +
                  rspec-core (~> 2.5.0)
         | 
| 27 | 
            +
                  rspec-expectations (~> 2.5.0)
         | 
| 28 | 
            +
                  rspec-mocks (~> 2.5.0)
         | 
| 29 | 
            +
                rspec-core (2.5.1)
         | 
| 30 | 
            +
                rspec-expectations (2.5.0)
         | 
| 31 | 
            +
                  diff-lcs (~> 1.1.2)
         | 
| 32 | 
            +
                rspec-mocks (2.5.0)
         | 
| 33 | 
            +
                sqlite3 (1.3.3)
         | 
| 34 | 
            +
                tzinfo (0.3.24)
         | 
| 35 | 
            +
                yard (0.6.4)
         | 
| 41 36 |  | 
| 42 37 | 
             
            PLATFORMS
         | 
| 43 38 | 
             
              ruby
         | 
| @@ -48,4 +43,5 @@ DEPENDENCIES | |
| 48 43 | 
             
              activesupport
         | 
| 49 44 | 
             
              jeweler
         | 
| 50 45 | 
             
              rspec
         | 
| 46 | 
            +
              sqlite3
         | 
| 51 47 | 
             
              yard
         | 
    
        data/README.textile
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            h1. json_serialize -- JSON serialization in ActiveRecord
         | 
| 2 2 |  | 
| 3 3 | 
             
            | *Author* | Tim Morgan |
         | 
| 4 | 
            -
            | *Version* |  | 
| 4 | 
            +
            | *Version* | 2.0 (Feb 21, 2011) |
         | 
| 5 5 | 
             
            | *License* | Released under the MIT license. |
         | 
| 6 6 |  | 
| 7 7 | 
             
            h2. About
         | 
| @@ -19,12 +19,12 @@ Firstly, add the gem to your Rails project's @Gemfile@: | |
| 19 19 | 
             
            gem 'json_serialize'
         | 
| 20 20 | 
             
            </code></pre>
         | 
| 21 21 |  | 
| 22 | 
            -
            Then,  | 
| 22 | 
            +
            Then, include into your model the @JsonSerialize@ module, and call the
         | 
| 23 23 | 
             
            @json_serialize@ method to indicate which fields should be serialized:
         | 
| 24 24 |  | 
| 25 25 | 
             
            <pre><code>
         | 
| 26 26 | 
             
            class MyModel < ActiveRecord::Base
         | 
| 27 | 
            -
               | 
| 27 | 
            +
              include JsonSerialize
         | 
| 28 28 | 
             
              json_serialize :favorites, :preferences
         | 
| 29 29 | 
             
            end
         | 
| 30 30 | 
             
            </code></pre>
         | 
    
        data/Rakefile
    CHANGED
    
    | @@ -1,13 +1,15 @@ | |
| 1 | 
            -
            require ' | 
| 1 | 
            +
            require 'rubygems'
         | 
| 2 | 
            +
            require 'bundler'
         | 
| 2 3 | 
             
            begin
         | 
| 3 | 
            -
               | 
| 4 | 
            -
            rescue  | 
| 5 | 
            -
              puts  | 
| 6 | 
            -
               | 
| 4 | 
            +
              Bundler.setup(:default, :development)
         | 
| 5 | 
            +
            rescue Bundler::BundlerError => e
         | 
| 6 | 
            +
              $stderr.puts e.message
         | 
| 7 | 
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         | 
| 8 | 
            +
              exit e.status_code
         | 
| 7 9 | 
             
            end
         | 
| 10 | 
            +
            require 'rake'
         | 
| 8 11 |  | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 12 | 
            +
            require 'jeweler'
         | 
| 11 13 | 
             
            Jeweler::Tasks.new do |gem|
         | 
| 12 14 | 
             
              gem.name = "json_serialize"
         | 
| 13 15 | 
             
              gem.summary = %Q{Adds JSON serialization to ActiveRecord models}
         | 
| @@ -15,17 +17,17 @@ Jeweler::Tasks.new do |gem| | |
| 15 17 | 
             
              gem.email = "git@timothymorgan.info"
         | 
| 16 18 | 
             
              gem.homepage = "http://github.com/riscfuture/json_serialize"
         | 
| 17 19 | 
             
              gem.authors = [ "Tim Morgan" ]
         | 
| 18 | 
            -
              gem.required_ruby_version = '>= 1.9'
         | 
| 19 | 
            -
              gem.add_dependency "activesupport", ">= 2.0"
         | 
| 20 20 | 
             
            end
         | 
| 21 | 
            -
            Jeweler:: | 
| 21 | 
            +
            Jeweler::RubygemsDotOrgTasks.new
         | 
| 22 22 |  | 
| 23 23 | 
             
            require 'rspec/core/rake_task'
         | 
| 24 24 | 
             
            RSpec::Core::RakeTask.new
         | 
| 25 25 |  | 
| 26 | 
            +
            require 'yard'
         | 
| 26 27 | 
             
            YARD::Rake::YardocTask.new('doc') do |doc|
         | 
| 27 28 | 
             
              doc.options << "-m" << "textile"
         | 
| 28 29 | 
             
              doc.options << "--protected"
         | 
| 30 | 
            +
              doc.options << "--no-private"
         | 
| 29 31 | 
             
              doc.options << "-r" << "README.textile"
         | 
| 30 32 | 
             
              doc.options << "-o" << "doc"
         | 
| 31 33 | 
             
              doc.options << "--title" << "json_serialize Documentation".inspect
         | 
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            2.0.0
         | 
    
        data/json_serialize.gemspec
    CHANGED
    
    | @@ -1,58 +1,72 @@ | |
| 1 1 | 
             
            # Generated by jeweler
         | 
| 2 2 | 
             
            # DO NOT EDIT THIS FILE DIRECTLY
         | 
| 3 | 
            -
            # Instead, edit Jeweler::Tasks in Rakefile, and run  | 
| 3 | 
            +
            # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
         | 
| 4 4 | 
             
            # -*- encoding: utf-8 -*-
         | 
| 5 5 |  | 
| 6 6 | 
             
            Gem::Specification.new do |s|
         | 
| 7 7 | 
             
              s.name = %q{json_serialize}
         | 
| 8 | 
            -
              s.version = " | 
| 8 | 
            +
              s.version = "2.0.0"
         | 
| 9 9 |  | 
| 10 10 | 
             
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         | 
| 11 11 | 
             
              s.authors = ["Tim Morgan"]
         | 
| 12 | 
            -
              s.date = %q{ | 
| 12 | 
            +
              s.date = %q{2011-02-21}
         | 
| 13 13 | 
             
              s.description = %q{Adds to ActiveRecord the ability to JSON-serialize certain fields.}
         | 
| 14 14 | 
             
              s.email = %q{git@timothymorgan.info}
         | 
| 15 15 | 
             
              s.extra_rdoc_files = [
         | 
| 16 16 | 
             
                "LICENSE",
         | 
| 17 | 
            -
             | 
| 17 | 
            +
                "README.textile"
         | 
| 18 18 | 
             
              ]
         | 
| 19 19 | 
             
              s.files = [
         | 
| 20 20 | 
             
                ".document",
         | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
                 "spec/spec_helper.rb"
         | 
| 21 | 
            +
                ".rspec",
         | 
| 22 | 
            +
                "Gemfile",
         | 
| 23 | 
            +
                "Gemfile.lock",
         | 
| 24 | 
            +
                "LICENSE",
         | 
| 25 | 
            +
                "README.textile",
         | 
| 26 | 
            +
                "Rakefile",
         | 
| 27 | 
            +
                "VERSION",
         | 
| 28 | 
            +
                "json_serialize.gemspec",
         | 
| 29 | 
            +
                "lib/json_serialize.rb",
         | 
| 30 | 
            +
                "spec/json_serialize_spec.rb",
         | 
| 31 | 
            +
                "spec/spec_helper.rb"
         | 
| 33 32 | 
             
              ]
         | 
| 34 33 | 
             
              s.homepage = %q{http://github.com/riscfuture/json_serialize}
         | 
| 35 | 
            -
              s.rdoc_options = ["--charset=UTF-8"]
         | 
| 36 34 | 
             
              s.require_paths = ["lib"]
         | 
| 37 | 
            -
              s. | 
| 38 | 
            -
              s.rubygems_version = %q{1.3.7}
         | 
| 35 | 
            +
              s.rubygems_version = %q{1.5.2}
         | 
| 39 36 | 
             
              s.summary = %q{Adds JSON serialization to ActiveRecord models}
         | 
| 40 37 | 
             
              s.test_files = [
         | 
| 41 38 | 
             
                "spec/json_serialize_spec.rb",
         | 
| 42 | 
            -
             | 
| 39 | 
            +
                "spec/spec_helper.rb"
         | 
| 43 40 | 
             
              ]
         | 
| 44 41 |  | 
| 45 42 | 
             
              if s.respond_to? :specification_version then
         | 
| 46 | 
            -
                current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
         | 
| 47 43 | 
             
                s.specification_version = 3
         | 
| 48 44 |  | 
| 49 45 | 
             
                if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
         | 
| 50 | 
            -
                  s.add_runtime_dependency(%q< | 
| 46 | 
            +
                  s.add_runtime_dependency(%q<activerecord>, [">= 0"])
         | 
| 47 | 
            +
                  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
         | 
| 48 | 
            +
                  s.add_development_dependency(%q<jeweler>, [">= 0"])
         | 
| 49 | 
            +
                  s.add_development_dependency(%q<yard>, [">= 0"])
         | 
| 50 | 
            +
                  s.add_development_dependency(%q<RedCloth>, [">= 0"])
         | 
| 51 | 
            +
                  s.add_development_dependency(%q<rspec>, [">= 0"])
         | 
| 52 | 
            +
                  s.add_development_dependency(%q<sqlite3>, [">= 0"])
         | 
| 51 53 | 
             
                else
         | 
| 52 | 
            -
                  s.add_dependency(%q< | 
| 54 | 
            +
                  s.add_dependency(%q<activerecord>, [">= 0"])
         | 
| 55 | 
            +
                  s.add_dependency(%q<activesupport>, [">= 0"])
         | 
| 56 | 
            +
                  s.add_dependency(%q<jeweler>, [">= 0"])
         | 
| 57 | 
            +
                  s.add_dependency(%q<yard>, [">= 0"])
         | 
| 58 | 
            +
                  s.add_dependency(%q<RedCloth>, [">= 0"])
         | 
| 59 | 
            +
                  s.add_dependency(%q<rspec>, [">= 0"])
         | 
| 60 | 
            +
                  s.add_dependency(%q<sqlite3>, [">= 0"])
         | 
| 53 61 | 
             
                end
         | 
| 54 62 | 
             
              else
         | 
| 55 | 
            -
                s.add_dependency(%q< | 
| 63 | 
            +
                s.add_dependency(%q<activerecord>, [">= 0"])
         | 
| 64 | 
            +
                s.add_dependency(%q<activesupport>, [">= 0"])
         | 
| 65 | 
            +
                s.add_dependency(%q<jeweler>, [">= 0"])
         | 
| 66 | 
            +
                s.add_dependency(%q<yard>, [">= 0"])
         | 
| 67 | 
            +
                s.add_dependency(%q<RedCloth>, [">= 0"])
         | 
| 68 | 
            +
                s.add_dependency(%q<rspec>, [">= 0"])
         | 
| 69 | 
            +
                s.add_dependency(%q<sqlite3>, [">= 0"])
         | 
| 56 70 | 
             
              end
         | 
| 57 71 | 
             
            end
         | 
| 58 72 |  | 
    
        data/lib/json_serialize.rb
    CHANGED
    
    | @@ -2,27 +2,69 @@ | |
| 2 2 | 
             
            #
         | 
| 3 3 | 
             
            # @example Basic usage
         | 
| 4 4 | 
             
            #   class MyModel < ActiveRecord::Base
         | 
| 5 | 
            -
            #      | 
| 5 | 
            +
            #     include JsonSerialize
         | 
| 6 6 | 
             
            #     json_serialize :some_field
         | 
| 7 7 | 
             
            #   end
         | 
| 8 8 |  | 
| 9 9 | 
             
            module JsonSerialize
         | 
| 10 | 
            +
              extend ActiveSupport::Concern
         | 
| 10 11 |  | 
| 11 | 
            -
               | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 12 | 
            +
              module ClassMethods
         | 
| 13 | 
            +
                # @overload json_serialize(field, ...)
         | 
| 14 | 
            +
                #   Marks one or more fields as JSON-serialized. These fields are stored in
         | 
| 15 | 
            +
                #   the database as JSON and encoded/decoded automatically upon read/write.
         | 
| 16 | 
            +
                #   @param [Symbol] field The database field to JSON-serialize.
         | 
| 15 17 |  | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 18 | 
            +
                def json_serialize(*fields)
         | 
| 19 | 
            +
                  fields.each do |field|
         | 
| 20 | 
            +
                    define_method field do
         | 
| 21 | 
            +
                      if instance_variable_defined?(field_ivar(field)) then
         | 
| 22 | 
            +
                        instance_variable_get field_ivar(field)
         | 
| 23 | 
            +
                      else
         | 
| 24 | 
            +
                        encoded = read_attribute(field)
         | 
| 25 | 
            +
                        decoded = encoded.nil? ? nil : ActiveSupport::JSON.decode(encoded)
         | 
| 26 | 
            +
                        instance_variable_set field_ivar(field), decoded
         | 
| 27 | 
            +
                        decoded
         | 
| 28 | 
            +
                      end
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
              
         | 
| 31 | 
            +
                    define_method :"#{field}=" do |value|
         | 
| 32 | 
            +
                      write_attribute field, (value.nil? ? nil : ActiveSupport::JSON.encode(value))
         | 
| 33 | 
            +
                      instance_variable_set field_ivar(field), value
         | 
| 34 | 
            +
                    end
         | 
| 21 35 | 
             
                  end
         | 
| 22 36 |  | 
| 23 | 
            -
                  define_method : | 
| 24 | 
            -
                     | 
| 37 | 
            +
                  define_method :serialize_json_values do
         | 
| 38 | 
            +
                    fields.each do |field|
         | 
| 39 | 
            +
                      if instance_variable_defined?(field_ivar(field)) then
         | 
| 40 | 
            +
                        send :"#{field}=", instance_variable_get(field_ivar(field))
         | 
| 41 | 
            +
                      end
         | 
| 42 | 
            +
                    end
         | 
| 25 43 | 
             
                  end
         | 
| 44 | 
            +
                  
         | 
| 45 | 
            +
                  define_method :reload_with_refresh_json_ivars do |*args|
         | 
| 46 | 
            +
                    res = reload_without_refresh_json_ivars *args
         | 
| 47 | 
            +
                    fields.each { |field| remove_instance_variable field_ivar(field) if instance_variable_defined?(field_ivar(field)) }
         | 
| 48 | 
            +
                    res
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
                  
         | 
| 51 | 
            +
                  define_method :update_with_refresh_json_ivars do |*args|
         | 
| 52 | 
            +
                    res = update_without_refresh_json_ivars(*args)
         | 
| 53 | 
            +
                    fields.each { |field| remove_instance_variable field_ivar(field) if instance_variable_defined?(field_ivar(field)) }
         | 
| 54 | 
            +
                    res
         | 
| 55 | 
            +
                  end
         | 
| 56 | 
            +
                  
         | 
| 57 | 
            +
                  before_validation :serialize_json_values
         | 
| 58 | 
            +
                  alias_method_chain :reload, :refresh_json_ivars
         | 
| 59 | 
            +
                  alias_method_chain :update, :refresh_json_ivars
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
              end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
              module InstanceMethods
         | 
| 64 | 
            +
                private
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                def field_ivar(name)
         | 
| 67 | 
            +
                  :"@_deserialized_#{name}"
         | 
| 26 68 | 
             
                end
         | 
| 27 69 | 
             
              end
         | 
| 28 70 | 
             
            end
         | 
    
        data/spec/json_serialize_spec.rb
    CHANGED
    
    | @@ -1,43 +1,47 @@ | |
| 1 1 | 
             
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 2 |  | 
| 3 | 
            -
            module SpecSupport
         | 
| 4 | 
            -
              class JsonSerializeTester
         | 
| 5 | 
            -
                extend JsonSerialize
         | 
| 6 | 
            -
                json_serialize :field
         | 
| 7 | 
            -
                def read_attribute(field) @value end
         | 
| 8 | 
            -
                def write_attribute(field, value) @value = value end
         | 
| 9 | 
            -
                def get() @value end
         | 
| 10 | 
            -
                def set(value) @value = value end
         | 
| 11 | 
            -
              end
         | 
| 12 | 
            -
            end
         | 
| 13 | 
            -
             | 
| 14 3 | 
             
            describe JsonSerialize do
         | 
| 15 | 
            -
              before :each do
         | 
| 16 | 
            -
                @object = SpecSupport::JsonSerializeTester.new
         | 
| 17 | 
            -
              end
         | 
| 18 | 
            -
             | 
| 19 4 | 
             
              describe "#json_serialize" do
         | 
| 20 | 
            -
                context "getter" do
         | 
| 5 | 
            +
                context "[getter]" do
         | 
| 21 6 | 
             
                  it "should JSON-decode the value" do
         | 
| 22 | 
            -
                     | 
| 23 | 
            -
                     | 
| 7 | 
            +
                    require 'logger'
         | 
| 8 | 
            +
                    object = Json.create!
         | 
| 9 | 
            +
                    Json.update_all(data: '{"foo":"bar"}')
         | 
| 10 | 
            +
                    object.reload.data.should eql('foo' => 'bar')
         | 
| 24 11 | 
             
                  end
         | 
| 25 12 |  | 
| 26 13 | 
             
                  it "should return nil if the value is nil" do
         | 
| 27 | 
            -
                     | 
| 28 | 
            -
                     | 
| 14 | 
            +
                    object = Json.create!
         | 
| 15 | 
            +
                    Json.update_all(data: nil)
         | 
| 16 | 
            +
                    object.reload.data.should be_nil
         | 
| 29 17 | 
             
                  end
         | 
| 30 18 | 
             
                end
         | 
| 31 19 |  | 
| 32 | 
            -
                context "setter" do
         | 
| 20 | 
            +
                context "[setter]" do
         | 
| 33 21 | 
             
                  it "should JSON-encode the value" do
         | 
| 34 | 
            -
                     | 
| 35 | 
            -
                     | 
| 22 | 
            +
                    object = Json.create!(data: { foo: 'bar' })
         | 
| 23 | 
            +
                    object.send(:read_attribute, :data).should eql({ foo: 'bar' }.to_json)
         | 
| 36 24 | 
             
                  end
         | 
| 37 25 |  | 
| 38 26 | 
             
                  it "should leave nil as nil" do
         | 
| 39 | 
            -
                     | 
| 40 | 
            -
                     | 
| 27 | 
            +
                    object = Json.create!(data: nil)
         | 
| 28 | 
            +
                    object.send(:read_attribute, :data).should be_nil
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
                
         | 
| 32 | 
            +
                context "[database backing]" do
         | 
| 33 | 
            +
                  it "should back the instance object with the database" do
         | 
| 34 | 
            +
                    object = Json.create!(data: { foo: 'bar' })
         | 
| 35 | 
            +
                    object.data[:foo2] = 'bar2'
         | 
| 36 | 
            +
                    object.save!
         | 
| 37 | 
            +
                    object.data.should eql('foo' => 'bar', 'foo2' => 'bar2')
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                  
         | 
| 40 | 
            +
                  it "should clear the in-memory reference on reload" do
         | 
| 41 | 
            +
                    object = Json.create!(data: { foo: 'bar' })
         | 
| 42 | 
            +
                    object.data[:foo2] = 'bar2'
         | 
| 43 | 
            +
                    object.reload
         | 
| 44 | 
            +
                    object.data.should eql('foo' => 'bar')
         | 
| 41 45 | 
             
                  end
         | 
| 42 46 | 
             
                end
         | 
| 43 47 | 
             
              end
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    | @@ -5,6 +5,19 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) | |
| 5 5 |  | 
| 6 6 | 
             
            require 'json_serialize'
         | 
| 7 7 |  | 
| 8 | 
            +
            ActiveRecord::Base.establish_connection(
         | 
| 9 | 
            +
              adapter: 'sqlite3',
         | 
| 10 | 
            +
              database: 'test.sqlite'
         | 
| 11 | 
            +
            )
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            class Json < ActiveRecord::Base
         | 
| 14 | 
            +
              include JsonSerialize
         | 
| 15 | 
            +
              json_serialize :data
         | 
| 16 | 
            +
            end
         | 
| 17 | 
            +
             | 
| 8 18 | 
             
            RSpec.configure do |config|
         | 
| 9 | 
            -
              
         | 
| 19 | 
            +
              config.before(:each) do
         | 
| 20 | 
            +
                Json.connection.execute "DROP TABLE IF EXISTS jsons"
         | 
| 21 | 
            +
                Json.connection.execute "CREATE TABLE jsons (id INTEGER PRIMARY KEY ASC, data TEXT)"
         | 
| 22 | 
            +
              end
         | 
| 10 23 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,12 +1,8 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: json_serialize
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              prerelease:  | 
| 5 | 
            -
               | 
| 6 | 
            -
              - 1
         | 
| 7 | 
            -
              - 0
         | 
| 8 | 
            -
              - 0
         | 
| 9 | 
            -
              version: 1.0.0
         | 
| 4 | 
            +
              prerelease: 
         | 
| 5 | 
            +
              version: 2.0.0
         | 
| 10 6 | 
             
            platform: ruby
         | 
| 11 7 | 
             
            authors: 
         | 
| 12 8 | 
             
            - Tim Morgan
         | 
| @@ -14,23 +10,86 @@ autorequire: | |
| 14 10 | 
             
            bindir: bin
         | 
| 15 11 | 
             
            cert_chain: []
         | 
| 16 12 |  | 
| 17 | 
            -
            date:  | 
| 13 | 
            +
            date: 2011-02-21 00:00:00 -08:00
         | 
| 18 14 | 
             
            default_executable: 
         | 
| 19 15 | 
             
            dependencies: 
         | 
| 20 16 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 21 | 
            -
              name:  | 
| 17 | 
            +
              name: activerecord
         | 
| 22 18 | 
             
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 23 19 | 
             
                none: false
         | 
| 24 20 | 
             
                requirements: 
         | 
| 25 21 | 
             
                - - ">="
         | 
| 26 22 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 27 | 
            -
                     | 
| 28 | 
            -
                    - 2
         | 
| 29 | 
            -
                    - 0
         | 
| 30 | 
            -
                    version: "2.0"
         | 
| 23 | 
            +
                    version: "0"
         | 
| 31 24 | 
             
              type: :runtime
         | 
| 32 25 | 
             
              prerelease: false
         | 
| 33 26 | 
             
              version_requirements: *id001
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 28 | 
            +
              name: activesupport
         | 
| 29 | 
            +
              requirement: &id002 !ruby/object:Gem::Requirement 
         | 
| 30 | 
            +
                none: false
         | 
| 31 | 
            +
                requirements: 
         | 
| 32 | 
            +
                - - ">="
         | 
| 33 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 34 | 
            +
                    version: "0"
         | 
| 35 | 
            +
              type: :runtime
         | 
| 36 | 
            +
              prerelease: false
         | 
| 37 | 
            +
              version_requirements: *id002
         | 
| 38 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 39 | 
            +
              name: jeweler
         | 
| 40 | 
            +
              requirement: &id003 !ruby/object:Gem::Requirement 
         | 
| 41 | 
            +
                none: false
         | 
| 42 | 
            +
                requirements: 
         | 
| 43 | 
            +
                - - ">="
         | 
| 44 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 45 | 
            +
                    version: "0"
         | 
| 46 | 
            +
              type: :development
         | 
| 47 | 
            +
              prerelease: false
         | 
| 48 | 
            +
              version_requirements: *id003
         | 
| 49 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 50 | 
            +
              name: yard
         | 
| 51 | 
            +
              requirement: &id004 !ruby/object:Gem::Requirement 
         | 
| 52 | 
            +
                none: false
         | 
| 53 | 
            +
                requirements: 
         | 
| 54 | 
            +
                - - ">="
         | 
| 55 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 56 | 
            +
                    version: "0"
         | 
| 57 | 
            +
              type: :development
         | 
| 58 | 
            +
              prerelease: false
         | 
| 59 | 
            +
              version_requirements: *id004
         | 
| 60 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 61 | 
            +
              name: RedCloth
         | 
| 62 | 
            +
              requirement: &id005 !ruby/object:Gem::Requirement 
         | 
| 63 | 
            +
                none: false
         | 
| 64 | 
            +
                requirements: 
         | 
| 65 | 
            +
                - - ">="
         | 
| 66 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 67 | 
            +
                    version: "0"
         | 
| 68 | 
            +
              type: :development
         | 
| 69 | 
            +
              prerelease: false
         | 
| 70 | 
            +
              version_requirements: *id005
         | 
| 71 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 72 | 
            +
              name: rspec
         | 
| 73 | 
            +
              requirement: &id006 !ruby/object:Gem::Requirement 
         | 
| 74 | 
            +
                none: false
         | 
| 75 | 
            +
                requirements: 
         | 
| 76 | 
            +
                - - ">="
         | 
| 77 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 78 | 
            +
                    version: "0"
         | 
| 79 | 
            +
              type: :development
         | 
| 80 | 
            +
              prerelease: false
         | 
| 81 | 
            +
              version_requirements: *id006
         | 
| 82 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 83 | 
            +
              name: sqlite3
         | 
| 84 | 
            +
              requirement: &id007 !ruby/object:Gem::Requirement 
         | 
| 85 | 
            +
                none: false
         | 
| 86 | 
            +
                requirements: 
         | 
| 87 | 
            +
                - - ">="
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 89 | 
            +
                    version: "0"
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: *id007
         | 
| 34 93 | 
             
            description: Adds to ActiveRecord the ability to JSON-serialize certain fields.
         | 
| 35 94 | 
             
            email: git@timothymorgan.info
         | 
| 36 95 | 
             
            executables: []
         | 
| @@ -42,7 +101,6 @@ extra_rdoc_files: | |
| 42 101 | 
             
            - README.textile
         | 
| 43 102 | 
             
            files: 
         | 
| 44 103 | 
             
            - .document
         | 
| 45 | 
            -
            - .gitignore
         | 
| 46 104 | 
             
            - .rspec
         | 
| 47 105 | 
             
            - Gemfile
         | 
| 48 106 | 
             
            - Gemfile.lock
         | 
| @@ -59,8 +117,8 @@ homepage: http://github.com/riscfuture/json_serialize | |
| 59 117 | 
             
            licenses: []
         | 
| 60 118 |  | 
| 61 119 | 
             
            post_install_message: 
         | 
| 62 | 
            -
            rdoc_options: 
         | 
| 63 | 
            -
             | 
| 120 | 
            +
            rdoc_options: []
         | 
| 121 | 
            +
             | 
| 64 122 | 
             
            require_paths: 
         | 
| 65 123 | 
             
            - lib
         | 
| 66 124 | 
             
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| @@ -68,22 +126,20 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 68 126 | 
             
              requirements: 
         | 
| 69 127 | 
             
              - - ">="
         | 
| 70 128 | 
             
                - !ruby/object:Gem::Version 
         | 
| 129 | 
            +
                  hash: 3149432936213936570
         | 
| 71 130 | 
             
                  segments: 
         | 
| 72 | 
            -
                  -  | 
| 73 | 
            -
                   | 
| 74 | 
            -
                  version: "1.9"
         | 
| 131 | 
            +
                  - 0
         | 
| 132 | 
            +
                  version: "0"
         | 
| 75 133 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 76 134 | 
             
              none: false
         | 
| 77 135 | 
             
              requirements: 
         | 
| 78 136 | 
             
              - - ">="
         | 
| 79 137 | 
             
                - !ruby/object:Gem::Version 
         | 
| 80 | 
            -
                  segments: 
         | 
| 81 | 
            -
                  - 0
         | 
| 82 138 | 
             
                  version: "0"
         | 
| 83 139 | 
             
            requirements: []
         | 
| 84 140 |  | 
| 85 141 | 
             
            rubyforge_project: 
         | 
| 86 | 
            -
            rubygems_version: 1. | 
| 142 | 
            +
            rubygems_version: 1.5.2
         | 
| 87 143 | 
             
            signing_key: 
         | 
| 88 144 | 
             
            specification_version: 3
         | 
| 89 145 | 
             
            summary: Adds JSON serialization to ActiveRecord models
         | 
    
        data/.gitignore
    DELETED
    
    | @@ -1,25 +0,0 @@ | |
| 1 | 
            -
            ## MAC OS
         | 
| 2 | 
            -
            .DS_Store
         | 
| 3 | 
            -
             | 
| 4 | 
            -
            ## TEXTMATE
         | 
| 5 | 
            -
            *.tmproj
         | 
| 6 | 
            -
            tmtags
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            ## EMACS
         | 
| 9 | 
            -
            *~
         | 
| 10 | 
            -
            \#*
         | 
| 11 | 
            -
            .\#*
         | 
| 12 | 
            -
             | 
| 13 | 
            -
            ## VIM
         | 
| 14 | 
            -
            *.swp
         | 
| 15 | 
            -
             | 
| 16 | 
            -
            ## PROJECT::GENERAL
         | 
| 17 | 
            -
            coverage
         | 
| 18 | 
            -
            rdoc
         | 
| 19 | 
            -
            pkg
         | 
| 20 | 
            -
            .bundle
         | 
| 21 | 
            -
            .rvmrc
         | 
| 22 | 
            -
             | 
| 23 | 
            -
            ## PROJECT::DOCUMENTATION
         | 
| 24 | 
            -
            .yardoc
         | 
| 25 | 
            -
            doc
         |