mongoid_fixtures 0.1.0 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # MongoidFixtures
2
2
  Fixtures for Ruby for Mongoid. No Rails needed!
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/mongoid_fixtures.svg)](http://badge.fury.io/rb/mongoid_fixtures)
5
+ [![Codacy Badge](https://api.codacy.com/project/badge/9a884363988548b9a83ae0044b6d74a1)](https://www.codacy.com/app/nycjv321/mongoid_fixtures)
4
6
  [![Code Climate](https://codeclimate.com/github/nycjv321/mongoid_fixtures/badges/gpa.svg)](https://codeclimate.com/github/nycjv321/mongoid_fixtures)
7
+ [![Build Status](https://travis-ci.org/nycjv321/mongoid_fixtures.svg?branch=master)](https://travis-ci.org/nycjv321/mongoid_fixtures)
5
8
  [![Test Coverage](https://codeclimate.com/github/nycjv321/mongoid_fixtures/badges/coverage.svg)](https://codeclimate.com/github/nycjv321/mongoid_fixtures/coverage)
6
9
 
7
10
  ## Installation
@@ -194,12 +197,12 @@ Or install it yourself as:
194
197
  1-N referenced, 1-1 referenced, 1-1 embedded relationships, and 1-N embedded relationships.
195
198
  If a relation already exists in the db, this library will return it instead of recreating it.
196
199
 
197
- 3. Invoke `MongoidFixtures::load(City)`
200
+ 3. Invoke `MongoidFixtures.load(City)`
198
201
  4. The above method invocation will load all test fixture instances of City objects defined
199
202
  in /test/fixtures/cities.yml as well as dependent objects
200
203
  5. Use your fixtures!
201
204
 
202
- cities = MongoidFixtures::load(City)
205
+ cities = MongoidFixtures.load(City)
203
206
  puts cities # {:new_york_city=>#<City _id: 55ebc6b2e138231068000004, name: "New York City", time_zone: nil, demonym: "New Yorker", settled: 1624, consolidated: 1989, custom_attributes: [{"boroughs"=>["Manhattan", "The Bronx", "Brooklyn", "Queens", "Staten Island"]}], geo_uri_scheme_id: BSON::ObjectId('55ebc6b2e138231068000006'), _type: "City", state_id: BSON::ObjectId('55ebc6b2e138231068000000')>, :terrytown=>#<City _id: 55ebc6b2e13823106800000c, name: "Terrytown", time_zone: nil, demonym: nil, settled: nil, consolidated: nil, custom_attributes: nil, geo_uri_scheme_id: BSON::ObjectId('55ebc6b2e138231068000005'), _type: "City", state_id: BSON::ObjectId('55ebc6b2e138231068000002')>}
204
207
 
205
208
  In the DB:
@@ -18,7 +18,13 @@ module MongoidFixtures
18
18
  end
19
19
 
20
20
  def self.load
21
- load_fixtures Dir[File.join(File.expand_path('../..', __FILE__), "#{path}/*.yml")]
21
+ if Dir.exists?("#{path}")
22
+ load_fixtures Dir["#{path}/*.yml"]
23
+ elsif Dir.exists?("../#{path}")
24
+ load_fixtures Dir["../#{path}/*.yml"]
25
+ else
26
+ raise('Unable to find fixtures in either /test/fixtures or ../test/fixtures')
27
+ end
22
28
  end
23
29
 
24
30
  def self.load_fixtures(fixture_names)
@@ -40,8 +46,8 @@ module MongoidFixtures
40
46
  :private
41
47
 
42
48
  Linguistics.use(:en)
43
- Loader::path = 'test/fixtures'
44
- Loader::load
49
+ Loader.path = 'test/fixtures'
50
+ Loader.load
45
51
 
46
52
  def self.load(clazz)
47
53
  fixture_instances = Loader.instance.fixtures[clazz.to_s.downcase.en.plural] # get class name
@@ -89,9 +95,13 @@ module MongoidFixtures
89
95
  elsif value.is_a? Hash
90
96
  # take hash convert it to object and serialize it
91
97
  instance[field] = EmbedUtils.create_embedded_instance(field_clazz, value, instance)
92
- # else just set the field
98
+ # else just set the field
93
99
  else
94
- instance[field] = value
100
+ if include_setter?(instance, field)
101
+ instance.send("#{field}=", value)
102
+ else
103
+ instance[field] = value
104
+ end
95
105
  end
96
106
  end
97
107
  instances[key] = create_or_save_instance(instance) # store it based on its key name
@@ -99,6 +109,10 @@ module MongoidFixtures
99
109
  instances
100
110
  end
101
111
 
112
+ def self.include_setter?(instance, setter)
113
+ instance.class.instance_methods.include? "#{setter}=".to_sym
114
+ end
115
+
102
116
  def self.flatten_attributes(attributes)
103
117
  flattened_attributes = {}
104
118
  if attributes.is_a? String
@@ -131,6 +145,7 @@ module MongoidFixtures
131
145
  end
132
146
  flattened_attributes
133
147
  end
148
+
134
149
  :private
135
150
 
136
151
  def self.create_or_save_instance(instance)
@@ -144,8 +159,8 @@ module MongoidFixtures
144
159
  end
145
160
  instance
146
161
  end
147
- :private
148
162
 
163
+ :private
149
164
 
150
165
 
151
166
  end
@@ -1,6 +1,11 @@
1
1
  module EmbedUtils
2
2
  def self.create_embedded_instance(clazz, hash, instance)
3
3
  embed = clazz.new
4
+
5
+ unless hash.is_a? Hash
6
+ raise("#{hash} was supposed to be a collection of #{embed}. You have configured #{clazz} objects to be embedded instances of #{instance}.\nPlease store these objects within the #{instance} yml. Refer to the documentation for examples.")
7
+ end
8
+
4
9
  hash.each do |key, value|
5
10
  embed.send("#{key}=", value)
6
11
  end
@@ -20,7 +25,7 @@ module EmbedUtils
20
25
  end
21
26
 
22
27
  def self.insert_embedded_ids(instance)
23
- attributes = instance.attributes.select { |key, value| !key.to_s.eql?('_id') }
28
+ attributes = instance.attributes.select { |key, _| !key.to_s.eql?('_id') }
24
29
 
25
30
  attributes.each do |key, value|
26
31
  if attributes[key].is_a? Hash
@@ -1,3 +1,3 @@
1
1
  module MongoidFixtures
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
33
33
  spec.add_dependency 'linguistics', '~> 2.0.4'
34
34
  spec.add_dependency 'mongoid', '~> 4.0.2'
35
35
  spec.add_dependency 'monkey_patches', '~> 0.0.3'
36
+ spec.add_development_dependency 'bcrypt'
36
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_fixtures
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier L. Velasquez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-20 00:00:00.000000000 Z
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 0.0.3
97
+ - !ruby/object:Gem::Dependency
98
+ name: bcrypt
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'
97
111
  description: Provides mechanism to easily create fixtures with Mongoid
98
112
  email:
99
113
  - nycjv321@gmail.com