humanoid 1.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.watchr +29 -0
- data/HISTORY +342 -0
- data/MIT_LICENSE +20 -0
- data/README.rdoc +56 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/caliper.yml +4 -0
- data/humanoid.gemspec +374 -0
- data/lib/humanoid.rb +111 -0
- data/lib/humanoid/associations.rb +258 -0
- data/lib/humanoid/associations/belongs_to.rb +64 -0
- data/lib/humanoid/associations/belongs_to_related.rb +62 -0
- data/lib/humanoid/associations/has_many.rb +180 -0
- data/lib/humanoid/associations/has_many_related.rb +109 -0
- data/lib/humanoid/associations/has_one.rb +95 -0
- data/lib/humanoid/associations/has_one_related.rb +81 -0
- data/lib/humanoid/associations/options.rb +57 -0
- data/lib/humanoid/associations/proxy.rb +31 -0
- data/lib/humanoid/attributes.rb +184 -0
- data/lib/humanoid/callbacks.rb +23 -0
- data/lib/humanoid/collection.rb +118 -0
- data/lib/humanoid/collections/cyclic_iterator.rb +34 -0
- data/lib/humanoid/collections/master.rb +28 -0
- data/lib/humanoid/collections/mimic.rb +46 -0
- data/lib/humanoid/collections/operations.rb +41 -0
- data/lib/humanoid/collections/slaves.rb +44 -0
- data/lib/humanoid/commands.rb +182 -0
- data/lib/humanoid/commands/create.rb +21 -0
- data/lib/humanoid/commands/delete.rb +16 -0
- data/lib/humanoid/commands/delete_all.rb +23 -0
- data/lib/humanoid/commands/deletion.rb +18 -0
- data/lib/humanoid/commands/destroy.rb +19 -0
- data/lib/humanoid/commands/destroy_all.rb +23 -0
- data/lib/humanoid/commands/save.rb +27 -0
- data/lib/humanoid/components.rb +24 -0
- data/lib/humanoid/config.rb +84 -0
- data/lib/humanoid/contexts.rb +25 -0
- data/lib/humanoid/contexts/enumerable.rb +117 -0
- data/lib/humanoid/contexts/ids.rb +25 -0
- data/lib/humanoid/contexts/mongo.rb +224 -0
- data/lib/humanoid/contexts/paging.rb +42 -0
- data/lib/humanoid/criteria.rb +259 -0
- data/lib/humanoid/criterion/complex.rb +21 -0
- data/lib/humanoid/criterion/exclusion.rb +65 -0
- data/lib/humanoid/criterion/inclusion.rb +91 -0
- data/lib/humanoid/criterion/optional.rb +128 -0
- data/lib/humanoid/cursor.rb +82 -0
- data/lib/humanoid/document.rb +300 -0
- data/lib/humanoid/enslavement.rb +38 -0
- data/lib/humanoid/errors.rb +77 -0
- data/lib/humanoid/extensions.rb +84 -0
- data/lib/humanoid/extensions/array/accessors.rb +17 -0
- data/lib/humanoid/extensions/array/aliasing.rb +4 -0
- data/lib/humanoid/extensions/array/assimilation.rb +26 -0
- data/lib/humanoid/extensions/array/conversions.rb +29 -0
- data/lib/humanoid/extensions/array/parentization.rb +13 -0
- data/lib/humanoid/extensions/boolean/conversions.rb +16 -0
- data/lib/humanoid/extensions/date/conversions.rb +15 -0
- data/lib/humanoid/extensions/datetime/conversions.rb +17 -0
- data/lib/humanoid/extensions/float/conversions.rb +16 -0
- data/lib/humanoid/extensions/hash/accessors.rb +38 -0
- data/lib/humanoid/extensions/hash/assimilation.rb +30 -0
- data/lib/humanoid/extensions/hash/conversions.rb +15 -0
- data/lib/humanoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/humanoid/extensions/hash/scoping.rb +12 -0
- data/lib/humanoid/extensions/integer/conversions.rb +16 -0
- data/lib/humanoid/extensions/nil/assimilation.rb +13 -0
- data/lib/humanoid/extensions/object/conversions.rb +33 -0
- data/lib/humanoid/extensions/proc/scoping.rb +12 -0
- data/lib/humanoid/extensions/string/conversions.rb +15 -0
- data/lib/humanoid/extensions/string/inflections.rb +97 -0
- data/lib/humanoid/extensions/symbol/inflections.rb +36 -0
- data/lib/humanoid/extensions/time/conversions.rb +18 -0
- data/lib/humanoid/factory.rb +19 -0
- data/lib/humanoid/field.rb +39 -0
- data/lib/humanoid/fields.rb +62 -0
- data/lib/humanoid/finders.rb +224 -0
- data/lib/humanoid/identity.rb +39 -0
- data/lib/humanoid/indexes.rb +30 -0
- data/lib/humanoid/matchers.rb +36 -0
- data/lib/humanoid/matchers/all.rb +11 -0
- data/lib/humanoid/matchers/default.rb +26 -0
- data/lib/humanoid/matchers/exists.rb +13 -0
- data/lib/humanoid/matchers/gt.rb +11 -0
- data/lib/humanoid/matchers/gte.rb +11 -0
- data/lib/humanoid/matchers/in.rb +11 -0
- data/lib/humanoid/matchers/lt.rb +11 -0
- data/lib/humanoid/matchers/lte.rb +11 -0
- data/lib/humanoid/matchers/ne.rb +11 -0
- data/lib/humanoid/matchers/nin.rb +11 -0
- data/lib/humanoid/matchers/size.rb +11 -0
- data/lib/humanoid/memoization.rb +27 -0
- data/lib/humanoid/named_scope.rb +40 -0
- data/lib/humanoid/scope.rb +75 -0
- data/lib/humanoid/timestamps.rb +30 -0
- data/lib/humanoid/versioning.rb +28 -0
- data/perf/benchmark.rb +77 -0
- data/spec/integration/humanoid/associations_spec.rb +301 -0
- data/spec/integration/humanoid/attributes_spec.rb +22 -0
- data/spec/integration/humanoid/commands_spec.rb +216 -0
- data/spec/integration/humanoid/contexts/enumerable_spec.rb +33 -0
- data/spec/integration/humanoid/criteria_spec.rb +224 -0
- data/spec/integration/humanoid/document_spec.rb +587 -0
- data/spec/integration/humanoid/extensions_spec.rb +26 -0
- data/spec/integration/humanoid/finders_spec.rb +119 -0
- data/spec/integration/humanoid/inheritance_spec.rb +137 -0
- data/spec/integration/humanoid/named_scope_spec.rb +46 -0
- data/spec/models/address.rb +39 -0
- data/spec/models/animal.rb +6 -0
- data/spec/models/comment.rb +8 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/game.rb +6 -0
- data/spec/models/inheritance.rb +56 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/patient.rb +4 -0
- data/spec/models/person.rb +98 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/post.rb +15 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +141 -0
- data/spec/unit/mongoid/associations/belongs_to_spec.rb +193 -0
- data/spec/unit/mongoid/associations/has_many_related_spec.rb +387 -0
- data/spec/unit/mongoid/associations/has_many_spec.rb +471 -0
- data/spec/unit/mongoid/associations/has_one_related_spec.rb +179 -0
- data/spec/unit/mongoid/associations/has_one_spec.rb +282 -0
- data/spec/unit/mongoid/associations/options_spec.rb +191 -0
- data/spec/unit/mongoid/associations_spec.rb +545 -0
- data/spec/unit/mongoid/attributes_spec.rb +484 -0
- data/spec/unit/mongoid/callbacks_spec.rb +55 -0
- data/spec/unit/mongoid/collection_spec.rb +171 -0
- data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
- data/spec/unit/mongoid/collections/master_spec.rb +41 -0
- data/spec/unit/mongoid/collections/mimic_spec.rb +43 -0
- data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
- data/spec/unit/mongoid/commands/create_spec.rb +30 -0
- data/spec/unit/mongoid/commands/delete_all_spec.rb +58 -0
- data/spec/unit/mongoid/commands/delete_spec.rb +35 -0
- data/spec/unit/mongoid/commands/destroy_all_spec.rb +23 -0
- data/spec/unit/mongoid/commands/destroy_spec.rb +44 -0
- data/spec/unit/mongoid/commands/save_spec.rb +105 -0
- data/spec/unit/mongoid/commands_spec.rb +282 -0
- data/spec/unit/mongoid/config_spec.rb +165 -0
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +374 -0
- data/spec/unit/mongoid/contexts/mongo_spec.rb +505 -0
- data/spec/unit/mongoid/contexts_spec.rb +25 -0
- data/spec/unit/mongoid/criteria_spec.rb +769 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +19 -0
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +91 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +211 -0
- data/spec/unit/mongoid/criterion/optional_spec.rb +329 -0
- data/spec/unit/mongoid/cursor_spec.rb +74 -0
- data/spec/unit/mongoid/document_spec.rb +986 -0
- data/spec/unit/mongoid/enslavement_spec.rb +63 -0
- data/spec/unit/mongoid/errors_spec.rb +103 -0
- data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
- data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +102 -0
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +70 -0
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
- data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +46 -0
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +21 -0
- data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +43 -0
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +91 -0
- data/spec/unit/mongoid/extensions/time/conversions_spec.rb +70 -0
- data/spec/unit/mongoid/factory_spec.rb +31 -0
- data/spec/unit/mongoid/field_spec.rb +81 -0
- data/spec/unit/mongoid/fields_spec.rb +158 -0
- data/spec/unit/mongoid/finders_spec.rb +368 -0
- data/spec/unit/mongoid/identity_spec.rb +88 -0
- data/spec/unit/mongoid/indexes_spec.rb +93 -0
- data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
- data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
- data/spec/unit/mongoid/matchers_spec.rb +329 -0
- data/spec/unit/mongoid/memoization_spec.rb +75 -0
- data/spec/unit/mongoid/named_scope_spec.rb +123 -0
- data/spec/unit/mongoid/scope_spec.rb +240 -0
- data/spec/unit/mongoid/timestamps_spec.rb +25 -0
- data/spec/unit/mongoid/versioning_spec.rb +41 -0
- data/spec/unit/mongoid_spec.rb +37 -0
- metadata +431 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Enslavement #:nodoc:
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
class_inheritable_accessor :enslaved
|
8
|
+
self.enslaved = false
|
9
|
+
|
10
|
+
delegate :enslaved?, :to => "self.class"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods #:nodoc
|
15
|
+
# Set whether or not this documents read operations should delegate to
|
16
|
+
# the slave database by default.
|
17
|
+
#
|
18
|
+
# Example:
|
19
|
+
#
|
20
|
+
# class Person
|
21
|
+
# include Humanoid::Document
|
22
|
+
# enslave
|
23
|
+
# end
|
24
|
+
def enslave
|
25
|
+
self.enslaved = true
|
26
|
+
end
|
27
|
+
|
28
|
+
# Determines if the class is enslaved or not.
|
29
|
+
#
|
30
|
+
# Returns:
|
31
|
+
#
|
32
|
+
# True if enslaved, false if not.
|
33
|
+
def enslaved?
|
34
|
+
self.enslaved == true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc
|
3
|
+
module Errors #:nodoc
|
4
|
+
|
5
|
+
# Raised when querying the database for a document by a specific id which
|
6
|
+
# does not exist. If multiple ids were passed then it will display all of
|
7
|
+
# those.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
#
|
11
|
+
# <tt>DocumentNotFound.new(Person, ["1", "2"])</tt>
|
12
|
+
class DocumentNotFound < RuntimeError
|
13
|
+
def initialize(klass, ids)
|
14
|
+
@klass, @identifier = klass, ids.is_a?(Array) ? ids.join(", ") : ids
|
15
|
+
end
|
16
|
+
def message
|
17
|
+
"Document not found for class #{@klass} and id(s) #{@identifier}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Raised when invalid options are passed into a constructor or method.
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
#
|
25
|
+
# <tt>InvalidOptions.new</tt>
|
26
|
+
class InvalidOptions < RuntimeError; end
|
27
|
+
|
28
|
+
# Raised when the database connection has not been set up properly, either
|
29
|
+
# by attempting to set an object on the db that is not a +Mongo::DB+, or
|
30
|
+
# not setting anything at all.
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
#
|
34
|
+
# <tt>InvalidDatabase.new("Not a DB")</tt>
|
35
|
+
class InvalidDatabase < RuntimeError
|
36
|
+
def initialize(database)
|
37
|
+
@database = database
|
38
|
+
end
|
39
|
+
def message
|
40
|
+
"Database should be a Mongo::DB, not #{@database.class.name}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Raised when a persisence method ending in ! fails validation. The message
|
45
|
+
# will contain the full error messages from the +Document+ in question.
|
46
|
+
#
|
47
|
+
# Example:
|
48
|
+
#
|
49
|
+
# <tt>Validations.new(person.errors)</tt>
|
50
|
+
class Validations < RuntimeError
|
51
|
+
def initialize(errors)
|
52
|
+
@errors = errors
|
53
|
+
end
|
54
|
+
def message
|
55
|
+
"Validation failed: #{@errors.full_messages}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# This error is raised when trying to access a Mongo::Collection from an
|
60
|
+
# embedded document.
|
61
|
+
#
|
62
|
+
# Example:
|
63
|
+
#
|
64
|
+
# <tt>InvalidCollection.new(Address)</tt>
|
65
|
+
class InvalidCollection < RuntimeError
|
66
|
+
def initialize(klass)
|
67
|
+
@klass = klass
|
68
|
+
end
|
69
|
+
def message
|
70
|
+
"Access to the collection for #{@klass.name} is not allowed " +
|
71
|
+
"since it is an embedded document, please access a collection from " +
|
72
|
+
"the root document"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "humanoid/extensions/array/accessors"
|
3
|
+
require "humanoid/extensions/array/aliasing"
|
4
|
+
require "humanoid/extensions/array/assimilation"
|
5
|
+
require "humanoid/extensions/array/conversions"
|
6
|
+
require "humanoid/extensions/array/parentization"
|
7
|
+
require "humanoid/extensions/boolean/conversions"
|
8
|
+
require "humanoid/extensions/date/conversions"
|
9
|
+
require "humanoid/extensions/datetime/conversions"
|
10
|
+
require "humanoid/extensions/float/conversions"
|
11
|
+
require "humanoid/extensions/hash/accessors"
|
12
|
+
require "humanoid/extensions/hash/assimilation"
|
13
|
+
require "humanoid/extensions/hash/conversions"
|
14
|
+
require "humanoid/extensions/hash/criteria_helpers"
|
15
|
+
require "humanoid/extensions/hash/scoping"
|
16
|
+
require "humanoid/extensions/integer/conversions"
|
17
|
+
require "humanoid/extensions/nil/assimilation"
|
18
|
+
require "humanoid/extensions/object/conversions"
|
19
|
+
require "humanoid/extensions/proc/scoping"
|
20
|
+
require "humanoid/extensions/string/conversions"
|
21
|
+
require "humanoid/extensions/string/inflections"
|
22
|
+
require "humanoid/extensions/symbol/inflections"
|
23
|
+
require "humanoid/extensions/time/conversions"
|
24
|
+
|
25
|
+
class Array #:nodoc
|
26
|
+
include Humanoid::Extensions::Array::Accessors
|
27
|
+
include Humanoid::Extensions::Array::Assimilation
|
28
|
+
include Humanoid::Extensions::Array::Conversions
|
29
|
+
include Humanoid::Extensions::Array::Parentization
|
30
|
+
end
|
31
|
+
|
32
|
+
class Boolean #:nodoc
|
33
|
+
extend Humanoid::Extensions::Boolean::Conversions
|
34
|
+
end
|
35
|
+
|
36
|
+
class Date #:nodoc
|
37
|
+
extend Humanoid::Extensions::Date::Conversions
|
38
|
+
end
|
39
|
+
|
40
|
+
class DateTime #:nodoc
|
41
|
+
extend Humanoid::Extensions::DateTime::Conversions
|
42
|
+
end
|
43
|
+
|
44
|
+
class Float #:nodoc
|
45
|
+
extend Humanoid::Extensions::Float::Conversions
|
46
|
+
end
|
47
|
+
|
48
|
+
class Hash #:nodoc
|
49
|
+
include Humanoid::Extensions::Hash::Accessors
|
50
|
+
include Humanoid::Extensions::Hash::Assimilation
|
51
|
+
include Humanoid::Extensions::Hash::CriteriaHelpers
|
52
|
+
include Humanoid::Extensions::Hash::Scoping
|
53
|
+
extend Humanoid::Extensions::Hash::Conversions
|
54
|
+
end
|
55
|
+
|
56
|
+
class Integer #:nodoc
|
57
|
+
extend Humanoid::Extensions::Integer::Conversions
|
58
|
+
end
|
59
|
+
|
60
|
+
class NilClass #:nodoc
|
61
|
+
include Humanoid::Extensions::Nil::Assimilation
|
62
|
+
end
|
63
|
+
|
64
|
+
class Object #:nodoc:
|
65
|
+
include Humanoid::Extensions::Object::Conversions
|
66
|
+
end
|
67
|
+
|
68
|
+
class Proc #:nodoc:
|
69
|
+
include Humanoid::Extensions::Proc::Scoping
|
70
|
+
end
|
71
|
+
|
72
|
+
class String #:nodoc
|
73
|
+
include Humanoid::Extensions::String::Inflections
|
74
|
+
extend Humanoid::Extensions::String::Conversions
|
75
|
+
end
|
76
|
+
|
77
|
+
class Symbol #:nodoc
|
78
|
+
remove_method :size if instance_methods.include? :size # temporal fix for ruby 1.9
|
79
|
+
include Humanoid::Extensions::Symbol::Inflections
|
80
|
+
end
|
81
|
+
|
82
|
+
class Time #:nodoc
|
83
|
+
extend Humanoid::Extensions::Time::Conversions
|
84
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Array #:nodoc:
|
5
|
+
module Accessors #:nodoc:
|
6
|
+
# If the attributes already exists in the array then they will be
|
7
|
+
# updated, otherwise they will be appended.
|
8
|
+
def update(attributes)
|
9
|
+
delete_if { |e| attributes["_id"] && (e["_id"] == attributes["_id"]) }
|
10
|
+
self.<< attributes
|
11
|
+
end
|
12
|
+
|
13
|
+
alias :merge! :update
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Array #:nodoc:
|
5
|
+
module Assimilation #:nodoc:
|
6
|
+
# Introduces a child object into the +Document+ object graph. This will
|
7
|
+
# set up the relationships between the parent and child and update the
|
8
|
+
# attributes of the parent +Document+.
|
9
|
+
#
|
10
|
+
# Options:
|
11
|
+
#
|
12
|
+
# parent: The +Document+ to assimilate into.
|
13
|
+
# options: The association +Options+ for the child.
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# <tt>[{:street => "Queen St."}, {:street => "King St."}].assimilate(person, options)</tt>
|
18
|
+
#
|
19
|
+
# Returns: The child +Document+.
|
20
|
+
def assimilate(parent, options)
|
21
|
+
each { |child| child.assimilate(parent, options) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Array #:nodoc:
|
5
|
+
# This module converts arrays into humanoid related objects.
|
6
|
+
module Conversions #:nodoc:
|
7
|
+
# Converts this array into an array of hashes.
|
8
|
+
def humanoidize
|
9
|
+
collect { |obj| obj.attributes }
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.included(base)
|
13
|
+
base.class_eval do
|
14
|
+
extend ClassMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def get(value)
|
20
|
+
value
|
21
|
+
end
|
22
|
+
def set(value)
|
23
|
+
value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Array #:nodoc:
|
5
|
+
module Parentization #:nodoc:
|
6
|
+
# Adds the parent document to each element in the array.
|
7
|
+
def parentize(parent, association_name)
|
8
|
+
each { |obj| obj.parentize(parent, association_name) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Boolean #:nodoc:
|
5
|
+
module Conversions #:nodoc:
|
6
|
+
def set(value)
|
7
|
+
val = value.to_s
|
8
|
+
val == "true" || val == "1"
|
9
|
+
end
|
10
|
+
def get(value)
|
11
|
+
value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Date #:nodoc:
|
5
|
+
module Conversions #:nodoc:
|
6
|
+
def set(value)
|
7
|
+
value.to_date.at_midnight.to_time unless value.blank?
|
8
|
+
end
|
9
|
+
def get(value)
|
10
|
+
value ? value.getlocal.to_date : value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module DateTime #:nodoc:
|
5
|
+
module Conversions #:nodoc:
|
6
|
+
def set(value)
|
7
|
+
return nil if value.blank?
|
8
|
+
::DateTime.parse(value.to_s).utc
|
9
|
+
end
|
10
|
+
def get(value)
|
11
|
+
return nil if value.blank?
|
12
|
+
::Time.zone ? ::Time.parse(value.to_s).getlocal.to_datetime : value.to_datetime
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Float #:nodoc:
|
5
|
+
module Conversions #:nodoc:
|
6
|
+
def set(value)
|
7
|
+
return nil if value.blank?
|
8
|
+
value =~ /\d/ ? value.to_f : value
|
9
|
+
end
|
10
|
+
def get(value)
|
11
|
+
value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Hash #:nodoc:
|
5
|
+
module Accessors #:nodoc:
|
6
|
+
|
7
|
+
# Remove a set of attributes from a hash. If the attributes are
|
8
|
+
# contained in an array it will remove it from the array, otherwise it
|
9
|
+
# will delete the child attribute completely.
|
10
|
+
def remove(key, attrs)
|
11
|
+
elements = self[key]
|
12
|
+
if elements
|
13
|
+
key.singular? ? self[key] = nil : elements.delete(attrs)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Inserts new attributes into the hash. If the elements are present in
|
18
|
+
# the hash it will update them, otherwise it will add the new
|
19
|
+
# attributes into the hash as either a child hash or child array of
|
20
|
+
# hashes.
|
21
|
+
def insert(key, attrs)
|
22
|
+
elements = self[key]
|
23
|
+
if elements
|
24
|
+
elements.merge!(attrs)
|
25
|
+
else
|
26
|
+
self[key] = key.singular? ? attrs : [attrs]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# If a _type key exists in the hash, return the class for the value.
|
31
|
+
def klass
|
32
|
+
class_name = self["_type"]
|
33
|
+
class_name ? class_name.constantize : nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Humanoid #:nodoc:
|
3
|
+
module Extensions #:nodoc:
|
4
|
+
module Hash #:nodoc:
|
5
|
+
module Assimilation #:nodoc:
|
6
|
+
# Introduces a child object into the +Document+ object graph. This will
|
7
|
+
# set up the relationships between the parent and child and update the
|
8
|
+
# attributes of the parent +Document+.
|
9
|
+
#
|
10
|
+
# Options:
|
11
|
+
#
|
12
|
+
# parent: The +Document+ to assimilate into.
|
13
|
+
# options: The association +Options+ for the child.
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# <tt>{:first_name => "Hank", :last_name => "Moody"}.assimilate(name, options)</tt>
|
18
|
+
#
|
19
|
+
# Returns: The child +Document+.
|
20
|
+
def assimilate(parent, options, type = nil)
|
21
|
+
klass = type ? type : options.klass
|
22
|
+
child = klass.instantiate(:_parent => parent)
|
23
|
+
child.write_attributes(self.merge("_type" => klass.name))
|
24
|
+
child.identify
|
25
|
+
child.assimilate(parent, options)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|