laserlemon-dry_serial 0.1.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Steve Richert
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.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = dry_serial
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 laserlemon. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |g|
9
+ g.name = 'dry_serial'
10
+ g.summary = %(Define serialization styles for your ActiveRecord models)
11
+ g.description = %(Define serialization styles for your ActiveRecord models)
12
+ g.email = 'steve@laserlemon.com'
13
+ g.homepage = 'http://github.com/laserlemon/dry_serial'
14
+ g.authors = %w(laserlemon)
15
+ g.add_development_dependency 'activerecord'
16
+ g.add_development_dependency 'thoughtbot-shoulda'
17
+ end
18
+ rescue LoadError
19
+ puts 'Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler'
20
+ end
21
+
22
+ Rake::TestTask.new(:test) do |t|
23
+ t.libs = %w(test)
24
+ t.pattern = 'test/**/*_test.rb'
25
+ end
26
+
27
+ task :test => :check_dependencies
28
+
29
+ task :default => :test
30
+
31
+ Rake::RDocTask.new do |r|
32
+ version = File.exist?('VERSION') ? File.read('VERSION') : nil
33
+ r.rdoc_dir = 'rdoc'
34
+ r.title = ['dry_serial', version].compact.join(' ')
35
+ r.rdoc_files.include('README*')
36
+ r.rdoc_files.include('lib/**/*.rb')
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dry_serial}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["laserlemon"]
12
+ s.date = %q{2009-08-24}
13
+ s.description = %q{Define serialization styles for your ActiveRecord models}
14
+ s.email = %q{steve@laserlemon.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "dry_serial.gemspec",
27
+ "init.rb",
28
+ "lib/dry_serial.rb",
29
+ "lib/serialization.rb",
30
+ "test/dry_serial_test.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/laserlemon/dry_serial}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Define serialization styles for your ActiveRecord models}
38
+ s.test_files = [
39
+ "test/dry_serial_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<activerecord>, [">= 0"])
49
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<activerecord>, [">= 0"])
52
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<activerecord>, [">= 0"])
56
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
+ end
58
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'dry_serial'
data/lib/dry_serial.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'serialization'
2
+
3
+ module LaserLemon
4
+ module DrySerial
5
+ def self.included(base)
6
+ base.class_eval do
7
+ class_inheritable_hash :serialization_options
8
+ self.serialization_options = {}
9
+
10
+ class_inheritable_accessor :default_serialization_style
11
+ self.default_serialization_style = nil
12
+ end
13
+ base.extend ClassMethods
14
+ end
15
+
16
+ module ClassMethods
17
+ def dry_serial(*args)
18
+ options = args.extract_options!
19
+ style = args.first || :default
20
+ self.serialization_options[style] = options
21
+ self.default_serialization_style ||= style
22
+ end
23
+
24
+ def default_serialization_options
25
+ self.serialization_options.fetch(self.default_serialization_style, {})
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ ActiveRecord::Base.send(:include, LaserLemon::DrySerial)
@@ -0,0 +1,81 @@
1
+ module LaserLemon
2
+ module DrySerial
3
+ module Serializer
4
+ def self.included(base)
5
+ base.alias_method_chain :initialize, :breakfast
6
+ base.alias_method_chain :add_includes, :breakfast
7
+ base.alias_method_chain :serializable_names, :breakfast
8
+ end
9
+
10
+ def initialize_with_breakfast(record, options = nil)
11
+ default_options = record.class.default_serialization_options
12
+
13
+ new_options = case options
14
+ when Hash
15
+ options.reverse_merge!(:style => record.class.default_serialization_style)
16
+ style_options = record.class.serialization_options.fetch(options[:style], default_options)
17
+ options.reverse_merge(style_options)
18
+ else
19
+ record.class.serialization_options.fetch(options, default_options).merge(:style => options)
20
+ end
21
+
22
+ initialize_without_breakfast(record, new_options)
23
+ end
24
+
25
+ def add_includes_with_breakfast(&block)
26
+ if include_associations = options.delete(:include)
27
+ include_has_options = include_associations.is_a?(Hash)
28
+ associations = include_has_options ? include_associations.keys : Array(include_associations)
29
+
30
+ for association in associations
31
+ records = case @record.class.reflect_on_association(association).macro
32
+ when :has_many, :has_and_belongs_to_many
33
+ @record.send(association).to_a
34
+ when :has_one, :belongs_to
35
+ @record.send(association)
36
+ end
37
+
38
+ unless records.nil?
39
+ association_options = include_has_options ? include_associations[association] : {}
40
+ association_options.reverse_merge!(:style => options[:style], :include => nil)
41
+ opts = options.except(:only, :except, :methods).merge(association_options)
42
+ yield(association, records, opts)
43
+ end
44
+ end
45
+
46
+ options[:include] = include_associations
47
+ end
48
+ end
49
+
50
+ def serializable_names_with_breakfast
51
+ serializable_names_without_breakfast.sort_by(&:to_s)
52
+ end
53
+ end
54
+
55
+ module XmlSerializer
56
+ def self.included(base)
57
+ base.alias_method_chain :add_attributes, :breakfast
58
+ end
59
+
60
+ def add_attributes_with_breakfast
61
+ (serializable_attributes + serializable_method_attributes).sort.each do |attribute|
62
+ add_tag(attribute)
63
+ end
64
+ end
65
+
66
+ module Attribute
67
+ def self.included(base)
68
+ base.send(:include, Comparable)
69
+ end
70
+
71
+ def <=>(other)
72
+ name.to_s <=> other.name.to_s
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ ActiveRecord::Serialization::Serializer.send(:include, LaserLemon::DrySerial::Serializer)
80
+ ActiveRecord::XmlSerializer.send(:include, LaserLemon::DrySerial::XmlSerializer)
81
+ ActiveRecord::XmlSerializer::Attribute.send(:include, LaserLemon::DrySerial::XmlSerializer::Attribute)
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class DrySerialTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'dry_serial'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: laserlemon-dry_serial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - laserlemon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: thoughtbot-shoulda
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Define serialization styles for your ActiveRecord models
36
+ email: steve@laserlemon.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - dry_serial.gemspec
52
+ - init.rb
53
+ - lib/dry_serial.rb
54
+ - lib/serialization.rb
55
+ - test/dry_serial_test.rb
56
+ - test/test_helper.rb
57
+ has_rdoc: false
58
+ homepage: http://github.com/laserlemon/dry_serial
59
+ licenses:
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Define serialization styles for your ActiveRecord models
84
+ test_files:
85
+ - test/dry_serial_test.rb
86
+ - test/test_helper.rb