mokkku 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
+ SHA256:
3
+ metadata.gz: dea0bbaeb26c51558aa530f63a7a57ea1118d5d858c46b284b0aa0eefa43fe1c
4
+ data.tar.gz: 63c3ff3581a61bae6f77c9f20a11a56cd7bca662c4722f7284c8ac1523b0b4fb
5
+ SHA512:
6
+ metadata.gz: dc4da5ff3401d84a7c6340da54caa95a334fb548b7d33eb7194487492276c202c61bf17dc9eea471d982ea134076d5f821de7d4d23f248929cdbf67fb7a67369
7
+ data.tar.gz: a8a65b0ce61a7317d43261ec44e83a47344524c4226fa8f6d78d69f27544d703b0f8b501fbb6001765ff555aec60b27f1b34d637de1960cdc8fe697ce2902f76
@@ -0,0 +1,29 @@
1
+ module Mokkku
2
+ class MockedObject
3
+ def initialize(mocks)
4
+ @mocks = mocks
5
+ @selected_object = nil
6
+ end
7
+
8
+ def reset_context!
9
+ @selected_object = nil
10
+ end
11
+
12
+ private
13
+
14
+ def mocked_objects
15
+ @mocked_objects ||= @mocks.map do |mock_attrs|
16
+ Data.define(*mock_attrs.keys.map(&:to_sym)).new(**mock_attrs)
17
+ end
18
+ end
19
+
20
+ def method_missing(method_name, *args, &block)
21
+ if @selected_object.nil?
22
+ @selected_object = mocked_objects.sample(random: Mokkku::Random)
23
+ @selected_object.public_send(method_name)
24
+ else
25
+ @selected_object.public_send(method_name)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ require 'yaml'
2
+ require_relative 'mocked_object'
3
+
4
+ module Mokkku
5
+ module Utils
6
+ def const_missing(const_name)
7
+ if const_name.match?(/\:\:/)
8
+ super(const_name)
9
+ else
10
+ mock_file_name = const_name.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
11
+ mock_path = File.join(Mokkku.configuration.mocks_path, "#{mock_file_name}.yml")
12
+ data = File.read(mock_path)
13
+ parsed_data = YAML.safe_load(data, symbolize_names: true)
14
+ mocked_object = Mokkku::MockedObject.new(parsed_data)
15
+
16
+ const_set const_name, mocked_object
17
+
18
+ mocked_object
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ module Mokkku
2
+ module Version
3
+ module_function
4
+
5
+ def to_s
6
+ '0.0.1'
7
+ end
8
+ end
9
+ end
data/lib/mokkku.rb ADDED
@@ -0,0 +1,62 @@
1
+ require_relative 'mokkku/utils'
2
+
3
+ module Mokkku
4
+ extend Mokkku::Utils
5
+
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+
10
+ def self.configure
11
+ self.configuration ||= Configuration.new
12
+
13
+ yield(configuration)
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :mocks_path
18
+
19
+ def initialize
20
+ @mocks_path = './spec/mocks'
21
+ end
22
+ end
23
+
24
+ # Copied from FFaker gem - https://github.com/ffaker/ffaker
25
+ # Random Number Generator (RNG) used with ModuleUtils#fetch, #shuffle, #rand
26
+ # in order to provide deterministic repeatability.
27
+ module Random
28
+ # Returns the current RNG seed.
29
+ def self.seed
30
+ @seed ||= ::Random.new_seed
31
+ end
32
+
33
+ # Sets the RNG seed and creates a new internal RNG.
34
+ def self.seed=(new_seed)
35
+ @seed = new_seed
36
+ reset!
37
+ new_seed
38
+ end
39
+
40
+ # Reset the RNG back to its initial state.
41
+ def self.reset!
42
+ @rng = new_rng
43
+ end
44
+
45
+ # Returns a random number using an RNG with a known seed.
46
+ def self.rand(max = nil)
47
+ return rng.rand(max) if max
48
+
49
+ rng.rand
50
+ end
51
+
52
+ # Returns the current Random object.
53
+ def self.rng
54
+ @rng ||= new_rng
55
+ end
56
+
57
+ # Returns a new Random object instantiated with #seed.
58
+ def self.new_rng
59
+ ::Random.new(seed)
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mokkku
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Dąbrowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.7.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.7'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.7.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: pry
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Generate mock data that is unique for your application
48
+ email: contact@paweldabrowski.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/mokkku.rb
54
+ - lib/mokkku/mocked_object.rb
55
+ - lib/mokkku/utils.rb
56
+ - lib/mokkku/version.rb
57
+ homepage:
58
+ licenses: []
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.4.10
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Generate mock data that is unique for your application
79
+ test_files: []