mm_to_view_model 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/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.1.0)
5
+ activesupport (= 3.1.0)
6
+ bcrypt-ruby (~> 3.0.0)
7
+ builder (~> 3.0.0)
8
+ i18n (~> 0.6)
9
+ activesupport (3.1.0)
10
+ multi_json (~> 1.0)
11
+ bcrypt-ruby (3.0.1)
12
+ bcrypt-ruby (3.0.1-java)
13
+ bson (1.4.0)
14
+ bson (1.4.0-java)
15
+ builder (3.0.0)
16
+ diff-lcs (1.1.3)
17
+ fuubar (0.0.6)
18
+ rspec (~> 2.0)
19
+ rspec-instafail (~> 0.1.8)
20
+ ruby-progressbar (~> 0.0.10)
21
+ i18n (0.6.0)
22
+ mocha (0.9.12)
23
+ mongo (1.4.0)
24
+ bson (= 1.4.0)
25
+ mongo_mapper (0.9.2)
26
+ activemodel (~> 3.0)
27
+ activesupport (~> 3.0)
28
+ plucky (~> 0.3.8)
29
+ multi_json (1.0.3)
30
+ plucky (0.3.8)
31
+ mongo (~> 1.3)
32
+ rspec (2.6.0)
33
+ rspec-core (~> 2.6.0)
34
+ rspec-expectations (~> 2.6.0)
35
+ rspec-mocks (~> 2.6.0)
36
+ rspec-core (2.6.4)
37
+ rspec-expectations (2.6.0)
38
+ diff-lcs (~> 1.1.2)
39
+ rspec-instafail (0.1.8)
40
+ rspec-mocks (2.6.0)
41
+ ruby-progressbar (0.0.10)
42
+ watchr (0.7)
43
+
44
+ PLATFORMS
45
+ java
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ fuubar
50
+ mocha
51
+ mongo_mapper
52
+ rspec
53
+ watchr
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Scott Lewis, c00lryguy@gmail.com
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/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/watchr ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if $0 == __FILE__
4
+ puts "Watchr started!"
5
+ system("watchr #{__FILE__}")
6
+ else
7
+ # --------------------------------------------------
8
+ # Convenience Methods
9
+ # --------------------------------------------------
10
+
11
+ def run(files_to_run)
12
+ command = "bundle exec rspec #{files_to_run}"
13
+ puts "Running: #{command}"
14
+ system command
15
+ end
16
+
17
+ def run_spec_matching(matchdata)
18
+ run "spec/#{matchdata[1]}_spec.rb"
19
+ end
20
+
21
+ def run_all_specs
22
+ run 'spec'
23
+ end
24
+
25
+ # --------------------------------------------------
26
+ # Watchr Rules
27
+ # --------------------------------------------------
28
+
29
+ watch '^lib/(.*)\.rb' do |matchdata|
30
+ run_spec_matching(matchdata)
31
+ end
32
+
33
+ watch '^spec/(.*)_spec\.rb' do |matchdata|
34
+ run_spec_matching(matchdata)
35
+ end
36
+
37
+ watch '^spec/spec_helper\.rb' do
38
+ run_all_specs
39
+ end
40
+
41
+ watch '^spec/support/.*\.rb' do
42
+ run_all_specs
43
+ end
44
+ end
@@ -0,0 +1,51 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default)
3
+ # require 'active_support/core_ext/string'
4
+
5
+ module MongoMapper
6
+ module Plugins
7
+ module ToViewModel
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def to_view_model
12
+ fields = keys.inject({}) do |memo, (k, v)|
13
+ memo[k.to_s] = if v.type == ObjectId
14
+ nil
15
+ else
16
+ v.type.new rescue nil
17
+ end
18
+ memo
19
+ end
20
+
21
+ relations = associations.inject({}) do |memo, (k, v)|
22
+ memo[k.to_s] = [v.klass.to_view_model] if ['OneAssociation', 'ManyAssociation'].include?(v.class.to_s.split(/::/).last)
23
+ memo
24
+ end
25
+
26
+ fields.merge(relations)
27
+ end
28
+ end
29
+
30
+ module InstanceMethods
31
+ def to_view_model
32
+ fields = keys.inject({}) do |memo, (k, v)|
33
+ memo[k.to_s] = send(k)
34
+ memo
35
+ end
36
+
37
+ relations = associations.inject({}) do |memo, (k, v)|
38
+ begin
39
+ memo[k.to_s] = send(k).collect(&:to_view_model)
40
+ rescue
41
+ # Don't add to hash
42
+ end
43
+ memo
44
+ end
45
+
46
+ fields.merge(relations)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,9 @@
1
+ require 'pathname'
2
+ __DIR__ = Pathname.new(__FILE__).dirname.expand_path
3
+ __LIB__ = __DIR__.join('..', 'lib').expand_path
4
+ $:.unshift(__LIB__) unless $:.include?(__LIB__)
5
+
6
+ require 'mm_to_view_model'
7
+
8
+ MongoMapper::Document.plugin(MongoMapper::Plugins::ToViewModel)
9
+ MongoMapper::EmbeddedDocument.plugin(MongoMapper::Plugins::ToViewModel)
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe "When required" do
4
+ it "should add the ToViewModel plugin to all MongoMapper Documents and EmbeddedDocuments" do
5
+ require 'mm_to_view_model/import'
6
+
7
+ MongoMapper::Document.expects(:plugin).with(MongoMapper::Plugins::ToViewModel)
8
+ MongoMapper::EmbeddedDocument.expects(:plugin).with(MongoMapper::Plugins::ToViewModel)
9
+ end
10
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ class User
4
+ include MongoMapper::Document
5
+ plugin MongoMapper::Plugins::ToViewModel
6
+
7
+ key :name, String
8
+ key :hobbies, Array
9
+
10
+ many :posts
11
+ end
12
+
13
+ class Post
14
+ include MongoMapper::Document
15
+ plugin MongoMapper::Plugins::ToViewModel
16
+
17
+ key :title, String
18
+ key :body, String
19
+
20
+ belongs_to :user
21
+ end
22
+
23
+
24
+ describe User do
25
+ describe ".to_view_model" do
26
+ subject { User }
27
+
28
+ let(:result) do
29
+ {
30
+ "_id" => nil,
31
+ "name" => "",
32
+ "hobbies" => [],
33
+ "posts" => [
34
+ { "_id" => nil, "title" => "", "body" => "" }
35
+ ]
36
+ }
37
+ end
38
+
39
+ it { subject.to_view_model.should == result }
40
+ end
41
+
42
+ describe "#to_view_model" do
43
+ let(:first_post) do
44
+ Post.new("title" => "Some new cool thing", "body" => "This new cool thing rocks!")
45
+ end
46
+
47
+ let(:second_post) do
48
+ Post.new("title" => "News about Paris", "body" => "They remade the eiffel tower out of cheese!")
49
+ end
50
+
51
+ subject do
52
+ User.new(
53
+ "name" => "SomeBrodi12",
54
+ "posts" => [first_post, second_post]
55
+ )
56
+ end
57
+
58
+ let(:result) do
59
+ {
60
+ "_id" => subject.id,
61
+ "name" => "SomeBrodi12",
62
+ "hobbies" => [],
63
+ "posts" => [
64
+ { "_id" => first_post.id, "title" => "Some new cool thing", "body" => "This new cool thing rocks!", "user_id" => subject.id },
65
+ { "_id" => second_post.id, "title" => "News about Paris", "body" => "They remade the eiffel tower out of cheese!", "user_id" => subject.id },
66
+ ]
67
+ }
68
+ end
69
+
70
+ it { subject.to_view_model.should == result }
71
+ end
72
+ end
@@ -0,0 +1,13 @@
1
+ require 'mm_to_view_model'
2
+ Bundler.require(:development)
3
+
4
+ MongoMapper.database = "mm_to_view_model"
5
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
6
+
7
+ RSpec.configure do |config|
8
+ config.before(:each) do
9
+ MongoMapper.database.collections.each { |collection| collection.remove }.collect(&:name)
10
+ system("clear")
11
+ end
12
+ end
13
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mm_to_view_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Scott Lewis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongo_mapper
16
+ requirement: &70194076731660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70194076731660
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70194076731120 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.6.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70194076731120
36
+ - !ruby/object:Gem::Dependency
37
+ name: mocha
38
+ requirement: &70194076730540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.10.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70194076730540
47
+ - !ruby/object:Gem::Dependency
48
+ name: fuubar
49
+ requirement: &70194076730060 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.6
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70194076730060
58
+ - !ruby/object:Gem::Dependency
59
+ name: watchr
60
+ requirement: &70194076729580 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.7.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70194076729580
69
+ description: ! '`mm_to_view_model` is a `MongoMapper` plugin that adds a `to_view_model`
70
+ class and instance method.'
71
+ email: c00lryguy@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE
76
+ - VERSION
77
+ files:
78
+ - LICENSE
79
+ - VERSION
80
+ - Gemfile.lock
81
+ - lib/mm_to_view_model/import.rb
82
+ - lib/mm_to_view_model.rb
83
+ - spec/mm_to_view_model/import_spec.rb
84
+ - spec/mm_to_view_model_spec.rb
85
+ - spec/spec_helper.rb
86
+ - bin/watchr
87
+ homepage: http://github.com/c00lryguy/mm_to_view_model
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.6
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Easily output your models in a view-model format.
111
+ test_files:
112
+ - bin/watchr
113
+ - spec/mm_to_view_model/import_spec.rb
114
+ - spec/mm_to_view_model_spec.rb
115
+ - spec/spec_helper.rb