faker_maker 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e70530ef4ef67e59247210037bbdbe4fd0c23c6c26666b1459db6f5ac9f65a5e
4
- data.tar.gz: f007f3658015e759942885a786424b4f444f34b4d655ab64f5da5044e90274df
3
+ metadata.gz: e265f317bfd43d44382e9db7e9359c7c0bf21e946b361c3d0973d9ca0cd9c6d1
4
+ data.tar.gz: 5765b8eff404d6e8156ef05b5dc3fff0fd1f747d53fb5e2bd8a50867ed459f47
5
5
  SHA512:
6
- metadata.gz: 42b0194260b51a49a03063183da089d885291211f4bc5ef3583b0df79d0ecac6f742ada1a7c0541fef24c2bded3056d91b83697db487eee2f569326dd00324d5
7
- data.tar.gz: cf02455a97627cb404d7cb6365d8485d5ff8266797e91bb9745eeb8a40499575832b31cd4dfceaedeea8634aed6c7b5ee0ccc78922243be5fecbd6f322da1e1e
6
+ metadata.gz: 68c81cf0d7fc4be3bd21e303fb2315bbfd0b99b438292e6a5e525451a39e21ccf72d309210f0ad7c868a67c510534c540b94f3c80f321924783fdc5babe90350
7
+ data.tar.gz: 17aa8890f8e86834810742ecc6d10784c2a4e673af059db8f0249a7bfb4cd9b84335cf34ad416b879f7b824632cbd9003bbcba8340a55a82b8100985f19741dd
data/.gitignore CHANGED
@@ -7,5 +7,6 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ Gemfile.lock
10
11
  # rspec failure tracking
11
12
  .rspec_status
data/README.md CHANGED
@@ -123,6 +123,28 @@ FakerMaker.factory :basket do
123
123
  end
124
124
  ```
125
125
 
126
+ JSON keys are often have a different stylistic convention than Ruby's snake-case so you can override the attribute's name when is come to generating JSON:
127
+
128
+ ```ruby
129
+ FakerMaker.factory :item do
130
+ name( json: 'productName' ) { Faker::Commerce.product_name }
131
+ price( json: 'ticketPrice' ) { Faker::Commerce.price }
132
+ end
133
+ ```
134
+
135
+ so in Ruby you would access the attribute using the attribute name as in:
136
+
137
+ ```ruby
138
+ item = FM[:item].build
139
+ puts item
140
+ ```
141
+
142
+ but `item.to_json` would produce:
143
+
144
+ ```javascript
145
+ {"productName":"Enormous Silk Pants","ticketPrice":55.35}
146
+ ```
147
+
126
148
  ### Building instances
127
149
 
128
150
  Instances are Plain Ol' Ruby Objects and the attributes are attached with getters and setters with their values assigned to the value return from their block at build time.
@@ -1,12 +1,13 @@
1
1
  module FakerMaker
2
2
  class Attribute
3
- attr_reader :name, :block
3
+ attr_reader :name, :block, :translation
4
4
 
5
5
  def initialize name, options={}, block
6
6
  assert_valid_options options
7
7
  @name = name
8
8
  @block = block
9
9
  @cardinality = options[:has] || 1
10
+ @translation = options[:json]
10
11
  @array = options[:array] == true
11
12
  end
12
13
 
@@ -22,6 +23,10 @@ module FakerMaker
22
23
  end
23
24
  end
24
25
 
26
+ def translation?
27
+ ! @translation.blank?
28
+ end
29
+
25
30
  private
26
31
 
27
32
  def forced_array?
@@ -29,7 +34,7 @@ module FakerMaker
29
34
  end
30
35
 
31
36
  def assert_valid_options options
32
- options.assert_valid_keys :has, :array
37
+ options.assert_valid_keys :has, :array, :json
33
38
  end
34
39
 
35
40
  end
@@ -32,6 +32,7 @@ module FakerMaker
32
32
  @klass = Class.new @parent_class
33
33
  Object.const_set @class_name, @klass
34
34
  attach_attributes_to_class
35
+ attach_json_overrides_to_class
35
36
  end
36
37
  @klass
37
38
  end
@@ -44,6 +45,18 @@ module FakerMaker
44
45
  ! @parent.nil?
45
46
  end
46
47
 
48
+ def json_key_map
49
+ unless @json_key_map
50
+ @json_key_map = {}.with_indifferent_access
51
+ @json_key_map.merge!( FakerMaker[parent].json_key_map ) if parent?
52
+ attributes.each_with_object( @json_key_map ) do |attr, map|
53
+ key = attr.translation? ? attr.translation : attr.name
54
+ map[attr.name] = key
55
+ end
56
+ end
57
+ @json_key_map
58
+ end
59
+
47
60
  protected
48
61
 
49
62
  def populate_instance instance
@@ -59,6 +72,7 @@ module FakerMaker
59
72
 
60
73
  instance.send "#{attr.name}=", value
61
74
  end
75
+ instance.instance_variable_set( :@fm_factory, self )
62
76
  end
63
77
 
64
78
  private
@@ -71,6 +85,13 @@ module FakerMaker
71
85
  @attributes.each do |attr|
72
86
  @klass.send( :attr_accessor, attr.name )
73
87
  end
88
+ @klass.send( :attr_reader, :fm_factory )
89
+ end
90
+
91
+ def attach_json_overrides_to_class
92
+ @klass.define_method :as_json do |options={}|
93
+ super( options.merge( except: 'fm_factory' ) ).transform_keys{ |key| @fm_factory.json_key_map[key] || key }
94
+ end
74
95
  end
75
96
 
76
97
  def assert_valid_options options
@@ -1,3 +1,3 @@
1
1
  module FakerMaker
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faker_maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Brookes-Thomas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-05 00:00:00.000000000 Z
11
+ date: 2019-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -120,7 +120,6 @@ files:
120
120
  - ".rspec"
121
121
  - ".travis.yml"
122
122
  - Gemfile
123
- - Gemfile.lock
124
123
  - LICENSE.txt
125
124
  - README.md
126
125
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,65 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- faker_maker (0.1.0)
5
- activesupport (>= 5.2)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activesupport (5.2.2)
11
- concurrent-ruby (~> 1.0, >= 1.0.2)
12
- i18n (>= 0.7, < 2)
13
- minitest (~> 5.1)
14
- tzinfo (~> 1.1)
15
- coderay (1.1.2)
16
- concurrent-ruby (1.1.4)
17
- diff-lcs (1.3)
18
- docile (1.3.1)
19
- faker (1.9.3)
20
- i18n (>= 0.7)
21
- i18n (1.5.3)
22
- concurrent-ruby (~> 1.0)
23
- json (2.2.0)
24
- method_source (0.9.2)
25
- minitest (5.11.3)
26
- pry (0.12.2)
27
- coderay (~> 1.1.0)
28
- method_source (~> 0.9.0)
29
- rake (10.5.0)
30
- rspec (3.8.0)
31
- rspec-core (~> 3.8.0)
32
- rspec-expectations (~> 3.8.0)
33
- rspec-mocks (~> 3.8.0)
34
- rspec-core (3.8.0)
35
- rspec-support (~> 3.8.0)
36
- rspec-expectations (3.8.2)
37
- diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.8.0)
39
- rspec-mocks (3.8.0)
40
- diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.8.0)
42
- rspec-support (3.8.0)
43
- simplecov (0.16.1)
44
- docile (~> 1.1)
45
- json (>= 1.8, < 3)
46
- simplecov-html (~> 0.10.0)
47
- simplecov-html (0.10.2)
48
- thread_safe (0.3.6)
49
- tzinfo (1.2.5)
50
- thread_safe (~> 0.1)
51
-
52
- PLATFORMS
53
- ruby
54
-
55
- DEPENDENCIES
56
- bundler (~> 2.0)
57
- faker (>= 1.9)
58
- faker_maker!
59
- pry
60
- rake (~> 10.0)
61
- rspec (>= 3.8)
62
- simplecov (>= 0.16)
63
-
64
- BUNDLED WITH
65
- 2.0.1