faker_maker 1.0.0 → 1.0.1

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: f2d4bf41d893f06f219a7ed7c630b5115f4351af244e480e582f554fca3d59aa
4
- data.tar.gz: 8bb38648b33e21b14afed798db4284e501cfc4f408377574d2bec6925d54bf0f
3
+ metadata.gz: fee4b085d7597c65882a322c750393492922c22653b66bb5c4b2e2da064c85be
4
+ data.tar.gz: ad9fa43a58aefcba71b1beaa0a2a53ebbf200644b01f9cfd568f234d665d1631
5
5
  SHA512:
6
- metadata.gz: b0db65f7c7432cde9d278d1fc789106fd50df317d9b7b637b5345561449ce543275cdb5ae7edaccd79019aa48595737d360d9be684276e7544f639d21fba6810
7
- data.tar.gz: fce49187a0b109f9a88b86a859fcd569bd9425e1f2d481f0df4b0a39c8625515d0ac863f91d0880848bc7bab846a97cf0118359807cd9f7b2f07b7129ccb8a50
6
+ metadata.gz: 14dd70aa6b4cdac5402e1603073bd018dbd699b23181ef8294c8ea5ec3b759d8e6dc6fffcccec30f66e3b34d764c975e6c899b8703027f94ffbdf4b4e53ae2f5
7
+ data.tar.gz: '083a7808d7c4c8d8830b1c02595192f9f72aef08880918d9667e7f63e8d5749f42878f239f24443aa4c680b7f5d6cce8f39b1992be1c50bde782f9b02f4a71ca'
@@ -0,0 +1,13 @@
1
+ # Use the latest 2.1 version of CircleCI pipeline processing engine, see https://circleci.com/docs/2.0/configuration-reference/
2
+ version: 2.1
3
+
4
+ jobs:
5
+ build:
6
+ docker:
7
+ - image: circleci/ruby:2.6.3-stretch-node
8
+ steps:
9
+ - checkout
10
+ - run: gem update --system
11
+ - run: gem install bundler
12
+ - run: bundle install
13
+ - run: bundle exec rake spec
data/.gitignore CHANGED
@@ -12,3 +12,6 @@ Gemfile.lock
12
12
  .rspec_status
13
13
 
14
14
  .DS_Store
15
+
16
+ faker_maker-*.gem
17
+ test.rb
data/.rubocop.yml CHANGED
@@ -1,5 +1,16 @@
1
1
  Layout/SpaceInsideParens:
2
2
  Enabled: false
3
-
3
+
4
4
  Metrics/LineLength:
5
- Max: 120
5
+ Max: 120
6
+
7
+ Metrics/ModuleLength:
8
+ Exclude:
9
+ - "**/*_spec.rb"
10
+
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - "**/*_spec.rb"
14
+
15
+ Layout/TrailingWhitespace:
16
+ Enabled: false
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
data/README.md CHANGED
@@ -1,3 +1,8 @@
1
+
2
+ ![CircleCI branch](https://img.shields.io/circleci/project/github/BillyRuffian/faker_maker/master.svg?style=flat-square)
3
+ [![CodeFactor](https://www.codefactor.io/repository/github/billyruffian/faker_maker/badge?style=flat-square)](https://www.codefactor.io/repository/github/billyruffian/faker_maker)
4
+ ![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/BillyRuffian/faker_maker.svg?style=flat-square)
5
+
1
6
  # FakerMaker
2
7
 
3
8
  FakerMaker is a simple fixture generator with a concise and straightforward syntax.
@@ -155,7 +160,7 @@ result = FakerMaker[:item].build( name: 'Electric Blanket' ) do |i|
155
160
  end
156
161
  ```
157
162
 
158
- then the value of `i.name` is 'Electric Sheep'.
163
+ then the value of `result.name` is 'Electric Sheep'.
159
164
 
160
165
  Beware when overriding values in this way: there is no type checking. You will get an exception if you try to set a value to an attribute that doesn't exist but you won't get one if you assign, say, an array of values where you would otherwise have a string and vice versa.
161
166
 
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "faker_maker"
3
+ require 'bundler/setup'
4
+ require 'faker_maker'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ # require 'pry'
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
data/faker_maker.gemspec CHANGED
@@ -1,3 +1,7 @@
1
+ # Disable rubocop checks for the .gemspec
2
+ # I'll take the output from 'bundle gem new' to be authoritative
3
+ # rubocop:disable all
4
+
1
5
  lib = File.expand_path('../lib', __FILE__)
2
6
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
7
  require 'faker_maker/version'
@@ -35,13 +39,13 @@ Gem::Specification.new do |spec|
35
39
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
40
  spec.require_paths = ['lib']
37
41
 
38
- spec.add_dependency 'activesupport', '>= 5.2'
42
+ spec.add_dependency 'activesupport', '>= 5.2', '< 6'
39
43
 
40
- spec.add_development_dependency 'bundler', '~> 2.0'
41
- spec.add_development_dependency 'faker', '>= 1.9'
42
- spec.add_development_dependency 'pry'
43
- spec.add_development_dependency 'rake', '~> 10.0'
44
- spec.add_development_dependency 'rspec', '>= 3.8'
45
- spec.add_development_dependency 'rubocop'
46
- spec.add_development_dependency 'simplecov', '>= 0.16'
44
+ spec.add_development_dependency 'bundler', '~> 2.0'
45
+ spec.add_development_dependency 'faker', '~> 1.9'
46
+ spec.add_development_dependency 'pry', '~> 0.12'
47
+ spec.add_development_dependency 'rake', '~> 12.3'
48
+ spec.add_development_dependency 'rspec', '~> 3.8'
49
+ spec.add_development_dependency 'rubocop', '~> 0.68'
50
+ spec.add_development_dependency 'simplecov', '~> 0.16'
47
51
  end
@@ -1,8 +1,9 @@
1
1
  module FakerMaker
2
+ # Attributes describe the fields of classes
2
3
  class Attribute
3
4
  attr_reader :name, :block, :translation
4
5
 
5
- def initialize name, options={}, block
6
+ def initialize( name, block = nil, options = {} )
6
7
  assert_valid_options options
7
8
  @name = name
8
9
  @block = block
@@ -24,7 +25,7 @@ module FakerMaker
24
25
  end
25
26
 
26
27
  def translation?
27
- ! @translation.blank?
28
+ !@translation.blank?
28
29
  end
29
30
 
30
31
  private
@@ -33,9 +34,8 @@ module FakerMaker
33
34
  @cardinality.is_a?( Range ) || @cardinality > 1
34
35
  end
35
36
 
36
- def assert_valid_options options
37
+ def assert_valid_options( options )
37
38
  options.assert_valid_keys :has, :array, :json
38
39
  end
39
-
40
40
  end
41
- end
41
+ end
@@ -1,7 +1,7 @@
1
1
  module FakerMaker
2
+ # Base module for defining the DSL
2
3
  module Base
3
-
4
- def factory name, options={}, &block
4
+ def factory(name, options = {}, &block)
5
5
  factory = FakerMaker::Factory.new name, options
6
6
  proxy = DefinitionProxy.new factory
7
7
  proxy.instance_eval( &block ) if block_given?
@@ -1,14 +1,17 @@
1
1
  module FakerMaker
2
+ # Proxy for mapping attribute names
2
3
  class DefinitionProxy
3
-
4
- def initialize factory
4
+ def initialize(factory)
5
5
  @factory = factory
6
6
  end
7
7
 
8
- def method_missing name, *args, &block
9
- attribute = FakerMaker::Attribute.new name, *args, block
8
+ def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper
9
+ attribute = FakerMaker::Attribute.new name, block, *args
10
10
  @factory.attach_attribute attribute
11
11
  end
12
-
12
+
13
+ def respond_to_missing?(method_name, include_private = false)
14
+ super
15
+ end
13
16
  end
14
- end
17
+ end
@@ -1,26 +1,30 @@
1
1
  module FakerMaker
2
+ # Factories construct instances of a fake
2
3
  class Factory
3
- attr_reader :name, :attributes, :class_name, :parent_class, :parent
4
+ attr_reader :name, :attributes, :class_name, :parent
4
5
 
5
- def initialize name, options={}
6
+ def initialize( name, options = {} )
6
7
  assert_valid_options options
7
8
  @name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
8
9
  @class_name = (options[:class] || @name).to_s.camelcase
9
10
  @attributes = []
10
11
  @klass = nil
11
12
  @parent = options[:parent]
12
- @parent_class = if @parent
13
+ end
14
+
15
+ def parent_class
16
+ if @parent
13
17
  Object.const_get( FakerMaker[@parent].class_name )
14
18
  else
15
- Object
19
+ Object
16
20
  end
17
21
  end
18
22
 
19
- def attach_attribute attribute
23
+ def attach_attribute( attribute )
20
24
  @attributes << attribute
21
25
  end
22
26
 
23
- def build attributes={}
27
+ def build( attributes = {} )
24
28
  instance = instantiate
25
29
  populate_instance instance, attributes
26
30
  yield instance if block_given?
@@ -29,7 +33,7 @@ module FakerMaker
29
33
 
30
34
  def assemble
31
35
  if @klass.nil?
32
- @klass = Class.new @parent_class
36
+ @klass = Class.new parent_class
33
37
  Object.const_set @class_name, @klass
34
38
  attach_attributes_to_class
35
39
  attach_json_overrides_to_class
@@ -37,12 +41,12 @@ module FakerMaker
37
41
  @klass
38
42
  end
39
43
 
40
- def to_json
44
+ def to_json(*_args)
41
45
  build.to_json
42
46
  end
43
47
 
44
48
  def parent?
45
- ! @parent.nil?
49
+ !@parent.nil?
46
50
  end
47
51
 
48
52
  def json_key_map
@@ -59,30 +63,39 @@ module FakerMaker
59
63
 
60
64
  protected
61
65
 
62
- def populate_instance instance, attr_override_values
63
- if ! ( unknown_attrs = attr_override_values.keys - @attributes.map( &:name ) ).empty?
64
- raise FakerMaker::NoSuchAttributeError,
65
- "Can't build an instance of '#{class_name}' setting '#{unknown_attrs.join( ', ' )}', no such attribute(s)"
66
- end
66
+ def populate_instance( instance, attr_override_values )
67
+ assert_only_know_attributes_for_override( attr_override_values )
67
68
 
68
- FakerMaker[parent].populate_instance instance if self.parent?
69
- @attributes.each do |attr|
70
- value = nil
71
-
72
- if ! attr_override_values[attr.name].nil?
73
- value = attr_override_values[attr.name]
74
- elsif attr.array?
75
- value = Array.new.tap{ |a| attr.cardinality.times{ a << attr.block.call } }
76
- else
77
- value = attr.block.call
78
- end
79
-
69
+ FakerMaker[parent].populate_instance instance if parent?
70
+ @attributes.each do |attr|
71
+ value = value_for_attribute( attr, attr_override_values )
80
72
  instance.send "#{attr.name}=", value
81
73
  end
82
74
  instance.instance_variable_set( :@fm_factory, self )
83
75
  end
84
76
 
85
77
  private
78
+
79
+ def assert_only_know_attributes_for_override( attr_override_values )
80
+ unknown_attrs = attr_override_values.keys - @attributes.map( &:name )
81
+ issue = "Can't build an instance of '#{class_name}' " \
82
+ "setting '#{unknown_attrs.join( ', ' )}', no such attribute(s)"
83
+ raise FakerMaker::NoSuchAttributeError, issue unless unknown_attrs.empty?
84
+ end
85
+
86
+ def attribute_hash_overridden_value?( attr, attr_override_values )
87
+ !attr_override_values[attr.name].nil?
88
+ end
89
+
90
+ def value_for_attribute( attr, attr_override_values )
91
+ if attribute_hash_overridden_value?( attr, attr_override_values )
92
+ attr_override_values[attr.name]
93
+ elsif attr.array?
94
+ [].tap { |a| attr.cardinality.times { a << attr.block.call } }
95
+ else
96
+ attr.block.call
97
+ end
98
+ end
86
99
 
87
100
  def instantiate
88
101
  assemble.new
@@ -96,13 +109,13 @@ module FakerMaker
96
109
  end
97
110
 
98
111
  def attach_json_overrides_to_class
99
- @klass.define_method :as_json do |options={}|
100
- super( options.merge( except: 'fm_factory' ) ).transform_keys{ |key| @fm_factory.json_key_map[key] || key }
112
+ @klass.define_method :as_json do |options = {}|
113
+ super( options.merge( except: 'fm_factory' ) ).transform_keys { |key| @fm_factory.json_key_map[key] || key }
101
114
  end
102
115
  end
103
116
 
104
- def assert_valid_options options
117
+ def assert_valid_options( options )
105
118
  options.assert_valid_keys :class, :parent
106
119
  end
107
120
  end
108
- end
121
+ end
@@ -1,3 +1,3 @@
1
1
  module FakerMaker
2
- VERSION = "1.0.0"
2
+ VERSION = '1.0.1'.freeze
3
3
  end
data/lib/faker_maker.rb CHANGED
@@ -8,6 +8,7 @@ require 'faker_maker/factory'
8
8
  require 'faker_maker/definition_proxy'
9
9
  require 'faker_maker/attribute'
10
10
 
11
+ # FakerMaker module for generating Fakes
11
12
  module FakerMaker
12
13
  extend FakerMaker::Base
13
14
 
@@ -18,7 +19,7 @@ module FakerMaker
18
19
 
19
20
  module_function
20
21
 
21
- def register_factory factory
22
+ def register_factory( factory )
22
23
  factory.assemble
23
24
  factories[factory.name] = factory
24
25
  end
@@ -27,19 +28,19 @@ module FakerMaker
27
28
  @factories ||= {}
28
29
  end
29
30
 
30
- def build name
31
+ def build( name )
31
32
  find_factory( name ).build
32
33
  end
33
34
 
34
- def [] name
35
+ def []( name )
35
36
  find_factory name
36
37
  end
37
38
 
38
- def find_factory name
39
+ def find_factory( name )
39
40
  raise NoSuchFactoryError, "No such factory '#{name}'" if factories[name].nil?
41
+
40
42
  factories[name]
41
43
  end
42
-
43
44
  end
44
45
 
45
46
  FM = FakerMaker
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: 1.0.0
4
+ version: 1.0.1
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-04-18 00:00:00.000000000 Z
11
+ date: 2019-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -17,6 +17,9 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '5.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -24,6 +27,9 @@ dependencies:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '5.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -42,84 +48,84 @@ dependencies:
42
48
  name: faker
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
45
- - - ">="
51
+ - - "~>"
46
52
  - !ruby/object:Gem::Version
47
53
  version: '1.9'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
- - - ">="
58
+ - - "~>"
53
59
  - !ruby/object:Gem::Version
54
60
  version: '1.9'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: pry
57
63
  requirement: !ruby/object:Gem::Requirement
58
64
  requirements:
59
- - - ">="
65
+ - - "~>"
60
66
  - !ruby/object:Gem::Version
61
- version: '0'
67
+ version: '0.12'
62
68
  type: :development
63
69
  prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
- - - ">="
72
+ - - "~>"
67
73
  - !ruby/object:Gem::Version
68
- version: '0'
74
+ version: '0.12'
69
75
  - !ruby/object:Gem::Dependency
70
76
  name: rake
71
77
  requirement: !ruby/object:Gem::Requirement
72
78
  requirements:
73
79
  - - "~>"
74
80
  - !ruby/object:Gem::Version
75
- version: '10.0'
81
+ version: '12.3'
76
82
  type: :development
77
83
  prerelease: false
78
84
  version_requirements: !ruby/object:Gem::Requirement
79
85
  requirements:
80
86
  - - "~>"
81
87
  - !ruby/object:Gem::Version
82
- version: '10.0'
88
+ version: '12.3'
83
89
  - !ruby/object:Gem::Dependency
84
90
  name: rspec
85
91
  requirement: !ruby/object:Gem::Requirement
86
92
  requirements:
87
- - - ">="
93
+ - - "~>"
88
94
  - !ruby/object:Gem::Version
89
95
  version: '3.8'
90
96
  type: :development
91
97
  prerelease: false
92
98
  version_requirements: !ruby/object:Gem::Requirement
93
99
  requirements:
94
- - - ">="
100
+ - - "~>"
95
101
  - !ruby/object:Gem::Version
96
102
  version: '3.8'
97
103
  - !ruby/object:Gem::Dependency
98
104
  name: rubocop
99
105
  requirement: !ruby/object:Gem::Requirement
100
106
  requirements:
101
- - - ">="
107
+ - - "~>"
102
108
  - !ruby/object:Gem::Version
103
- version: '0'
109
+ version: '0.68'
104
110
  type: :development
105
111
  prerelease: false
106
112
  version_requirements: !ruby/object:Gem::Requirement
107
113
  requirements:
108
- - - ">="
114
+ - - "~>"
109
115
  - !ruby/object:Gem::Version
110
- version: '0'
116
+ version: '0.68'
111
117
  - !ruby/object:Gem::Dependency
112
118
  name: simplecov
113
119
  requirement: !ruby/object:Gem::Requirement
114
120
  requirements:
115
- - - ">="
121
+ - - "~>"
116
122
  - !ruby/object:Gem::Version
117
123
  version: '0.16'
118
124
  type: :development
119
125
  prerelease: false
120
126
  version_requirements: !ruby/object:Gem::Requirement
121
127
  requirements:
122
- - - ">="
128
+ - - "~>"
123
129
  - !ruby/object:Gem::Version
124
130
  version: '0.16'
125
131
  description: FakerMaker is a simple fixture generator with a concise and straightforward
@@ -130,6 +136,7 @@ executables: []
130
136
  extensions: []
131
137
  extra_rdoc_files: []
132
138
  files:
139
+ - ".circleci/config.yml"
133
140
  - ".gitignore"
134
141
  - ".rspec"
135
142
  - ".rubocop.yml"
@@ -147,7 +154,6 @@ files:
147
154
  - lib/faker_maker/definition_proxy.rb
148
155
  - lib/faker_maker/factory.rb
149
156
  - lib/faker_maker/version.rb
150
- - test.rb
151
157
  homepage: https://github.com/BillyRuffian/faker_maker
152
158
  licenses:
153
159
  - MIT
data/test.rb DELETED
@@ -1,61 +0,0 @@
1
- FakerMaker.factory :Foo, class: 'FooBar' do
2
- bob {'hey'}
3
- end
4
-
5
- FakerMaker.factory :bar, parent: :Foo, class: 'BarFoo' do
6
- box {'ho'}
7
- bex { {foo: :bar } }
8
- end
9
-
10
- FakerMaker.factory :green_eggs_and_ham, class: 'Egg' do
11
- name { Faker::Name.name }
12
- date { Time.now }
13
- parameter( has: 2 ) { Faker::Company.bs }
14
- array( array: true ) { 'bang' }
15
- sub has: 0..2 do
16
- FakerMaker.build :Foo
17
- end
18
- blank { nil }
19
- end
20
-
21
- FakerMaker.factory :seuss, parent: :green_eggs_and_ham do
22
- quote { 'Today you are you! That is truer than true! There is no one alive who is you-er than you!' }
23
- end
24
-
25
- FakerMaker.factory :vehicle do
26
- wheels { 4 }
27
- colour { Faker::Color.color_name }
28
- engine_capacity( json: 'engineCapacity' ) { rand( 600..2500 ) }
29
- end
30
-
31
- FakerMaker.factory :motorbike, parent: :vehicle do
32
- wheels { 2 }
33
- sidecar { [true, false].sample }
34
- end
35
-
36
-
37
- FakerMaker.factory :item do
38
- name( json: 'productName' ) { Faker::Commerce.product_name }
39
- price( json: 'ticketPrice' ) { Faker::Commerce.price }
40
- end
41
-
42
-
43
- FakerMaker.factory :basket do
44
- items( has: 10 ) { FakerMaker[:item].build }
45
- end
46
-
47
- FakerMaker.factory :a do
48
- a( json: 'bigA' ) { 'a' }
49
- end
50
-
51
- FakerMaker.factory :b, parent: :a do
52
- b( json: 'bigB' ) { 'b' }
53
- end
54
-
55
- FakerMaker.factory :c, parent: :b do
56
- c( json: 'bigC' ) { 'c' }
57
- end
58
-
59
- FakerMaker.factory :d do
60
- label{ 'hello' }
61
- end