traitr 0.0.1
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.
- checksums.yaml +7 -0
- data/lib/traitor/config.rb +15 -0
- data/lib/traitor/error.rb +3 -0
- data/lib/traitor/find_definitions.rb +28 -0
- data/lib/traitor.rb +103 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0a70000928ed720a84d8e3917407c987746e00b2
|
4
|
+
data.tar.gz: eaa7da7e08396b1943446e3fc3ad3e9e319cb6cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49ff026c248777c002fb8f743c54eff1fc4e4f56d3984599a712d60cb35c60836137a1fc3ac555ed60379f926c643983fb3236a504ab88b3437d957a9453d648
|
7
|
+
data.tar.gz: 1f258d37b1e7be543bf8b0aa0bb44348664737b3b654bfa46405ddb87cf2c0689f84ecdd0964872a80eae67012c9111c2f12c0c548622c3d30ffe45e56853d88
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Traitor
|
2
|
+
class Config
|
3
|
+
def self.configure_for_rails!
|
4
|
+
Traitor.instance_variable_set(:@save_method, :save)
|
5
|
+
Traitor.instance_variable_set(:@save_kwargs, {validate: false})
|
6
|
+
Traitor.instance_variable_set(:@build_kwargs, {without_protection: true})
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.configure_safe_for_rails!
|
10
|
+
Traitor.instance_variable_set(:@save_method, :save)
|
11
|
+
Traitor.instance_variable_set(:@save_kwargs, {})
|
12
|
+
Traitor.instance_variable_set(:@build_kwargs, {})
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# based on https://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/find_definitions.rb
|
2
|
+
# as of 2016-07-21
|
3
|
+
# Copyright (c) 2008-2016 Joe Ferris and thoughtbot, inc. MIT License
|
4
|
+
module Traitor
|
5
|
+
class << self
|
6
|
+
# An Array of strings specifying locations that should be searched for
|
7
|
+
# factory definitions. By default, traitor will attempt to require
|
8
|
+
# "traitors", "test/traitors" and "spec/traitors". Only the first
|
9
|
+
# existing file will be loaded.
|
10
|
+
attr_accessor :definition_file_paths
|
11
|
+
end
|
12
|
+
|
13
|
+
self.definition_file_paths = %w(traitors test/traitors spec/traitors)
|
14
|
+
|
15
|
+
def self.find_definitions
|
16
|
+
absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }
|
17
|
+
|
18
|
+
absolute_definition_file_paths.uniq.each do |path|
|
19
|
+
load("#{path}.rb") if File.exist?("#{path}.rb")
|
20
|
+
|
21
|
+
if File.directory? path
|
22
|
+
Dir[File.join(path, '**', '*.rb')].sort.each do |file|
|
23
|
+
load file
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/traitor.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'traitor/config'
|
2
|
+
require 'traitor/find_definitions'
|
3
|
+
require 'traitor/error'
|
4
|
+
|
5
|
+
module Traitor
|
6
|
+
@trait_library = {}
|
7
|
+
@block_library = {}
|
8
|
+
|
9
|
+
@save_method = nil
|
10
|
+
@save_kwargs = {}
|
11
|
+
@build_kwargs = {}
|
12
|
+
|
13
|
+
@class_cache = {}
|
14
|
+
@trait_cache = {}
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_writer :save_method, :save_kwargs, :build_kwargs
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.reset!
|
21
|
+
@trait_library = {}
|
22
|
+
@class_cache = {}
|
23
|
+
@trait_cache = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.define(klass, **traits, &block)
|
27
|
+
if @trait_library.include? klass
|
28
|
+
@trait_library[klass].merge!(traits)
|
29
|
+
else
|
30
|
+
@trait_library[klass] = traits
|
31
|
+
end
|
32
|
+
@block_library[klass] = block if block_given?
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# build an instance of an object using the defined traits and attributes,
|
37
|
+
# and then save it.
|
38
|
+
##
|
39
|
+
def self.create(klass, *traits, **attributes, &block)
|
40
|
+
raise Traitor::Error.new("Cannot call Traitor.create until you have configured Traitor.save_method!") unless @save_method
|
41
|
+
record = build(klass, *traits, **attributes, &block)
|
42
|
+
record.public_send(@save_method, **@save_kwargs)
|
43
|
+
yield(record, at: :create) if block_given?
|
44
|
+
run_class_block(klass, record, :build)
|
45
|
+
record
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# build an instance of an object using the defined traits and attributes.
|
50
|
+
##
|
51
|
+
def self.build(klass, *traits, **attributes, &block)
|
52
|
+
attributes = get_attributes_from_traits(klass, traits).merge(attributes)
|
53
|
+
record = convert_to_class(klass).new(attributes, **@build_kwargs)
|
54
|
+
yield(record, at: :build) if block_given?
|
55
|
+
run_class_block(klass, record, :build)
|
56
|
+
record
|
57
|
+
end
|
58
|
+
|
59
|
+
# private methods
|
60
|
+
|
61
|
+
def self.run_class_block(klass, record, at)
|
62
|
+
class_block = @block_library[klass]
|
63
|
+
class_block.call(record, at: at) if class_block
|
64
|
+
end
|
65
|
+
private_class_method :run_class_block
|
66
|
+
|
67
|
+
def self.convert_to_class(klass)
|
68
|
+
@class_cache[klass] ||= Object.const_get(camelize(klass))
|
69
|
+
rescue NameError
|
70
|
+
raise Traitor::Error.new("Tried to create a #{camelize(klass)}, but it does not exist!")
|
71
|
+
end
|
72
|
+
private_class_method :convert_to_class
|
73
|
+
|
74
|
+
def self.get_attributes_from_traits(klass, traits)
|
75
|
+
# we only call this method when the klass has been converted to a key inside create
|
76
|
+
return {} unless library = @trait_library[klass]
|
77
|
+
|
78
|
+
traits = [:default_traits] + traits # always include default_traits as the first thing
|
79
|
+
|
80
|
+
cache_key = klass.to_s + ':' + traits.join(':')
|
81
|
+
@trait_cache[cache_key] ||= {}.tap do |attributes|
|
82
|
+
traits.each { |trait| attributes.merge!(library[trait] || {}) }
|
83
|
+
end
|
84
|
+
|
85
|
+
# use late resolution on lambda values by calling them here as part of constructing a new hash
|
86
|
+
Hash[
|
87
|
+
@trait_cache[cache_key].map do |attribute, value|
|
88
|
+
[attribute, value.is_a?(Proc) ? value.call : value]
|
89
|
+
end
|
90
|
+
]
|
91
|
+
end
|
92
|
+
private_class_method :get_attributes_from_traits
|
93
|
+
|
94
|
+
# simplified from Rails.
|
95
|
+
def self.camelize(term)
|
96
|
+
string = term.to_s
|
97
|
+
string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
|
98
|
+
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
|
99
|
+
string.gsub!('/'.freeze, '::'.freeze)
|
100
|
+
string
|
101
|
+
end
|
102
|
+
private_class_method :camelize
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: traitr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zach Lome
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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-its
|
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: timecop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: a lightweight, simplified, pure-ruby way to handle object model construction,
|
98
|
+
similar to FactoryGirl.
|
99
|
+
email:
|
100
|
+
- zslome@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- lib/traitor.rb
|
106
|
+
- lib/traitor/config.rb
|
107
|
+
- lib/traitor/error.rb
|
108
|
+
- lib/traitor/find_definitions.rb
|
109
|
+
homepage: https://github.com/kuraiou/traitor
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.4.5.1
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: a lightweight way to bind groups of attributes to a trait.
|
133
|
+
test_files: []
|
134
|
+
has_rdoc:
|