fancy_serializer 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/Gemfile +13 -0
 - data/MIT-LICENSE.txt +20 -0
 - data/README +29 -0
 - data/Rakefile +29 -0
 - data/lib/fancy_serializer.rb +71 -0
 - metadata +52 -0
 
    
        data/Gemfile
    ADDED
    
    | 
         @@ -0,0 +1,13 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            source "http://rubygems.org"
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            gem "rails", "3.1.0"
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            # In case you're encouterting weird behavior,
         
     | 
| 
      
 6 
     | 
    
         
            +
            # please test if it works with old Rails.
         
     | 
| 
      
 7 
     | 
    
         
            +
            #gem "rails", "3.0.10"
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            gem "sqlite3"
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
         
     | 
| 
      
 12 
     | 
    
         
            +
            # gem 'ruby-debug'
         
     | 
| 
      
 13 
     | 
    
         
            +
            # gem 'ruby-debug19'
         
     | 
    
        data/MIT-LICENSE.txt
    ADDED
    
    | 
         @@ -0,0 +1,20 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2011 [name of plugin creator]
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 4 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 5 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 6 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 7 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 8 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 9 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 12 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 15 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 16 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 17 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 18 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 19 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 20 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README
    ADDED
    
    | 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Builds on top of Rails serialize method to create accessors for your serialized fields.
         
     | 
| 
      
 2 
     | 
    
         
            +
            Please read this post as <a href="http://gregmoreno.ca/preventing-model-explosion-via-rails-serialization/">I explain my motivation</a> behind this.
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            Basically, it lets you do the following in your ActiveRecord models.
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            {% highlight ruby %}
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            class User
         
     | 
| 
      
 9 
     | 
    
         
            +
              serializeable :preferences, { :show_email => false }
         
     | 
| 
      
 10 
     | 
    
         
            +
            end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            u = User.new :show_email => true
         
     | 
| 
      
 13 
     | 
    
         
            +
            u.show_email
         
     | 
| 
      
 14 
     | 
    
         
            +
            => true
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            u = User.new
         
     | 
| 
      
 17 
     | 
    
         
            +
            u.show_email
         
     | 
| 
      
 18 
     | 
    
         
            +
            => false
         
     | 
| 
      
 19 
     | 
    
         
            +
            u.show_email = true
         
     | 
| 
      
 20 
     | 
    
         
            +
            => true
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            u = User.new :show_email => true
         
     | 
| 
      
 23 
     | 
    
         
            +
            u.save
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            u = User.find(u)
         
     | 
| 
      
 26 
     | 
    
         
            +
            u.show_email
         
     | 
| 
      
 27 
     | 
    
         
            +
            => true
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            {% endhighlight %}
         
     | 
    
        data/Rakefile
    ADDED
    
    | 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # encoding: UTF-8
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'rubygems'
         
     | 
| 
      
 3 
     | 
    
         
            +
            begin
         
     | 
| 
      
 4 
     | 
    
         
            +
              require 'bundler/setup'
         
     | 
| 
      
 5 
     | 
    
         
            +
            rescue LoadError
         
     | 
| 
      
 6 
     | 
    
         
            +
              puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
         
     | 
| 
      
 7 
     | 
    
         
            +
            end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            require 'rake'
         
     | 
| 
      
 10 
     | 
    
         
            +
            require 'rake/rdoctask'
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            require 'rake/testtask'
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            Rake::TestTask.new(:test) do |t|
         
     | 
| 
      
 15 
     | 
    
         
            +
              t.libs << 'lib'
         
     | 
| 
      
 16 
     | 
    
         
            +
              t.libs << 'test'
         
     | 
| 
      
 17 
     | 
    
         
            +
              t.pattern = 'test/**/*_test.rb'
         
     | 
| 
      
 18 
     | 
    
         
            +
              t.verbose = false
         
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            task :default => :test
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            Rake::RDocTask.new(:rdoc) do |rdoc|
         
     | 
| 
      
 24 
     | 
    
         
            +
              rdoc.rdoc_dir = 'rdoc'
         
     | 
| 
      
 25 
     | 
    
         
            +
              rdoc.title    = 'AttributeSerializer'
         
     | 
| 
      
 26 
     | 
    
         
            +
              rdoc.options << '--line-numbers' << '--inline-source'
         
     | 
| 
      
 27 
     | 
    
         
            +
              rdoc.rdoc_files.include('README.rdoc')
         
     | 
| 
      
 28 
     | 
    
         
            +
              rdoc.rdoc_files.include('lib/**/*.rb')
         
     | 
| 
      
 29 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,71 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module FancySerializer
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
              module ClassMethods
         
     | 
| 
      
 4 
     | 
    
         
            +
                # class User
         
     | 
| 
      
 5 
     | 
    
         
            +
                #   serializeable :preferences, { :show_email => false }
         
     | 
| 
      
 6 
     | 
    
         
            +
                # end
         
     | 
| 
      
 7 
     | 
    
         
            +
                #
         
     | 
| 
      
 8 
     | 
    
         
            +
                # u = User.new :show_email => true
         
     | 
| 
      
 9 
     | 
    
         
            +
                # u.show_email
         
     | 
| 
      
 10 
     | 
    
         
            +
                # => true
         
     | 
| 
      
 11 
     | 
    
         
            +
                #
         
     | 
| 
      
 12 
     | 
    
         
            +
                # u = User.new
         
     | 
| 
      
 13 
     | 
    
         
            +
                # u.show_email
         
     | 
| 
      
 14 
     | 
    
         
            +
                # => false
         
     | 
| 
      
 15 
     | 
    
         
            +
                # u.show_email = true
         
     | 
| 
      
 16 
     | 
    
         
            +
                # => true
         
     | 
| 
      
 17 
     | 
    
         
            +
                #
         
     | 
| 
      
 18 
     | 
    
         
            +
                # u = User.find(1)
         
     | 
| 
      
 19 
     | 
    
         
            +
                # u.show_email
         
     | 
| 
      
 20 
     | 
    
         
            +
                # => true or false
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                def serializeable(serialized, serialized_accessors={})  
         
     | 
| 
      
 23 
     | 
    
         
            +
                  serialize serialized, serialized_accessors.class
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                  serialized_attr_accessor serialized, serialized_accessors
         
     | 
| 
      
 26 
     | 
    
         
            +
                  serialized_attr_sanitizer serialized
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                # Creates the accessors
         
     | 
| 
      
 30 
     | 
    
         
            +
                def serialized_attr_accessor(serialized, accessors)
         
     | 
| 
      
 31 
     | 
    
         
            +
                  define_method("memoized_#{serialized}") do
         
     | 
| 
      
 32 
     | 
    
         
            +
                    if self[serialized].blank?
         
     | 
| 
      
 33 
     | 
    
         
            +
                      self[serialized] = accessors.clone
         
     | 
| 
      
 34 
     | 
    
         
            +
                    else
         
     | 
| 
      
 35 
     | 
    
         
            +
                      self[serialized]
         
     | 
| 
      
 36 
     | 
    
         
            +
                    end
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                  after_initialize "memoized_#{serialized}"
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                  accessors.keys.each do |k|
         
     | 
| 
      
 42 
     | 
    
         
            +
                    define_method("#{k}") do
         
     | 
| 
      
 43 
     | 
    
         
            +
                      self.send("memoized_#{serialized}")[k]
         
     | 
| 
      
 44 
     | 
    
         
            +
                    end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                    define_method("#{k}=") do |value|
         
     | 
| 
      
 47 
     | 
    
         
            +
                      self.send("memoized_#{serialized}")[k] = value
         
     | 
| 
      
 48 
     | 
    
         
            +
                    end
         
     | 
| 
      
 49 
     | 
    
         
            +
                  end
         
     | 
| 
      
 50 
     | 
    
         
            +
                end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                # Helper for removing the serialized field in your queries
         
     | 
| 
      
 53 
     | 
    
         
            +
                # You have to call +sanitize_serialize_attributes+ explicitly
         
     | 
| 
      
 54 
     | 
    
         
            +
                def serialized_attr_sanitizer(serialized)
         
     | 
| 
      
 55 
     | 
    
         
            +
                  sanitizer = lambda { |attributes|
         
     | 
| 
      
 56 
     | 
    
         
            +
                    sanitized = attributes.clone
         
     | 
| 
      
 57 
     | 
    
         
            +
                    sanitized.delete(serialized)
         
     | 
| 
      
 58 
     | 
    
         
            +
                    sanitized
         
     | 
| 
      
 59 
     | 
    
         
            +
                  }
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                  self.class.send(:define_method, "sanitize_serialized_attributes", sanitizer) 
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
              end # ClassMethods
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
            end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            class ActiveRecord::Base
         
     | 
| 
      
 69 
     | 
    
         
            +
              extend FancySerializer::ClassMethods
         
     | 
| 
      
 70 
     | 
    
         
            +
            end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,52 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: fancy_serializer
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Greg Moreno
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2011-09-07 00:00:00.000000000 -07:00
         
     | 
| 
      
 13 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 14 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 15 
     | 
    
         
            +
            description: Please read this post http://gregmoreno.ca/preventing-model-explosion-via-rails-serialization/
         
     | 
| 
      
 16 
     | 
    
         
            +
              for my motivation behind this gem
         
     | 
| 
      
 17 
     | 
    
         
            +
            email: rubyoncloud@gmail.com
         
     | 
| 
      
 18 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 19 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 20 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 21 
     | 
    
         
            +
            files:
         
     | 
| 
      
 22 
     | 
    
         
            +
            - lib/fancy_serializer.rb
         
     | 
| 
      
 23 
     | 
    
         
            +
            - MIT-LICENSE.txt
         
     | 
| 
      
 24 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 25 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 26 
     | 
    
         
            +
            - README
         
     | 
| 
      
 27 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 28 
     | 
    
         
            +
            homepage: http://gregmoreno.ca/preventing-model-explosion-via-rails-serialization/
         
     | 
| 
      
 29 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 30 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 31 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 32 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 33 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 34 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 35 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 36 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 37 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 38 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 39 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 40 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 41 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 42 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 43 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 44 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 45 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 46 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 47 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 48 
     | 
    
         
            +
            rubygems_version: 1.6.2
         
     | 
| 
      
 49 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 50 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 51 
     | 
    
         
            +
            summary: ActiveRecord method to create accessors for your serialized fields.
         
     | 
| 
      
 52 
     | 
    
         
            +
            test_files: []
         
     |