traitr 0.0.1 → 0.0.2
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 +4 -4
- data/lib/traitor/config.rb +23 -9
- data/lib/traitor.rb +56 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ed989bf89f57b2646ce63d1e3bf8622adb9b174
|
4
|
+
data.tar.gz: fe0731743fb6c9c74bbc4a831dcf768d8a7302b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 496b42da0b8f2b876302e6f039b2e42ef137ab193fceb5671cfb08f825ad1c7f4c5b25682388689fcf710dc2637a5a3d8b16b7cc42af1470eb911d87966e5548
|
7
|
+
data.tar.gz: f4adb687f50cd4c6820274244ee3aec07a5a476698968aa543c99d68f72275213369d5077b9448939803207a99ba23882039eac15c8b76645b564cb1421e1336
|
data/lib/traitor/config.rb
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
module Traitor
|
2
2
|
class Config
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
@save_method = nil
|
4
|
+
@save_kwargs = {}
|
5
|
+
@build_kwargs = {}
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :save_method, :save_kwargs, :build_kwargs
|
9
|
+
|
10
|
+
def configure_for_rails!
|
11
|
+
@save_method = :save
|
12
|
+
@save_kwargs = { validate: false }
|
13
|
+
@build_kwargs = { without_validation: true }
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure_safe_for_rails!
|
17
|
+
@save_method = :save
|
18
|
+
@save_kwargs = {}
|
19
|
+
@build_kwargs = {}
|
20
|
+
end
|
8
21
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
22
|
+
def reset!
|
23
|
+
@save_method = nil
|
24
|
+
@save_kwargs = {}
|
25
|
+
@build_kwargs = {}
|
26
|
+
end
|
13
27
|
end
|
14
28
|
end
|
15
29
|
end
|
data/lib/traitor.rb
CHANGED
@@ -3,66 +3,91 @@ require 'traitor/find_definitions'
|
|
3
3
|
require 'traitor/error'
|
4
4
|
|
5
5
|
module Traitor
|
6
|
+
BLOCK_KEYS = [:after_build, :after_create]
|
7
|
+
|
6
8
|
@trait_library = {}
|
7
9
|
@block_library = {}
|
8
|
-
|
9
|
-
@save_method = nil
|
10
|
-
@save_kwargs = {}
|
11
|
-
@build_kwargs = {}
|
12
|
-
|
13
10
|
@class_cache = {}
|
14
11
|
@trait_cache = {}
|
15
12
|
|
16
|
-
class << self
|
17
|
-
attr_writer :save_method, :save_kwargs, :build_kwargs
|
18
|
-
end
|
19
|
-
|
20
13
|
def self.reset!
|
21
14
|
@trait_library = {}
|
15
|
+
@block_library = {}
|
22
16
|
@class_cache = {}
|
23
17
|
@trait_cache = {}
|
24
18
|
end
|
25
19
|
|
26
|
-
def self.define(klass, **traits
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
20
|
+
def self.define(klass, **traits)
|
21
|
+
@trait_library[klass] ||= {}
|
22
|
+
|
23
|
+
(traits.keys & BLOCK_KEYS).each do |block_type|
|
24
|
+
block = traits.delete block_type
|
25
|
+
@block_library[klass] ||= {class: {}, traits: {}}
|
26
|
+
@block_library[klass][:class][block_type] = block
|
31
27
|
end
|
32
|
-
|
28
|
+
|
29
|
+
traits.each do |trait, attributes|
|
30
|
+
(attributes.keys & BLOCK_KEYS).each do |block_type|
|
31
|
+
block = attributes.delete block_type
|
32
|
+
@block_library[klass] ||= {class: {}, traits: {}}
|
33
|
+
@block_library[klass][:traits][trait] ||= {}
|
34
|
+
@block_library[klass][:traits][trait][block_type] = block
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
@trait_library[klass].merge!(traits)
|
33
39
|
end
|
34
40
|
|
35
41
|
##
|
36
42
|
# build an instance of an object using the defined traits and attributes,
|
37
43
|
# and then save it.
|
38
44
|
##
|
39
|
-
def self.create(klass, *traits, **attributes
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
def self.create(klass, *traits, **attributes)
|
46
|
+
save_method = Traitor::Config.save_method
|
47
|
+
raise Traitor::Error.new("Cannot call Traitor.create until you have configured Traitor.save_method .") unless save_method
|
48
|
+
|
49
|
+
record = build(klass, *traits, **attributes)
|
50
|
+
|
51
|
+
if (save_kwargs = Traitor::Config.save_kwargs).any? # assignment intentional
|
52
|
+
record.public_send(save_method, **save_kwargs)
|
53
|
+
else
|
54
|
+
record.public_send(save_method)
|
55
|
+
end
|
56
|
+
|
57
|
+
call_blocks(klass, :after_create, record, *traits)
|
45
58
|
record
|
46
59
|
end
|
47
60
|
|
48
61
|
##
|
49
62
|
# build an instance of an object using the defined traits and attributes.
|
50
63
|
##
|
51
|
-
def self.build(klass, *traits, **attributes
|
64
|
+
def self.build(klass, *traits, **attributes)
|
52
65
|
attributes = get_attributes_from_traits(klass, traits).merge(attributes)
|
53
|
-
|
54
|
-
|
55
|
-
|
66
|
+
|
67
|
+
record = if (build_kwargs = Traitor::Config.build_kwargs).any? # assignment intentional
|
68
|
+
convert_to_class(klass).new(attributes, **build_kwargs)
|
69
|
+
else
|
70
|
+
convert_to_class(klass).new(attributes)
|
71
|
+
end
|
72
|
+
|
73
|
+
call_blocks(klass, :after_build, record, *traits)
|
56
74
|
record
|
57
75
|
end
|
58
76
|
|
59
77
|
# private methods
|
60
78
|
|
61
|
-
def self.
|
62
|
-
|
63
|
-
|
79
|
+
def self.call_blocks(klass, trigger, record, *traits)
|
80
|
+
return unless @block_library[klass]
|
81
|
+
[].tap do |blocks|
|
82
|
+
blocks << @block_library[klass][:class][trigger]
|
83
|
+
traits.each do |trait|
|
84
|
+
if @block_library[klass][:traits][trait]
|
85
|
+
blocks << @block_library[klass][:traits][trait][trigger]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end.compact.each { |block| block.call(record) }
|
64
89
|
end
|
65
|
-
private_class_method :
|
90
|
+
private_class_method :call_blocks
|
66
91
|
|
67
92
|
def self.convert_to_class(klass)
|
68
93
|
@class_cache[klass] ||= Object.const_get(camelize(klass))
|
@@ -85,16 +110,15 @@ module Traitor
|
|
85
110
|
# use late resolution on lambda values by calling them here as part of constructing a new hash
|
86
111
|
Hash[
|
87
112
|
@trait_cache[cache_key].map do |attribute, value|
|
88
|
-
[attribute, value.is_a?(Proc) ? value.call : value]
|
113
|
+
[attribute, value.is_a?(Proc) && ![:after_build, :after_create].include?(attribute) ? value.call : value]
|
89
114
|
end
|
90
115
|
]
|
91
116
|
end
|
92
117
|
private_class_method :get_attributes_from_traits
|
93
118
|
|
94
|
-
# simplified from Rails.
|
95
119
|
def self.camelize(term)
|
96
120
|
string = term.to_s
|
97
|
-
string = string
|
121
|
+
string[0] = string[0].upcase
|
98
122
|
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
|
99
123
|
string.gsub!('/'.freeze, '::'.freeze)
|
100
124
|
string
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: traitr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Lome
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|