activerecord_to_poro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 587b516b162dcf78327bfe17f0321636477647c0
4
+ data.tar.gz: cbe5c6dfee693432493fa3f6edc03e17b5e103c4
5
+ SHA512:
6
+ metadata.gz: c842467aa6ac65f6108088d5160180fa3b4be50b267cc393e18345bccf3988a3a672a448e59a11b00ef3f5ee7cab755a9028ea7470a64d6c5367af604c25fa51
7
+ data.tar.gz: 395836a68972a580931c8480b4a7b8eb7cdbfbd5acfcccb1a20dda3ff456311be71a21ed0cfd2dc406f1d448578a7b32f1f8587daef5d7dd994678263a90dc30
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea/
7
+ .rspec
8
+ .versions.conf
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
21
+ *.sqlite3
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ notifications:
5
+ recipients:
6
+ - shad0wrunner@gmx.de
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+ ruby "2.1.0"
3
+
4
+ # Specify your gem's dependencies in yaoc.gemspec
5
+ gemspec
6
+
7
+ gem 'simplecov', require: false, group: :test
8
+ gem 'coveralls', require: false
9
+
10
+ gem 'yaoc', github: 'slowjack2k/yaoc', branch: 'master'
data/Guardfile ADDED
@@ -0,0 +1,25 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, all_after_pass: true ,
5
+ all_on_start: true do
6
+
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+
9
+ watch('lib/activerecord_to_poro.rb') { "spec" }
10
+
11
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/unit/lib/#{m[1]}_spec.rb" }
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/integration/lib/#{m[1]}_spec.rb" }
13
+
14
+ watch('spec/spec_helper.rb') { "spec" }
15
+
16
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
17
+
18
+ end
19
+
20
+
21
+ guard :bundler do
22
+ watch('Gemfile')
23
+ # Uncomment next line if your Gemfile contains the `gemspec' command.
24
+ watch(/^.+\.gemspec/)
25
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dieter Späth
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # ActiverecordToPoro [![Code Climate](https://codeclimate.com/github/slowjack2k/activerecord_to_poro.png)](https://codeclimate.com/github/slowjack2k/activerecord_to_poro ) [![Build Status](https://travis-ci.org/slowjack2k/activerecord_to_poro.png?branch=master)](https://travis-ci.org/slowjack2k/activerecord_to_poro) [![Coverage Status](https://coveralls.io/repos/slowjack2k/activerecord_to_poro/badge.png?branch=master)](https://coveralls.io/r/slowjack2k/activerecord_to_poro?branch=master) [![Gem Version](https://badge.fury.io/rb/activerecord_to_poro.png)](http://badge.fury.io/rb/activerecord_to_poro)
2
+
3
+ Final goal is a mapping of ActiveRecord-Objects to plain old ruby objects.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activerecord_to_poro'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activerecord_to_poro
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+
23
+ roles_converter = ActiverecordToPoro::Converter.new(Role)
24
+ salutation_converter = ActiverecordToPoro::Converter.new(Salutation)
25
+ user_converter = ActiverecordToPoro::Converter.new(User, roles: roles_converter, salutation: salutation_converter)
26
+
27
+
28
+ poro = user_converter.load(User.first)
29
+
30
+
31
+
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( http://github.com/slowjack2k/activerecord_to_poro/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
8
+
9
+ desc "Run RSpec with code coverage"
10
+ task :coverage do
11
+ ENV['SIMPLE_COVERAGE'] = "true"
12
+ Rake::Task["spec"].execute
13
+ end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord_to_poro/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord_to_poro"
8
+ spec.version = ActiverecordToPoro::VERSION
9
+ spec.authors = ["Dieter Späth"]
10
+ spec.email = ["shad0wrunner@gmx.de"]
11
+ spec.summary = %q{Convert ActiveRecord objects to plain old ruby objects and vice versa}
12
+ spec.description = %q{Convert ActiveRecord objects to plain old ruby objects and vice versa}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_runtime_dependency 'abstract_type', '~> 0.0.7'
24
+ spec.add_runtime_dependency 'yaoc'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.5"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency 'sqlite3'
29
+ spec.add_development_dependency 'activerecord'
30
+ spec.add_development_dependency 'database_cleaner'
31
+
32
+
33
+ spec.add_development_dependency "rspec", "2.99.0.beta1"
34
+ # show nicely how many specs have to be run
35
+ spec.add_development_dependency "fuubar"
36
+ # extended console
37
+ spec.add_development_dependency "pry"
38
+ spec.add_development_dependency 'pry-remote'
39
+
40
+ # automatic execute tasks on file changes
41
+ spec.add_development_dependency 'guard'
42
+ spec.add_development_dependency 'guard-rspec'
43
+ spec.add_development_dependency 'guard-bundler'
44
+ spec.add_development_dependency 'rb-fsevent'
45
+
46
+ # https://github.com/pry/pry-stack_explorer
47
+ spec.add_development_dependency 'pry-stack_explorer'
48
+ # https://github.com/nixme/pry-debugger
49
+ spec.add_development_dependency 'pry-debugger'
50
+
51
+ # https://github.com/dkubb/equalizer
52
+ spec.add_development_dependency 'equalizer'
53
+ end
@@ -0,0 +1,97 @@
1
+ module ActiverecordToPoro
2
+ class Converter
3
+ include ColumnHelper
4
+
5
+ attr_accessor :load_source_class,
6
+ :dump_source_class,
7
+ :association_converters,
8
+ :use_lazy_loading
9
+
10
+ def initialize(ar_class, use_lazy_loading=true, **association_converters)
11
+ self.load_source_class = ar_class
12
+ self.dump_source_class = DefaultPoroClassBuilder.new(ar_class).()
13
+ self.association_converters = association_converters
14
+ self.use_lazy_loading = use_lazy_loading
15
+ end
16
+
17
+ def load(to_convert)
18
+ mapper.load(to_convert)
19
+ end
20
+
21
+ def dump(to_convert)
22
+ mapper.dump(to_convert)
23
+ end
24
+
25
+ def mapper
26
+ @mapper||= Yaoc::ObjectMapper.new(self.dump_source_class, self.load_source_class).tap do |mapper|
27
+ add_mapping_for_current_class(mapper)
28
+ add_mapping_for_associations(mapper)
29
+ end
30
+ end
31
+
32
+ protected
33
+
34
+ def add_mapping_for_current_class(mapper)
35
+ tmp_quirk = plain_attributes
36
+
37
+ mapper.add_mapping do
38
+ fetcher :public_send
39
+ rule to: tmp_quirk
40
+
41
+ rule to: :_set_metadata,
42
+ converter: ->(source, result){ fill_result_with_value(result, :_set_metadata_from_ar, source) },
43
+ reverse_converter: ->(source, result){
44
+
45
+ needs_conversion = if source.respond_to?("_needs_conversion?")
46
+ source._needs_conversion?
47
+ else
48
+ ! source.nil? #would trigger lazy loading when it is a ToProcDelegator
49
+ end
50
+
51
+ fill_result_with_value(result, :_set_metadata_to_ar, source._metadata.to_hash) if needs_conversion
52
+ }
53
+ end
54
+ end
55
+
56
+ def add_mapping_for_associations(mapper)
57
+ association_converters.each_pair do |association_name, association_converter|
58
+ map_collection = self.load_source_class.reflections[association_name].collection?
59
+
60
+ lazy_quirk = self.use_lazy_loading
61
+
62
+ mapper.add_mapping do
63
+ fetcher :public_send
64
+ rule to: association_name,
65
+ lazy_loading: lazy_quirk,
66
+ reverse_lazy_loading: false, #AR doesn't like ToProcDelegator
67
+ object_converter: association_converter.mapper,
68
+ is_collection: map_collection
69
+ end
70
+ end
71
+ end
72
+
73
+ def dump_source_class=(new_dump_source)
74
+ @dump_source_class = new_dump_source.tap do |source|
75
+ unless source.respond_to? :_metadata
76
+ source.send(:include, MetadataEnabled)
77
+ end
78
+ end
79
+ end
80
+
81
+ def load_source_class=(new_source)
82
+ @load_source_class=new_source.tap do |source|
83
+ unless source.respond_to? :_set_metadata_to_ar=
84
+ source.send(:include, ActiverecordToPoro::MetadataToAr)
85
+ end
86
+ end
87
+ end
88
+
89
+ def plain_attributes
90
+ columns(self.load_source_class) -
91
+ primary_keys(self.load_source_class) -
92
+ association_specific_columns(self.load_source_class) -
93
+ associated_object_accessors(self.load_source_class)
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,46 @@
1
+ module ActiverecordToPoro
2
+ module ColumnHelper
3
+ module_function
4
+ def columns(ar_class)
5
+ ar_class.column_names
6
+ end
7
+
8
+ def primary_keys(ar_class)
9
+ [ar_class.primary_key]
10
+ end
11
+
12
+ def association_specific_columns(ar_class)
13
+ ar_class.reflect_on_all_associations(:belongs_to).map(&:foreign_key)
14
+ end
15
+
16
+ def associated_object_accessors(ar_class)
17
+ ar_class.reflections.keys
18
+ end
19
+ end
20
+
21
+
22
+ class DefaultPoroClassBuilder
23
+ include ColumnHelper
24
+
25
+ attr_accessor :ar_class
26
+
27
+ def initialize(ar_class)
28
+ self.ar_class = ar_class
29
+ end
30
+
31
+ def call
32
+ create_class
33
+ end
34
+
35
+ protected
36
+
37
+ def create_class
38
+ @create_dump_source_class ||= Yaoc::Helper::StructHE(*(attributes_for_poro))
39
+ end
40
+
41
+ def attributes_for_poro
42
+ (columns(ar_class) - primary_keys(ar_class) - association_specific_columns(ar_class) + associated_object_accessors(ar_class)).map(&:to_sym)
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ module ActiverecordToPoro
2
+ class Metadata
3
+ attr_accessor :primary_key_column, :primary_key_value
4
+
5
+
6
+ def initialize_from_ar(ar_object=nil)
7
+ unless ar_object.nil?
8
+ set_primary_key(ar_object)
9
+ end
10
+ end
11
+
12
+ def set_primary_key(ar_object)
13
+ self.primary_key_column = ar_object.class.primary_key
14
+ self.primary_key_value = ar_object.send(self.primary_key_column)
15
+ end
16
+
17
+ def to_hash
18
+ { self.primary_key_column => self.primary_key_value }
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module ActiverecordToPoro
2
+ module MetadataEnabled
3
+ def _metadata
4
+ @_metadata ||= Metadata.new
5
+ end
6
+
7
+
8
+ def _set_metadata_from_ar=(ar_object)
9
+ _metadata.initialize_from_ar(ar_object)
10
+ end
11
+
12
+ end
13
+
14
+ module MetadataToAr
15
+ def _set_metadata_to_ar=(metadata)
16
+ metadata.each_pair do |attr, value|
17
+ self.send("#{attr}=", value)
18
+ @new_record = false
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module ActiverecordToPoro
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'yaoc'
2
+ require 'activerecord_to_poro/version'
3
+ require 'activerecord_to_poro/metadata_enabled'
4
+ require 'activerecord_to_poro/metadata'
5
+ require 'activerecord_to_poro/default_poro_class_builder'
6
+ require 'activerecord_to_poro/converter'
7
+
8
+ module ActiverecordToPoro
9
+
10
+ end
@@ -0,0 +1,53 @@
1
+ require 'acceptance_spec_helper'
2
+
3
+ feature 'Map active record associations', %q{
4
+ In order to use plain old ruby objects instead of active record objects
5
+ as a lib user
6
+ I want to map automatically associations
7
+ } do
8
+
9
+ given!(:mapper){
10
+ ActiverecordToPoro::Converter.new(a_active_record_class, roles: roles_converter, salutation: salutation_converter)
11
+ }
12
+
13
+ given!(:roles_converter){
14
+ ActiverecordToPoro::Converter.new(Role)
15
+ }
16
+
17
+ given!(:salutation_converter){
18
+ ActiverecordToPoro::Converter.new(Salutation)
19
+ }
20
+
21
+ given!(:a_active_record_class){
22
+ User
23
+ }
24
+
25
+ given!(:a_active_record_object){
26
+ a_active_record_class.create!(name: "my name", email: "my_name@example.com").tap do |user|
27
+ user.roles.create!(name: "admin")
28
+ user.roles.create!(name: "guest")
29
+ user.salutation = Salutation.create!(name: "Mister")
30
+
31
+ user.save!
32
+ end
33
+ }
34
+
35
+
36
+ scenario "creates a poro out of an ActiveRecord object with associations set" do
37
+ expect(mapper.load(a_active_record_object).roles.size).to eq 2
38
+ expect(mapper.load(a_active_record_object).salutation.name).to eq "Mister"
39
+ end
40
+
41
+ scenario "creates an ActiveRecord object from a poro object with associations set" do
42
+ poro = mapper.load(a_active_record_object)
43
+ expect(mapper.dump(poro).roles.size).to eq 2
44
+ end
45
+
46
+ scenario "lazy loads associated objects" do
47
+ expect(a_active_record_object).not_to receive :salutation
48
+ expect(a_active_record_object).not_to receive :roles
49
+
50
+ mapper.load(a_active_record_object)
51
+ end
52
+
53
+ end
@@ -0,0 +1,35 @@
1
+ require "acceptance_spec_helper"
2
+
3
+ feature "Map active record objects", %q{
4
+ In order to use plain old ruby objects instead of active record objects
5
+ as a lib user
6
+ I want to map from active record objects to plain old ruby objects and reverse
7
+ } do
8
+
9
+ given(:mapper){
10
+ ActiverecordToPoro::Converter.new(a_active_record_class)
11
+ }
12
+
13
+ given(:a_active_record_class){
14
+ User
15
+ }
16
+
17
+ given(:a_active_record_object){
18
+ a_active_record_class.create!(name: "my name", email: "my_name@example.com")
19
+ }
20
+
21
+ given(:expected_poro_class_attributes){
22
+ [:name, :email, :roles, :salutation]
23
+ }
24
+
25
+
26
+ scenario "creates a poro out of an ActiveRecord object" do
27
+ expect(mapper.load(a_active_record_object).members).to eq expected_poro_class_attributes
28
+ end
29
+
30
+ scenario "creates an ActiveRecord object from a poro object" do
31
+ poro = mapper.load(a_active_record_object)
32
+ expect(mapper.dump(poro)).to eq a_active_record_object
33
+ end
34
+
35
+ end
@@ -0,0 +1,2 @@
1
+ require 'spec_helper'
2
+ require 'ar_spec_helper'
@@ -0,0 +1,33 @@
1
+ require 'sqlite3'
2
+ require 'active_record'
3
+ require 'database_cleaner'
4
+
5
+ db_dir = File.join(File.expand_path(__dir__ ), "ar_support/db/")
6
+
7
+ ActiveRecord::Base.establish_connection({
8
+ pool: 5,
9
+ timeout: 5000,
10
+ database: ":memory:",
11
+ adapter: "sqlite3"
12
+ })
13
+
14
+ ActiveRecord::Migrator.migrate "#{db_dir}/migrate"
15
+
16
+ Dir[File.join(File.expand_path(__dir__ ), "ar_support/models/**/*.rb")].each { |f| require f }
17
+
18
+ RSpec.configure do |config|
19
+
20
+ config.before(:suite) do
21
+ DatabaseCleaner.strategy = :transaction
22
+ DatabaseCleaner.clean_with(:truncation)
23
+ end
24
+
25
+ config.before(:each) do
26
+ DatabaseCleaner.start
27
+ end
28
+
29
+ config.after(:each) do
30
+ DatabaseCleaner.clean
31
+ end
32
+
33
+ end
@@ -0,0 +1,18 @@
1
+ class CreateMyArClass < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name
5
+ t.string :email
6
+ t.integer :salutation_id
7
+ end
8
+
9
+ create_table :roles do |t|
10
+ t.string :name
11
+ t.integer :user_id
12
+ end
13
+
14
+ create_table :salutations do |t|
15
+ t.string :name
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ class Role < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,3 @@
1
+ class Salutation < ActiveRecord::Base
2
+ has_many :users
3
+ end
@@ -0,0 +1,8 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :roles
3
+ belongs_to :salutation
4
+
5
+ def zz
6
+ ActiveRecord::Base.new
7
+ end
8
+ end
@@ -0,0 +1,67 @@
1
+ require 'integration_spec_helper'
2
+
3
+ describe ActiverecordToPoro::Converter do
4
+ subject!{
5
+ ActiverecordToPoro::Converter.new(User, roles: roles_converter, salutation: salutation_converter)
6
+ }
7
+
8
+ let(:roles_converter){
9
+ ActiverecordToPoro::Converter.new(Role)
10
+ }
11
+
12
+ let(:salutation_converter){
13
+ ActiverecordToPoro::Converter.new(Salutation)
14
+ }
15
+
16
+ let(:ar_object){
17
+ User.create!(name: 'my name', email: 'my_name@example.com').tap do |user|
18
+ user.roles.create!(name: 'admin')
19
+ user.roles.create!(name: 'guest')
20
+ end
21
+ }
22
+
23
+ let!(:loaded_poro_object){
24
+ subject.load(ar_object)
25
+ }
26
+
27
+ describe '#load' do
28
+ it 'creates a poro' do
29
+ expect(subject.load(ar_object)).not_to be_kind_of ActiveRecord::Base
30
+ end
31
+
32
+ it 'sets metadata for loaded objects' do
33
+ expect(loaded_poro_object._metadata).not_to be_nil
34
+ expect(loaded_poro_object._metadata.primary_key_value).to eq ar_object.id
35
+ end
36
+
37
+ it 'converts also associated objects' do
38
+ expect(subject.load(ar_object).roles.size).to eq 2
39
+ end
40
+
41
+ it 'lazy loads associated objects' do
42
+ expect(ar_object).not_to receive :roles
43
+ subject.load(ar_object)
44
+ end
45
+
46
+ end
47
+
48
+ describe '#dump' do
49
+ it 'creates an ActiveRecordObject' do
50
+ expect(subject.dump(loaded_poro_object)).to be_kind_of ActiveRecord::Base
51
+ end
52
+
53
+ it 'sets the primary key when it existed before' do
54
+ expect(subject.dump(loaded_poro_object).id).to eq ar_object.id
55
+ end
56
+
57
+ it 'sets new_record to false when it is an existing record' do
58
+ expect(subject.dump(loaded_poro_object).new_record?).to be_falsy
59
+ end
60
+
61
+ it 'converts also associated objects' do
62
+ count_roles = ar_object.roles.size
63
+
64
+ expect(subject.dump(loaded_poro_object).roles.size).to eq count_roles
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,19 @@
1
+ require 'integration_spec_helper'
2
+
3
+ describe ActiverecordToPoro::DefaultPoroClassBuilder do
4
+ subject{
5
+ ActiverecordToPoro::DefaultPoroClassBuilder.new(User)
6
+ }
7
+
8
+ let(:expected_poro_class){
9
+ Yaoc::Helper::StructHE(:name, :email, :roles, :salutation)
10
+ }
11
+
12
+ describe "#call" do
13
+
14
+ it "creates a poro class for an ActiveRecord class" do
15
+ expect(subject.call.members).to eq expected_poro_class.members
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require 'spec_helper'
2
+ require 'ar_spec_helper'
@@ -0,0 +1,42 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:development)
3
+
4
+ require 'coveralls'
5
+ Coveralls.wear! unless ENV["SIMPLE_COVERAGE"]
6
+
7
+ begin
8
+ if ENV["SIMPLE_COVERAGE"]
9
+ require 'simplecov'
10
+ SimpleCov.start do
11
+ add_group "Lib", "lib"
12
+
13
+ add_filter "/spec/"
14
+ end
15
+ end
16
+ rescue LoadError
17
+ warn "=" * 80
18
+ warn 'simplecov not installed. No coverage report'
19
+ warn "=" * 80
20
+ end
21
+
22
+ require 'activerecord_to_poro'
23
+
24
+ Dir[File.join(File.expand_path(__dir__ ), "support/**/*.rb")].each { |f| require f }
25
+
26
+ # This file was generated by the `rspec --init` command. Conventionally, all
27
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
28
+ # Require this file using `require "spec_helper"` to ensure that it is only
29
+ # loaded once.
30
+ #
31
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
32
+ RSpec.configure do |config|
33
+ config.treat_symbols_as_metadata_keys_with_true_values = true
34
+ config.run_all_when_everything_filtered = true
35
+ config.filter_run :focus
36
+
37
+ # Run specs in random order to surface order dependencies. If you find an
38
+ # order dependency and want to debug it, you can fix the order by providing
39
+ # the seed, which is printed after each run.
40
+ # --seed 1234
41
+ config.order = 'random'
42
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # taken from https://github.com/jnicklas/capybara/blob/master/lib/capybara/rspec/features.rb
3
+ #
4
+ module Capybara
5
+ module Features
6
+ def self.included(base)
7
+ base.instance_eval do
8
+ alias :background :before
9
+ alias :scenario :it
10
+ alias :xscenario :xit
11
+ alias :given :let
12
+ alias :given! :let!
13
+ alias :feature :describe
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+
20
+ def self.feature(*args, &block)
21
+ options = if args.last.is_a?(Hash) then args.pop else {} end
22
+ options[:capybara_feature] = true
23
+ options[:type] = :feature
24
+ options[:caller] ||= caller
25
+ args.push(options)
26
+
27
+ describe(*args, &block)
28
+ end
29
+
30
+ RSpec.configuration.include Capybara::Features, :capybara_feature => true
@@ -0,0 +1,32 @@
1
+ require 'unit_spec_helper'
2
+
3
+ describe ActiverecordToPoro::Metadata do
4
+ let(:ar_object){
5
+ Struct.new(:id) do
6
+ def self.primary_key
7
+ "id"
8
+ end
9
+
10
+ end.new(1)
11
+ }
12
+
13
+ describe "#initialize_from_ar" do
14
+
15
+ it "sets the primary_key" do
16
+ subject.initialize_from_ar(ar_object)
17
+
18
+ expect(subject.primary_key_column).to eq "id"
19
+ expect(subject.primary_key_value).to eq 1
20
+ end
21
+
22
+ end
23
+
24
+ describe "set_primary_key" do
25
+ it "sets the primary_key" do
26
+ subject.set_primary_key(ar_object)
27
+
28
+ expect(subject.primary_key_column).to eq "id"
29
+ expect(subject.primary_key_value).to eq 1
30
+ end
31
+ end
32
+ end
@@ -0,0 +1 @@
1
+ require 'spec_helper'
metadata ADDED
@@ -0,0 +1,340 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_to_poro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dieter Späth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: abstract_type
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: yaoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 2.99.0.beta1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 2.99.0.beta1
125
+ - !ruby/object:Gem::Dependency
126
+ name: fuubar
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-remote
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: guard
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: guard-rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: guard-bundler
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: rb-fsevent
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: pry-stack_explorer
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ - !ruby/object:Gem::Dependency
238
+ name: pry-debugger
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - ">="
242
+ - !ruby/object:Gem::Version
243
+ version: '0'
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - ">="
249
+ - !ruby/object:Gem::Version
250
+ version: '0'
251
+ - !ruby/object:Gem::Dependency
252
+ name: equalizer
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - ">="
256
+ - !ruby/object:Gem::Version
257
+ version: '0'
258
+ type: :development
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - ">="
263
+ - !ruby/object:Gem::Version
264
+ version: '0'
265
+ description: Convert ActiveRecord objects to plain old ruby objects and vice versa
266
+ email:
267
+ - shad0wrunner@gmx.de
268
+ executables: []
269
+ extensions: []
270
+ extra_rdoc_files: []
271
+ files:
272
+ - ".gitignore"
273
+ - ".travis.yml"
274
+ - Gemfile
275
+ - Guardfile
276
+ - LICENSE.txt
277
+ - README.md
278
+ - Rakefile
279
+ - activerecord_to_poro.gemspec
280
+ - lib/activerecord_to_poro.rb
281
+ - lib/activerecord_to_poro/converter.rb
282
+ - lib/activerecord_to_poro/default_poro_class_builder.rb
283
+ - lib/activerecord_to_poro/metadata.rb
284
+ - lib/activerecord_to_poro/metadata_enabled.rb
285
+ - lib/activerecord_to_poro/version.rb
286
+ - spec/acceptance/map_ar_associations_spec.rb
287
+ - spec/acceptance/map_ar_objects_spec.rb
288
+ - spec/acceptance_spec_helper.rb
289
+ - spec/ar_spec_helper.rb
290
+ - spec/ar_support/db/migrate/20140127093447_create_my_ar_class.rb
291
+ - spec/ar_support/models/role.rb
292
+ - spec/ar_support/models/salutation.rb
293
+ - spec/ar_support/models/user.rb
294
+ - spec/integration/lib/activerecord_to_poro/converter_spec.rb
295
+ - spec/integration/lib/activerecord_to_poro/default_poro_class_builder_spec.rb
296
+ - spec/integration_spec_helper.rb
297
+ - spec/spec_helper.rb
298
+ - spec/support/feature.rb
299
+ - spec/unit/lib/activerecord_to_poro/metadata_spec.rb
300
+ - spec/unit_spec_helper.rb
301
+ homepage: ''
302
+ licenses:
303
+ - MIT
304
+ metadata: {}
305
+ post_install_message:
306
+ rdoc_options: []
307
+ require_paths:
308
+ - lib
309
+ required_ruby_version: !ruby/object:Gem::Requirement
310
+ requirements:
311
+ - - ">="
312
+ - !ruby/object:Gem::Version
313
+ version: 2.0.0
314
+ required_rubygems_version: !ruby/object:Gem::Requirement
315
+ requirements:
316
+ - - ">="
317
+ - !ruby/object:Gem::Version
318
+ version: '0'
319
+ requirements: []
320
+ rubyforge_project:
321
+ rubygems_version: 2.2.1
322
+ signing_key:
323
+ specification_version: 4
324
+ summary: Convert ActiveRecord objects to plain old ruby objects and vice versa
325
+ test_files:
326
+ - spec/acceptance/map_ar_associations_spec.rb
327
+ - spec/acceptance/map_ar_objects_spec.rb
328
+ - spec/acceptance_spec_helper.rb
329
+ - spec/ar_spec_helper.rb
330
+ - spec/ar_support/db/migrate/20140127093447_create_my_ar_class.rb
331
+ - spec/ar_support/models/role.rb
332
+ - spec/ar_support/models/salutation.rb
333
+ - spec/ar_support/models/user.rb
334
+ - spec/integration/lib/activerecord_to_poro/converter_spec.rb
335
+ - spec/integration/lib/activerecord_to_poro/default_poro_class_builder_spec.rb
336
+ - spec/integration_spec_helper.rb
337
+ - spec/spec_helper.rb
338
+ - spec/support/feature.rb
339
+ - spec/unit/lib/activerecord_to_poro/metadata_spec.rb
340
+ - spec/unit_spec_helper.rb