factory_json 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: 46bd43eb84be63ce213d2aa07324182523b194c0
4
+ data.tar.gz: 0ebcafa553e503bb5bb140426e7e95ef55e721b0
5
+ SHA512:
6
+ metadata.gz: 21478e944982ce49bf27bb74d1a9d48aa914ea60693bf05f13cc2708090ae2c3a68f36eec295bee2e13691f00e251f1efaa8007b0d15c215fc697b9fb5db4ab0
7
+ data.tar.gz: f41687a7e44449836e34aa69924098f23743cf33db5d00ac74972c98de6dd946f8cd0086ec97625dd8e8c3a7fdadc416d2605d46baf63379adf0de9fef13c3a4
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .rvmrc
7
+ .ruby-version
8
+ .ruby-gemset
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
+ *.bundle
22
+ *.so
23
+ *.o
24
+ *.a
25
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in factory_json.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 E-Max
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,96 @@
1
+ # FactoryJson
2
+
3
+ Hi! The main purpose of this gem is to provide a simple way to create JSON data structures using the power of
4
+ FactoryGirl's DSL.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'factory_json'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install factory_json
19
+
20
+ ## Usage
21
+
22
+ This extension provides two new ways of working with FactoryGirl
23
+
24
+ #### Dealing with Hashes
25
+
26
+ Tired of creating json fixtures somewhere in your app and keeping them up to date alongside with your factories? Now you can create your JSON fixtures with factories: All you need to do is specify a Hash as the build class while registering the factory ... and that is pretty much it!
27
+
28
+ ```ruby
29
+ # spec/factories/data_structures.rb
30
+ FactoryGirl.define do
31
+ factory :entry_relation, class: 'Hash' do
32
+ type "RELATED"
33
+ label "Apartment:"
34
+ name_summary "Mr Darth Vader"
35
+
36
+ trait :with_address do
37
+ street_summary "Tatooine"
38
+ city_summary "Galactic Empire"
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ And this might be a way to use these in our specs
45
+
46
+ ```ruby
47
+ entry_relation = FactoryGirl.as_json(:entry_relation, :with_address) # to return data as a Ruby hash, that beeing piped through JSON parse
48
+ entry_relation = FactoryGirl.json(:entry_relation, :with_address) # to return string, that contains valid JSON
49
+ ```
50
+
51
+ #### Dealing with Models
52
+
53
+
54
+ You have a factory that you'd also like to use as a JSON fixture? Use common usage patterns (to_json) with our fixtures now! Not only can you get JSON from your factories, but of course also Hashes. All you need to do is to define to_json or to_hash (since to_json is based on to_hash) on your model.
55
+
56
+ ```ruby
57
+ # spec/factories/users.rb
58
+ FactoryGirl.define do
59
+ factory :user, class: 'User' do
60
+ name "User 1"
61
+ email "test@example.com"
62
+
63
+ trait :with_profile
64
+ association :profile
65
+ end
66
+ end
67
+ end
68
+
69
+ # app/models/user.rb
70
+ class User
71
+
72
+ def to_json
73
+ {
74
+ name: self.name,
75
+ email: self.email,
76
+ profile: profile.to_hash
77
+ }.to_json
78
+ end
79
+ end
80
+ ```
81
+
82
+
83
+ ```ruby
84
+ user = FactoryGirl.build(:user, :with_profile) # to return new User object
85
+ user = FactoryGirl.create(:user, :with_profile) # to return persistent User object
86
+ user_hash = FactoryGirl.as_json(:user, :with_profile) # to return data as a Ruby hash, that beeing piped through JSON parse
87
+ user_json = FactoryGirl.json(:user, :with_profile) # to return string, that contains valid JSON
88
+ ```
89
+
90
+ ## Contributing
91
+
92
+ 1. Fork it ( https://github.com/[my-github-username]/factory_json/fork )
93
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
94
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 4. Push to the branch (`git push origin my-new-feature`)
96
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'factory_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "factory_json"
8
+ spec.version = FactoryJson::VERSION
9
+ spec.authors = ["E-Max"]
10
+ spec.email = ["max.zab87@gmail.com"]
11
+ spec.summary = %q{FactoryGirl's extension for JSON support.}
12
+ spec.description = %q{FactoryGirl can now handle JSON representation for an object.}
13
+ spec.homepage = "https://github.com/local-ch/factory_json"
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.add_dependency "factory_girl"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "mocha", ">= 0.12.8"
27
+ end
@@ -0,0 +1,22 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class AsJSON
4
+ def association(runner)
5
+ runner.run(:null)
6
+ end
7
+
8
+ def result(evaluation)
9
+ source =
10
+ build_class_is_hash?(evaluation) ? evaluation.hash : evaluation.object
11
+
12
+ ::JSON.parse source.to_json
13
+ end
14
+
15
+ private
16
+
17
+ def build_class_is_hash?(evaluation)
18
+ evaluation.instance_variable_get(:@attribute_assigner).instance_variable_get(:@build_class) == Hash
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class JSON
4
+ def association(runner)
5
+ runner.run(:null)
6
+ end
7
+
8
+ def result(evaluation)
9
+ source =
10
+ build_class_is_hash?(evaluation) ? evaluation.hash : evaluation.object
11
+
12
+ source.to_json
13
+ end
14
+
15
+ private
16
+
17
+ def build_class_is_hash?(evaluation)
18
+ evaluation.instance_variable_get(:@attribute_assigner).instance_variable_get(:@build_class) == Hash
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ require "factory_json/version"
2
+
3
+ require "factory_girl/strategy/json"
4
+ require "factory_girl/strategy/as_json"
5
+
6
+ FactoryGirl.register_strategy(:json, FactoryGirl::Strategy::JSON)
7
+ FactoryGirl.register_strategy(:as_json, FactoryGirl::Strategy::AsJSON)
@@ -0,0 +1,3 @@
1
+ module FactoryJson
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,58 @@
1
+
2
+ require 'spec_helper'
3
+
4
+ describe FactoryGirl::Strategy::AsJSON do
5
+ it_should_behave_like "strategy without association support"
6
+
7
+ context "Object source" do
8
+ let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false } }
9
+ let(:object) { stub("user", to_json: result.to_json, ) }
10
+ let(:evaluation) { stub("evaluation", object: object) }
11
+
12
+ before(:each) do
13
+ subject.stubs(:build_class_is_hash?).returns(false)
14
+ end
15
+
16
+ it "pipes hash through JSON lib" do
17
+ JSON.expects(:parse).with(kind_of(String))
18
+
19
+ subject.result(evaluation)
20
+ end
21
+
22
+ it "returns to_hash from the evaluation for an object" do
23
+ expect(subject.result(evaluation)).to eq(result)
24
+ end
25
+
26
+ it "does not run the to_create block" do
27
+ expect do
28
+ subject.result(evaluation)
29
+ end.to_not raise_error
30
+ end
31
+ end
32
+
33
+ context "Hash source" do
34
+ let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false } }
35
+ let(:hash) { { name: "John Doe", gender: "Male", admin: false } }
36
+ let(:evaluation) { stub("evaluation", hash: hash) }
37
+
38
+ before(:each) do
39
+ subject.stubs(:build_class_is_hash?).returns(true)
40
+ end
41
+
42
+ it "pipes hash through JSON lib" do
43
+ JSON.expects(:parse).with(kind_of(String))
44
+
45
+ subject.result(evaluation)
46
+ end
47
+
48
+ it "returns the hash from the evaluation" do
49
+ expect(subject.result(evaluation)).to eq result
50
+ end
51
+
52
+ it "does not run the to_create block" do
53
+ expect do
54
+ subject.result(evaluation)
55
+ end.to_not raise_error
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,46 @@
1
+
2
+ require 'spec_helper'
3
+
4
+ describe FactoryGirl::Strategy::JSON do
5
+ it_should_behave_like "strategy without association support"
6
+
7
+ context "Object source" do
8
+ let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false }.to_json }
9
+ let(:object) { stub("user", to_json: result) }
10
+ let(:evaluation) { stub("evaluation", object: object) }
11
+
12
+ before(:each) do
13
+ subject.stubs(:build_class_is_hash?).returns(false)
14
+ end
15
+
16
+ it "returns to_hash from the evaluation for an object" do
17
+ expect(subject.result(evaluation)).to eq(result)
18
+ end
19
+
20
+ it "does not run the to_create block" do
21
+ expect do
22
+ subject.result(evaluation)
23
+ end.to_not raise_error
24
+ end
25
+ end
26
+
27
+ context "Hash source" do
28
+ let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false }.to_json }
29
+ let(:hash) { { name: "John Doe", gender: "Male", admin: false } }
30
+ let(:evaluation) { stub("evaluation", hash: hash) }
31
+
32
+ before(:each) do
33
+ subject.stubs(:build_class_is_hash?).returns(true)
34
+ end
35
+
36
+ it "returns the hash from the evaluation" do
37
+ expect(subject.result(evaluation)).to eq result
38
+ end
39
+
40
+ it "does not run the to_create block" do
41
+ expect do
42
+ subject.result(evaluation)
43
+ end.to_not raise_error
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'rspec'
6
+ require 'json'
7
+ require 'mocha/api'
8
+
9
+ require 'factory_girl'
10
+ require 'factory_json'
11
+
12
+ spec = Gem::Specification.find_by_name('factory_girl')
13
+ require File.join(spec.gem_dir, 'spec', 'support', 'shared_examples', 'strategy')
14
+
15
+ RSpec.configure do |config|
16
+ config.expect_with :rspec do |expectations|
17
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
18
+ end
19
+
20
+ config.mock_framework = :mocha
21
+
22
+ config.after do
23
+ FactoryGirl.reload
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - E-Max
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: factory_girl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.12.8
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.12.8
83
+ description: FactoryGirl can now handle JSON representation for an object.
84
+ email:
85
+ - max.zab87@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - factory_json.gemspec
97
+ - lib/factory_girl/strategy/as_json.rb
98
+ - lib/factory_girl/strategy/json.rb
99
+ - lib/factory_json.rb
100
+ - lib/factory_json/version.rb
101
+ - spec/factory_girl/strategy/as_json_spec.rb
102
+ - spec/factory_girl/strategy/json_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: https://github.com/local-ch/factory_json
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: FactoryGirl's extension for JSON support.
128
+ test_files:
129
+ - spec/factory_girl/strategy/as_json_spec.rb
130
+ - spec/factory_girl/strategy/json_spec.rb
131
+ - spec/spec_helper.rb