mongomodel 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,7 @@ module MongoModel
20
20
  autoload :Translation, 'mongomodel/concerns/translation'
21
21
  autoload :Validations, 'mongomodel/concerns/validations'
22
22
  autoload :Callbacks, 'mongomodel/concerns/callbacks'
23
+ autoload :Serialization, 'mongomodel/concerns/serialization'
23
24
  autoload :Logging, 'mongomodel/concerns/logging'
24
25
  autoload :Timestamps, 'mongomodel/concerns/timestamps'
25
26
  autoload :PrettyInspect, 'mongomodel/concerns/pretty_inspect'
@@ -20,6 +20,10 @@ module MongoModel
20
20
  def model_properties
21
21
  properties.reject { |k, p| p.internal? }
22
22
  end
23
+
24
+ def internal_properties
25
+ properties.select { |k, p| p.internal? }.map { |k, p| p }
26
+ end
23
27
  end
24
28
 
25
29
  class Property
@@ -0,0 +1,33 @@
1
+ module MongoModel
2
+ module Serialization
3
+ extend ActiveSupport::Concern
4
+
5
+ include ActiveModel::Serializers::JSON
6
+
7
+ def serializable_hash(options = nil)
8
+ options ||= {}
9
+
10
+ options[:only] = Array.wrap(options[:only]).map { |n| n.to_s }
11
+ options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }
12
+
13
+ attribute_names = attributes.keys.map { |k| k.to_s }.sort
14
+ attribute_names -= self.class.internal_properties.map { |p| p.name.to_s }
15
+
16
+ if options[:only].any?
17
+ attribute_names &= options[:only]
18
+ elsif options[:except].any?
19
+ attribute_names -= options[:except]
20
+ end
21
+
22
+ method_names = Array.wrap(options[:methods]).inject([]) do |methods, name|
23
+ methods << name if respond_to?(name.to_s)
24
+ methods
25
+ end
26
+
27
+ (attribute_names + method_names).inject({}) { |hash, name|
28
+ hash[name] = send(name) if respond_to?(name)
29
+ hash
30
+ }
31
+ end
32
+ end
33
+ end
@@ -29,6 +29,7 @@ module MongoModel
29
29
  include Logging
30
30
  include RecordStatus
31
31
  include ActiveModelCompatibility
32
+ include Serialization
32
33
  include Timestamps
33
34
  include PrettyInspect
34
35
  include AbstractClass
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -54,7 +54,7 @@ module MongoModel
54
54
 
55
55
  describe "#model_name" do
56
56
  it "should return an ActiveModel::Name object" do
57
- TestModel.model_name.should == ActiveModel::Name.new('TestModel')
57
+ TestModel.model_name.should == ActiveModel::Name.new(mock('TestModel class', :name => 'TestModel'))
58
58
  end
59
59
  end
60
60
  end
@@ -25,5 +25,10 @@ module MongoModel
25
25
  :skill => Properties::Property.new(:skill, String)
26
26
  })
27
27
  end
28
+
29
+ it "should have a set of internal property names" do
30
+ Person.internal_properties.should include(Person.properties[:type])
31
+ Person.internal_properties.should include(Person.properties[:id]) if described_class == Document
32
+ end
28
33
  end
29
34
  end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ module MongoModel
4
+ specs_for(Document, EmbeddedDocument) do
5
+ define_class(:TestModel, described_class) do
6
+ property :name, String
7
+ property :age, Integer
8
+ property :paid, Boolean
9
+ property :prefs, Hash
10
+ property :internal, String, :internal => true
11
+
12
+ def hello
13
+ "Hi friend!"
14
+ end
15
+ end
16
+
17
+ let(:instance) do
18
+ TestModel.new(:name => 'Hello World', :age => 25, :paid => true, :prefs => { :foo => 'bar' }, :internal => 'hideme')
19
+ end
20
+
21
+ it "should include root in json" do
22
+ begin
23
+ TestModel.include_root_in_json = true
24
+
25
+ json = instance.to_json
26
+ json.should match(/^\{"test_model":\{/)
27
+ ensure
28
+ TestModel.include_root_in_json = false
29
+ end
30
+ end
31
+
32
+ it "should encode all public attributes" do
33
+ json = instance.to_json
34
+ json.should match(/"name":"Hello World"/)
35
+ json.should match(/"age":25/)
36
+ json.should match(/"paid":true/)
37
+ json.should match(/"prefs":\{"foo":"bar"\}/)
38
+ end
39
+
40
+ it "should not encode internal attributes" do
41
+ json = instance.to_json
42
+ json.should_not match(/"internal":"hideme"/)
43
+ end
44
+
45
+ it "should allow attribute filtering with only" do
46
+ json = instance.to_json(:only => [:name, :age])
47
+ json.should match(/"name":"Hello World"/)
48
+ json.should match(/"age":25/)
49
+ json.should_not match(/"paid":true/)
50
+ json.should_not match(/"prefs":\{"foo":"bar"\}/)
51
+ end
52
+
53
+ it "should allow attribute filtering with except" do
54
+ json = instance.to_json(:except => [:name, :age])
55
+ json.should_not match(/"name":"Hello World"/)
56
+ json.should_not match(/"age":25/)
57
+ json.should match(/"paid":true/)
58
+ json.should match(/"prefs":\{"foo":"bar"\}/)
59
+ end
60
+
61
+ it "should allow methods to be included" do
62
+ json = instance.to_json(:methods => [:hello, :type])
63
+ json.should match(/"hello":"Hi friend!"/)
64
+ json.should match(/"type":"TestModel"/)
65
+ end
66
+ end
67
+ end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'active_support/time'
2
3
 
3
4
  module MongoModel
4
5
  specs_for(Document, EmbeddedDocument) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Pohlenz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-10 00:00:00 +10:30
12
+ date: 2010-02-16 00:00:00 +10:30
13
13
  default_executable: console
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -82,6 +82,7 @@ files:
82
82
  - lib/mongomodel/concerns/pretty_inspect.rb
83
83
  - lib/mongomodel/concerns/properties.rb
84
84
  - lib/mongomodel/concerns/record_status.rb
85
+ - lib/mongomodel/concerns/serialization.rb
85
86
  - lib/mongomodel/concerns/timestamps.rb
86
87
  - lib/mongomodel/concerns/translation.rb
87
88
  - lib/mongomodel/concerns/validations.rb
@@ -132,6 +133,7 @@ files:
132
133
  - spec/mongomodel/concerns/logging_spec.rb
133
134
  - spec/mongomodel/concerns/pretty_inspect_spec.rb
134
135
  - spec/mongomodel/concerns/properties_spec.rb
136
+ - spec/mongomodel/concerns/serialization/json_serialization_spec.rb
135
137
  - spec/mongomodel/concerns/timestamps_spec.rb
136
138
  - spec/mongomodel/concerns/validations_spec.rb
137
139
  - spec/mongomodel/document/callbacks_spec.rb
@@ -205,6 +207,7 @@ test_files:
205
207
  - spec/mongomodel/concerns/logging_spec.rb
206
208
  - spec/mongomodel/concerns/pretty_inspect_spec.rb
207
209
  - spec/mongomodel/concerns/properties_spec.rb
210
+ - spec/mongomodel/concerns/serialization/json_serialization_spec.rb
208
211
  - spec/mongomodel/concerns/timestamps_spec.rb
209
212
  - spec/mongomodel/concerns/validations_spec.rb
210
213
  - spec/mongomodel/document/callbacks_spec.rb