jafry 0.1.1 → 0.2.0
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/.gitignore +1 -0
- data/Gemfile +3 -0
- data/README.md +7 -3
- data/jafry.gemspec +2 -2
- data/lib/jafry.rb +14 -13
- data/lib/jafry/accessor.rb +24 -0
- data/lib/jafry/builder.rb +68 -0
- data/lib/jafry/configurator.rb +64 -0
- data/lib/jafry/identificator.rb +143 -0
- data/lib/jafry/scheme.rb +24 -38
- data/lib/jafry/utilator.rb +88 -0
- data/lib/jafry/version.rb +1 -1
- data/spec/acceptance/config_spec.rb +7 -0
- data/spec/modules/accessor_spec.rb +13 -0
- data/spec/modules/builder_spec.rb +38 -0
- data/spec/modules/configurator_spec.rb +20 -0
- data/spec/modules/identificator_spec.rb +58 -0
- data/spec/modules/scheme_spec.rb +14 -0
- data/spec/modules/utilator_spec.rb +27 -0
- data/spec/spec_helper.rb +11 -1
- data/spec/support/filer.rb +7 -0
- data/test.json +1 -0
- metadata +24 -35
- data/lib/jafry/actions.rb +0 -63
- data/spec/jafry_spec.rb +0 -26
- data/spec/scheme_spec.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec06dcef0467623fe62f15c9f2df86ec4c8893ea
|
4
|
+
data.tar.gz: ba71ac4388445f3f2f88661497c51b541ae2da5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13ca8c4e473ec752093e90e508f7c396beac45955acabca2b5b98895a4426689fd938c6f8dbb24116ce1e3d24fc913cf6e11216b48b0ef043aa90ce5d7a5918d
|
7
|
+
data.tar.gz: 4121b4b9fcc89470b955be7b4501013061b20dbaad79f6559d7614bfeef0cbed1444eac089f41c6fce4833aa2361a0da1eea243be5e4e86862b200c00ac318ed
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
This gem allows you to easily create JSON data fixtures from data schemes you can specify by yourself.
|
8
8
|
The main purpose is to stub JSON received form wide range of JSON API's for testing.
|
9
9
|
And if you use many API's or kind of difficult one you may want to have a tool for easy creation of tons of JSON data you have.
|
10
|
-
And Jafry can
|
10
|
+
And Jafry can helps you here.
|
11
11
|
Just specify data scheme in json file and then you can change your data easly, wrap it namespaces, build lists, etc.
|
12
12
|
|
13
13
|
## Installation
|
@@ -24,12 +24,16 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
$ gem install jafry
|
26
26
|
|
27
|
+
## Versioning
|
28
|
+
|
29
|
+
Gem is under active development so version 0.2.0 has some breakable changes.
|
30
|
+
|
27
31
|
## Usage
|
28
32
|
|
29
33
|
1. Create file `data_scheme_name.json`.
|
30
34
|
2. Put json data in it.
|
31
35
|
3. In your `spec_helper.rb` require `jafry`: `require "jafry"`.
|
32
|
-
4. Call `Jafry.build('data_scheme_name')` to get object or `Jafry.create('data_scheme_name')` in your spec.
|
36
|
+
4. Call `Jafry.build('data_scheme_name')` to get object or `Jafry.create('data_scheme_name')` to get JSON in your spec.
|
33
37
|
|
34
38
|
## Contributing
|
35
39
|
|
@@ -37,4 +41,4 @@ Or install it yourself as:
|
|
37
41
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
42
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
43
|
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
-
5. Create a new Pull Request
|
44
|
+
5. Create a new Pull Request on `dev` branch
|
data/jafry.gemspec
CHANGED
@@ -23,8 +23,8 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.6"
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
spec.add_development_dependency "rspec"
|
26
|
-
spec.add_development_dependency "coveralls"
|
26
|
+
# spec.add_development_dependency "coveralls"
|
27
27
|
spec.add_development_dependency "yajl-ruby"
|
28
28
|
spec.add_development_dependency "hashie"
|
29
|
-
spec.add_development_dependency "yard"
|
29
|
+
# spec.add_development_dependency "yard"
|
30
30
|
end
|
data/lib/jafry.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
require 'jafry/version'
|
2
|
-
require 'jafry/
|
2
|
+
require 'jafry/builder'
|
3
|
+
require 'jafry/accessor'
|
3
4
|
|
4
5
|
# Main Jafry module
|
5
|
-
#
|
6
|
-
# @attr [String] schemes_path Path to Jafry schemes
|
7
|
-
|
8
6
|
module Jafry
|
9
|
-
|
10
|
-
|
11
|
-
#
|
7
|
+
# @attr [String] schemes_path Path to Jafry schemes
|
8
|
+
# @attr [String] id_wrapper Defines what symbols will be used for scheme's id
|
9
|
+
# @attr [String] id_type Defines type of scheme's id
|
10
|
+
# @option id_type [String] 'number' Usually decimial number
|
11
|
+
# @option id_type [String] 'hash' 32-digit unique hash
|
12
|
+
extend Builder
|
13
|
+
extend Accessor
|
12
14
|
|
13
|
-
|
14
|
-
@@schemes_path
|
15
|
-
end
|
15
|
+
mattrs :schemes_path, :id_wrapper, :id_type
|
16
16
|
|
17
|
-
|
17
|
+
@@id_wrapper = 'id'
|
18
|
+
@@id_type = 'number'
|
18
19
|
|
19
|
-
def self.
|
20
|
-
|
20
|
+
def self.setup
|
21
|
+
yield self
|
21
22
|
end
|
22
23
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Jafry
|
2
|
+
# Module providing getters/setters for module level variables
|
3
|
+
# @since 0.2.0
|
4
|
+
module Accessor
|
5
|
+
# Method for creating getters/setters by attribute name
|
6
|
+
#
|
7
|
+
# @param [Array] args Attributes
|
8
|
+
def mattrs(*args)
|
9
|
+
args.each do |attr|
|
10
|
+
class_eval(%Q(
|
11
|
+
def self.#{attr}=(attr)
|
12
|
+
@@#{attr}=attr
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.#{attr}
|
16
|
+
@@#{attr}
|
17
|
+
end
|
18
|
+
)
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'jafry/scheme'
|
2
|
+
require 'jafry/utilator'
|
3
|
+
|
4
|
+
module Jafry
|
5
|
+
# Module for factoring schemes
|
6
|
+
# @since 0.1.0
|
7
|
+
module Builder
|
8
|
+
|
9
|
+
include Utilator
|
10
|
+
# Return object builded from scheme
|
11
|
+
#
|
12
|
+
# @param scheme_name [String] Scheme name
|
13
|
+
# @param [Hash] options Options
|
14
|
+
# @option options [String] :wrap Namespace to wrap scheme in
|
15
|
+
# @return [Scheme] New Scheme object build with pointed options
|
16
|
+
# @see Scheme
|
17
|
+
|
18
|
+
def build(scheme_name, options={})
|
19
|
+
scheme = Scheme.new(parse_scheme(scheme_name))
|
20
|
+
scheme.update(options) if options
|
21
|
+
scheme.self_name = scheme_name
|
22
|
+
Jafry::Configurator.set_config(scheme_name) unless Jafry::Configurator.find_config(scheme_name)
|
23
|
+
Jafry::Identificator.set_id_for_scheme(scheme)
|
24
|
+
return wrap(options[:wrap], scheme) if options[:wrap]
|
25
|
+
scheme
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return json from scheme
|
29
|
+
#
|
30
|
+
# @param scheme_name [String] Scheme name
|
31
|
+
# @param options [Hash] Options
|
32
|
+
# @option options [String] :wrap Namespace to wrap json in
|
33
|
+
# @return [Scheme] New Scheme object encoded to json
|
34
|
+
# @see Scheme
|
35
|
+
|
36
|
+
def create(scheme_name, options={})
|
37
|
+
encode_json(build(scheme_name, options))
|
38
|
+
end
|
39
|
+
|
40
|
+
# Retun array of Scheme objects
|
41
|
+
#
|
42
|
+
# @param scheme_name [String] Scheme name
|
43
|
+
# @param count [Integer] Number of objects in array
|
44
|
+
# @param options [Hash] Options
|
45
|
+
# @option options [String] :wrap Namespace to wrap list in
|
46
|
+
# @return [Array] Array of Schemes
|
47
|
+
# @see Scheme
|
48
|
+
|
49
|
+
def build_list(scheme_name, count, options={})
|
50
|
+
list = []
|
51
|
+
count.times {list << build(scheme_name, options)}
|
52
|
+
list
|
53
|
+
end
|
54
|
+
|
55
|
+
# Retun array of Scheme objects encoded to json
|
56
|
+
#
|
57
|
+
# @param scheme_name [String] Scheme name
|
58
|
+
# @param count [Integer] Number of objects in array
|
59
|
+
# @param options [Hash] Options
|
60
|
+
# @option options [String] :wrap Namespace to wrap json list in
|
61
|
+
# @return [Array] Json array of schemes
|
62
|
+
# @see Scheme
|
63
|
+
|
64
|
+
def create_list(scheme_name, count, options={})
|
65
|
+
encode_json(build_list(scheme_name, count, options))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'jafry/accessor'
|
2
|
+
|
3
|
+
module Jafry
|
4
|
+
# Module for saving and changing scheme's configs
|
5
|
+
# @since 0.2.0
|
6
|
+
module Configurator
|
7
|
+
|
8
|
+
extend Accessor
|
9
|
+
|
10
|
+
@@schemes_configs = []
|
11
|
+
|
12
|
+
mattrs :schemes_configs
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# Setups config for scheme
|
16
|
+
#
|
17
|
+
# @param scheme_name [String] Scheme name
|
18
|
+
# @param [Hash] config Config
|
19
|
+
|
20
|
+
def set_config(scheme_name, config={})
|
21
|
+
config = default if config.empty?
|
22
|
+
scheme_config = init_or_update(scheme_name, config)
|
23
|
+
self.schemes_configs << scheme_config
|
24
|
+
scheme_config
|
25
|
+
end
|
26
|
+
|
27
|
+
# Creates new or updates existed config
|
28
|
+
#
|
29
|
+
# @param scheme_name [String] Scheme name
|
30
|
+
# @param [Hash] config Config to set/update
|
31
|
+
|
32
|
+
def init_or_update(scheme_name, config)
|
33
|
+
return find_config(scheme_name)[:config].merge!(config) if find_config(scheme_name)
|
34
|
+
{scheme: scheme_name, config: config}
|
35
|
+
end
|
36
|
+
|
37
|
+
# Finds config by scheme name
|
38
|
+
#
|
39
|
+
# @param scheme [String] Scheme name
|
40
|
+
# @return [Hash] Config
|
41
|
+
|
42
|
+
def get_config(scheme)
|
43
|
+
find_config(scheme)[:config]
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns scheme by name
|
47
|
+
#
|
48
|
+
# @param scheme_name [String] Scheme name
|
49
|
+
# @return [Hash] Hash of scheme name and config
|
50
|
+
|
51
|
+
def find_config(scheme_name)
|
52
|
+
self.schemes_configs.select {|item| item[:scheme] == scheme_name}.last
|
53
|
+
end
|
54
|
+
|
55
|
+
# Specifies default config
|
56
|
+
#
|
57
|
+
# @return [Hash] Default config
|
58
|
+
|
59
|
+
def default
|
60
|
+
{id_type: Jafry.id_type, id_wrapper: Jafry.id_wrapper}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'jafry/accessor'
|
2
|
+
require 'jafry/configurator'
|
3
|
+
|
4
|
+
module Jafry
|
5
|
+
# Module for automaticaly creatings schemes ids
|
6
|
+
# @since 0.2.0
|
7
|
+
module Identificator
|
8
|
+
|
9
|
+
extend Accessor
|
10
|
+
|
11
|
+
@@schemes = []
|
12
|
+
|
13
|
+
mattrs :schemes
|
14
|
+
|
15
|
+
class << self
|
16
|
+
# Creates identification record about scheme
|
17
|
+
#
|
18
|
+
# @param scheme_name [String] Scheme name
|
19
|
+
# @return [Hash] Identification information about scheme
|
20
|
+
|
21
|
+
def register_scheme(scheme_name)
|
22
|
+
config = Jafry::Configurator.get_config(scheme_name)
|
23
|
+
config = Jafry::Configurator.set_config(scheme_name) unless config
|
24
|
+
scheme = build_type(config[:id_type]).scheme_scaffold(scheme_name)
|
25
|
+
self.schemes << scheme
|
26
|
+
scheme
|
27
|
+
end
|
28
|
+
|
29
|
+
# Finds registered scheme
|
30
|
+
#
|
31
|
+
# @param scheme_name [String] Scheme name
|
32
|
+
# @return [Hash] Identification information about scheme
|
33
|
+
|
34
|
+
def find_scheme(scheme_name)
|
35
|
+
self.schemes.select {|item| item[:scheme] == scheme_name}.last
|
36
|
+
end
|
37
|
+
|
38
|
+
# Finds registered scheme and if there is no one, creates new
|
39
|
+
#
|
40
|
+
# @param scheme_name [String] Scheme name
|
41
|
+
# @return [Hash] Identification information about scheme
|
42
|
+
|
43
|
+
def find_or_register(scheme_name)
|
44
|
+
return find_scheme(scheme_name) if find_scheme(scheme_name)
|
45
|
+
register_scheme(scheme_name)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Modifies scheme with current id
|
49
|
+
#
|
50
|
+
# @param scheme [Scheme] Scheme instance
|
51
|
+
# @return [Scheme] Scheme with id
|
52
|
+
|
53
|
+
def set_id_for_scheme(scheme)
|
54
|
+
scheme.merge!(generate_id(scheme))
|
55
|
+
end
|
56
|
+
|
57
|
+
# Creates new scheme id type
|
58
|
+
#
|
59
|
+
# @param type [String] Id type
|
60
|
+
# @return [Type] Type of id
|
61
|
+
|
62
|
+
def build_type(type)
|
63
|
+
const_get("Jafry::Identificator::#{type.capitalize}").new
|
64
|
+
end
|
65
|
+
|
66
|
+
# Generates id
|
67
|
+
#
|
68
|
+
# @param [Scheme] scheme Scheme instance
|
69
|
+
|
70
|
+
def generate_id(scheme)
|
71
|
+
config = Jafry::Configurator.get_config(scheme.self_name)
|
72
|
+
build_type(config[:id_type]).id_scaffold(config[:id_wrapper], scheme)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Increment id counter by 1
|
76
|
+
#
|
77
|
+
# @param [Scheme] scheme Scheme instance
|
78
|
+
|
79
|
+
def inc_scheme_counter(scheme)
|
80
|
+
find_or_register(scheme.self_name)[:counter] += 1
|
81
|
+
end
|
82
|
+
|
83
|
+
# Creates hash id
|
84
|
+
#
|
85
|
+
# @param [Scheme] scheme Scheme instance
|
86
|
+
|
87
|
+
def hash_counter(scheme)
|
88
|
+
find_or_register(scheme.self_name)[:counter] = generate_hash
|
89
|
+
end
|
90
|
+
|
91
|
+
# Helper method for generate unique hash
|
92
|
+
#
|
93
|
+
# @return [String] Unique md5 hash
|
94
|
+
|
95
|
+
def generate_hash
|
96
|
+
Digest::MD5.hexdigest([Time.now, rand].join)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class Number
|
101
|
+
# Class for scheme id type 'number'
|
102
|
+
# @since 0.2.0
|
103
|
+
|
104
|
+
# Specify hash for basic scheme id type
|
105
|
+
#
|
106
|
+
# @return [Hash] Hash with basic id
|
107
|
+
|
108
|
+
def scheme_scaffold(scheme_name)
|
109
|
+
{scheme: scheme_name, counter: 0}
|
110
|
+
end
|
111
|
+
|
112
|
+
# Specify hash for basic scheme id type with custom id wrapper
|
113
|
+
#
|
114
|
+
# @return [Hash] Hash with basic id
|
115
|
+
|
116
|
+
def id_scaffold(wrapper, scheme)
|
117
|
+
{"#{wrapper}" => Identificator::inc_scheme_counter(scheme)}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class Hash
|
122
|
+
# Class for scheme id type 'hash'
|
123
|
+
# @since 0.2.0
|
124
|
+
|
125
|
+
# Specify hash for basic scheme id type
|
126
|
+
#
|
127
|
+
# @return [Hash] Hash with basic id
|
128
|
+
|
129
|
+
def scheme_scaffold(scheme_name)
|
130
|
+
{scheme: scheme_name, counter: generate_hash}
|
131
|
+
end
|
132
|
+
|
133
|
+
# Specify hash for basic scheme id type with custom id wrapper
|
134
|
+
#
|
135
|
+
# @return [Hash] Hash with basic id
|
136
|
+
|
137
|
+
def id_scaffold(wrapper, scheme)
|
138
|
+
{"#{wrapper}" => Identificator::hash_counter(scheme)}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
data/lib/jafry/scheme.rb
CHANGED
@@ -1,48 +1,34 @@
|
|
1
|
-
require 'yajl'
|
2
1
|
require 'hashie'
|
2
|
+
require 'jafry/identificator'
|
3
|
+
require 'jafry/utilator'
|
3
4
|
|
4
|
-
# Class for parsing json scheme
|
5
5
|
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# @raise [Exception] Raises when json scheme file not found
|
6
|
+
module Jafry
|
7
|
+
# Class for parsing json scheme
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
raise "Please, create correct scheme file first" unless File.exists?(build_schemes_path(path))
|
14
|
-
super(Yajl::Parser.new.parse(File.read(build_schemes_path(path))))
|
15
|
-
rescue Exception => e
|
16
|
-
puts e.message
|
17
|
-
puts e.backtrace
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Return json from scheme
|
22
|
-
#
|
23
|
-
# @return [String] JSON from scheme
|
24
|
-
|
25
|
-
def build
|
26
|
-
Yajl::Encoder.encode self
|
27
|
-
end
|
9
|
+
class Scheme < Hashie::Mash
|
10
|
+
include Utilator
|
28
11
|
|
29
|
-
|
30
|
-
#
|
31
|
-
# @return [Hash] Wrapped hash
|
12
|
+
attr_accessor :self_name
|
32
13
|
|
33
|
-
|
34
|
-
|
35
|
-
|
14
|
+
# Return json from scheme
|
15
|
+
#
|
16
|
+
# @return [String] JSON from scheme
|
36
17
|
|
37
|
-
|
18
|
+
def build
|
19
|
+
encode_json self
|
20
|
+
end
|
38
21
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
# @return [String] Full path to file
|
22
|
+
# Updates scheme with specified options
|
23
|
+
#
|
24
|
+
# @param options [Hash] Options to update
|
25
|
+
# @return [Scheme] Updated scheme
|
44
26
|
|
45
|
-
|
46
|
-
|
27
|
+
def update(options)
|
28
|
+
options.each do |attr, value|
|
29
|
+
self[attr] = value if self[attr]
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
47
33
|
end
|
48
|
-
end
|
34
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'yajl'
|
2
|
+
|
3
|
+
module Jafry
|
4
|
+
# Bunch of functions to work with encode/decode JSON and files
|
5
|
+
# @since 0.2.0
|
6
|
+
|
7
|
+
module Utilator
|
8
|
+
private
|
9
|
+
# Method for creating full path to scheme file
|
10
|
+
#
|
11
|
+
# @private
|
12
|
+
# @param scheme [String] Scheme name
|
13
|
+
# @return [String] Full path to file
|
14
|
+
|
15
|
+
def build_schemes_path(scheme)
|
16
|
+
"#{Jafry.schemes_path}/#{scheme}.json".gsub(/\/{2,}/, '/')
|
17
|
+
end
|
18
|
+
|
19
|
+
# Method for check scheme file existance
|
20
|
+
#
|
21
|
+
# @private
|
22
|
+
# @param scheme_name [String] Scheme name
|
23
|
+
# @return [Boolean] True or false
|
24
|
+
|
25
|
+
def scheme_file_exists?(scheme_name)
|
26
|
+
File.exists?(build_schemes_path(scheme_name))
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify message will be shown when scheme file not exist
|
30
|
+
#
|
31
|
+
# @private
|
32
|
+
# @return [String] Message
|
33
|
+
|
34
|
+
def not_exists_message
|
35
|
+
"Please, create correct scheme file first"
|
36
|
+
end
|
37
|
+
|
38
|
+
# Parses JSON
|
39
|
+
#
|
40
|
+
# @private
|
41
|
+
# @param [String] json JSON to parse
|
42
|
+
# @return [Hash] Parsed json
|
43
|
+
|
44
|
+
def parse_json(json)
|
45
|
+
Yajl::Parser.new.parse(json)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Encodes JSON
|
49
|
+
#
|
50
|
+
# @private
|
51
|
+
# @param [Hash] hash Hash to convert to JSON
|
52
|
+
# @return [String] Parsed json
|
53
|
+
|
54
|
+
def encode_json(hash)
|
55
|
+
Yajl::Encoder.encode hash
|
56
|
+
end
|
57
|
+
|
58
|
+
# Creates Hash from JSON-based scheme file
|
59
|
+
#
|
60
|
+
# @private
|
61
|
+
# @param [String] file_name Scheme file name
|
62
|
+
# @return [Hash] Parsed scheme
|
63
|
+
|
64
|
+
def parse_scheme(file_name)
|
65
|
+
begin
|
66
|
+
raise not_exists_message unless scheme_file_exists?(file_name)
|
67
|
+
parse_json(File.read(build_schemes_path(file_name)))
|
68
|
+
rescue Exception => e
|
69
|
+
puts e.message
|
70
|
+
puts e.backtrace
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Wraps hash in specified wrapper
|
75
|
+
#
|
76
|
+
# @private
|
77
|
+
# @param [String] wrapper Namespace hash will be wrapped in
|
78
|
+
# @param [Hash] hash Hash to wrap
|
79
|
+
# @return [Hash] Wrapped hash
|
80
|
+
#
|
81
|
+
# @example Wrap
|
82
|
+
# wrap('baz', {foo: 'bar'}) #=> {'baz' => {foo: 'bar'}}
|
83
|
+
|
84
|
+
def wrap(wrapper, hash)
|
85
|
+
{"#{wrapper}" => hash}
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/jafry/version.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jafry::Accessor do
|
4
|
+
subject(:stub_module) { Module.new { extend Jafry::Accessor; mattrs :foo; } }
|
5
|
+
|
6
|
+
it {is_expected.to respond_to(:foo)}
|
7
|
+
it {is_expected.to respond_to(:foo=)}
|
8
|
+
|
9
|
+
context "sets value" do
|
10
|
+
before {stub_module.foo = "bar"}
|
11
|
+
it {expect(stub_module.foo).to eq('bar')}
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jafry::Builder do
|
4
|
+
before(:each) do
|
5
|
+
write_file('test.json', %Q({"name":"test"}))
|
6
|
+
end
|
7
|
+
|
8
|
+
after(:each) do
|
9
|
+
Jafry::Identificator.schemes.clear
|
10
|
+
delete_file('test.json')
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:stub_obj) { Class.new { include Jafry::Builder }.new }
|
14
|
+
|
15
|
+
it '#build' do
|
16
|
+
expect(stub_obj.build('test')).to eq({"id" => 1, "name" => "test"})
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'builds scheme with options' do
|
20
|
+
expect(stub_obj.build('test', {name: "foo", wrap: "data"})).to eq({"data" => {"id" => 1, "name" => "foo"}})
|
21
|
+
end
|
22
|
+
|
23
|
+
it '#create' do
|
24
|
+
expect(stub_obj.create('test')).to eq("{\"name\":\"test\",\"id\":1}")
|
25
|
+
end
|
26
|
+
|
27
|
+
it '#build_list' do
|
28
|
+
expect(stub_obj.build_list('test', 2)).to eq([{"id" => 1, "name" => "test"}, {"id" => 2, "name" => "test"}])
|
29
|
+
end
|
30
|
+
|
31
|
+
it '#create_list' do
|
32
|
+
expect(stub_obj.create_list('test', 2)).to eq("[{\"name\":\"test\",\"id\":1},{\"name\":\"test\",\"id\":2}]")
|
33
|
+
end
|
34
|
+
|
35
|
+
context "from unexisting file" do
|
36
|
+
it {expect { stub_obj.build('foo') }.to output(/Please, create correct scheme file first/).to_stdout}
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jafry::Configurator do
|
4
|
+
before{described_class.schemes_configs.clear}
|
5
|
+
it ".set_config" do
|
6
|
+
expect(described_class.set_config('foo')).to eq({scheme: 'foo', config: {id_type: 'number', id_wrapper: 'id'}})
|
7
|
+
expect(described_class.set_config('test', {id_type: 'bar', id_wrapper: 'baz'})).to eq({scheme: 'test', config: {id_type: 'bar', id_wrapper: 'baz'}})
|
8
|
+
expect(described_class.schemes_configs.size).to eq(2)
|
9
|
+
end
|
10
|
+
|
11
|
+
it ".get_config" do
|
12
|
+
described_class.set_config('foo')
|
13
|
+
expect(described_class.get_config('foo')).to eq({id_type: 'number', id_wrapper: 'id'})
|
14
|
+
end
|
15
|
+
|
16
|
+
it "find_config" do
|
17
|
+
described_class.set_config('foo')
|
18
|
+
expect(described_class.find_config('foo')).to eq({scheme: 'foo', config: {id_type: 'number', id_wrapper: 'id'}})
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jafry::Identificator do
|
4
|
+
let(:scheme) {Jafry::Scheme.new({name: "test"})}
|
5
|
+
|
6
|
+
context "with unregistered scheme" do
|
7
|
+
it {expect(described_class.schemes).to be_empty}
|
8
|
+
end
|
9
|
+
|
10
|
+
context "with registered scheme" do
|
11
|
+
before(:each) do
|
12
|
+
scheme.self_name = 'test'
|
13
|
+
Jafry::Configurator.set_config('test')
|
14
|
+
described_class.register_scheme('foo')
|
15
|
+
end
|
16
|
+
|
17
|
+
it '.register_scheme' do
|
18
|
+
expect(described_class.schemes.last).to eq({scheme: 'foo', counter: 0})
|
19
|
+
end
|
20
|
+
|
21
|
+
it ".find_scheme" do
|
22
|
+
expect(described_class.find_scheme('foo')).to eq({scheme: 'foo', counter: 0})
|
23
|
+
end
|
24
|
+
|
25
|
+
it ".find_scheme_or_register" do
|
26
|
+
expect(described_class.find_or_register('foo')).to eq({scheme: 'foo', counter: 0})
|
27
|
+
Jafry::Configurator.set_config('bar')
|
28
|
+
described_class.find_or_register('bar')
|
29
|
+
expect(described_class.schemes).to include({scheme: 'bar', counter: 0})
|
30
|
+
end
|
31
|
+
|
32
|
+
it ".set_id_for_scheme" do
|
33
|
+
|
34
|
+
described_class.set_id_for_scheme(scheme)
|
35
|
+
expect(scheme.id).to eq(1)
|
36
|
+
end
|
37
|
+
|
38
|
+
context "sets with another id_wrapper" do
|
39
|
+
before do
|
40
|
+
scheme.self_name = 'test'
|
41
|
+
Jafry::Configurator.set_config('test', {id_wrapper: 'foo_id'})
|
42
|
+
described_class.set_id_for_scheme(scheme)
|
43
|
+
end
|
44
|
+
|
45
|
+
it {expect(scheme).to respond_to(:foo_id)}
|
46
|
+
end
|
47
|
+
|
48
|
+
context "sets with another id_type" do
|
49
|
+
before do
|
50
|
+
scheme.self_name = 'test'
|
51
|
+
Jafry::Configurator.set_config('test', {id_type: 'hash'})
|
52
|
+
described_class.set_id_for_scheme(scheme)
|
53
|
+
end
|
54
|
+
|
55
|
+
it {expect(scheme.id.size).to eq(32)}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jafry::Scheme do
|
4
|
+
|
5
|
+
subject(:scheme) {described_class.new({name: "test"})}
|
6
|
+
|
7
|
+
it '#build' do
|
8
|
+
expect(scheme.build).to eq(%Q({"name":"test"}))
|
9
|
+
end
|
10
|
+
|
11
|
+
it '#update' do
|
12
|
+
expect(scheme.update({name: "foo"})).to eq({'name'=>"foo"})
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jafry::Utilator do
|
4
|
+
let(:stub_obj) { Class.new { include Jafry::Utilator }.new }
|
5
|
+
|
6
|
+
it '#build_schemes_path' do
|
7
|
+
expect(stub_obj.send(:build_schemes_path, 'foo')).to eq("./foo.json")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "#scheme_file_exists?" do
|
11
|
+
expect(stub_obj.send(:scheme_file_exists?, 'foo')).to eq(false)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "#parse_json" do
|
15
|
+
expect(stub_obj.send(:parse_json, "{\"foo\":\"bar\"}")).to eq({"foo" => "bar"})
|
16
|
+
end
|
17
|
+
|
18
|
+
it "#encode_json" do
|
19
|
+
expect(stub_obj.send(:encode_json, {"foo" => "bar"})).to eq("{\"foo\":\"bar\"}")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "#parse_scheme" do
|
23
|
+
expect { stub_obj.send(:parse_scheme, 'foo') }.to output(/Please, create correct scheme file first/).to_stdout
|
24
|
+
write_file("test.json", %Q({"name":"test"}))
|
25
|
+
expect(stub_obj.send(:parse_scheme, 'test')).to eq({"name" => "test"})
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
+
Dir[Dir.pwd + '/spec/support/**/*.rb'].each {|f| puts f;require f;}
|
2
|
+
|
1
3
|
require 'coveralls'
|
2
4
|
Coveralls.wear!
|
3
5
|
|
4
|
-
require 'jafry'
|
6
|
+
require 'jafry'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.before(:all) do
|
10
|
+
Jafry.schemes_path = './'
|
11
|
+
Jafry.id_wrapper = 'id'
|
12
|
+
Jafry.id_type = 'number'
|
13
|
+
end
|
14
|
+
end
|
data/test.json
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"name":"test"}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jafry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: coveralls
|
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
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: yajl-ruby
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,20 +80,6 @@ dependencies:
|
|
94
80
|
- - ">="
|
95
81
|
- !ruby/object:Gem::Version
|
96
82
|
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: yard
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
83
|
description: |-
|
112
84
|
This gem allows you to easily create JSON data fixtures
|
113
85
|
from data schemes you can specify by yourself.
|
@@ -127,12 +99,23 @@ files:
|
|
127
99
|
- Rakefile
|
128
100
|
- jafry.gemspec
|
129
101
|
- lib/jafry.rb
|
130
|
-
- lib/jafry/
|
102
|
+
- lib/jafry/accessor.rb
|
103
|
+
- lib/jafry/builder.rb
|
104
|
+
- lib/jafry/configurator.rb
|
105
|
+
- lib/jafry/identificator.rb
|
131
106
|
- lib/jafry/scheme.rb
|
107
|
+
- lib/jafry/utilator.rb
|
132
108
|
- lib/jafry/version.rb
|
133
|
-
- spec/
|
134
|
-
- spec/
|
109
|
+
- spec/acceptance/config_spec.rb
|
110
|
+
- spec/modules/accessor_spec.rb
|
111
|
+
- spec/modules/builder_spec.rb
|
112
|
+
- spec/modules/configurator_spec.rb
|
113
|
+
- spec/modules/identificator_spec.rb
|
114
|
+
- spec/modules/scheme_spec.rb
|
115
|
+
- spec/modules/utilator_spec.rb
|
135
116
|
- spec/spec_helper.rb
|
117
|
+
- spec/support/filer.rb
|
118
|
+
- test.json
|
136
119
|
homepage: ''
|
137
120
|
licenses:
|
138
121
|
- MIT
|
@@ -158,7 +141,13 @@ signing_key:
|
|
158
141
|
specification_version: 4
|
159
142
|
summary: Gem for creating JSON data stubs
|
160
143
|
test_files:
|
161
|
-
- spec/
|
162
|
-
- spec/
|
144
|
+
- spec/acceptance/config_spec.rb
|
145
|
+
- spec/modules/accessor_spec.rb
|
146
|
+
- spec/modules/builder_spec.rb
|
147
|
+
- spec/modules/configurator_spec.rb
|
148
|
+
- spec/modules/identificator_spec.rb
|
149
|
+
- spec/modules/scheme_spec.rb
|
150
|
+
- spec/modules/utilator_spec.rb
|
163
151
|
- spec/spec_helper.rb
|
152
|
+
- spec/support/filer.rb
|
164
153
|
has_rdoc:
|
data/lib/jafry/actions.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'yajl'
|
2
|
-
require 'jafry/scheme'
|
3
|
-
|
4
|
-
# This module contains main functionality for Jafry
|
5
|
-
|
6
|
-
module Actions
|
7
|
-
# Return object builded from scheme
|
8
|
-
|
9
|
-
# @param scheme_name [String] Scheme name
|
10
|
-
# @param [Hash] options Options
|
11
|
-
# @option options [String] :wrap Namespace to wrap scheme in
|
12
|
-
# @return [Scheme] New Scheme object build with pointed options
|
13
|
-
# @see Scheme
|
14
|
-
|
15
|
-
def build(scheme_name, options={})
|
16
|
-
scheme = Scheme.new(scheme_name)
|
17
|
-
options.each do |attr, value|
|
18
|
-
scheme[attr] = value if scheme[attr]
|
19
|
-
end
|
20
|
-
return scheme.wrap(options[:wrap]) if options[:wrap]
|
21
|
-
scheme
|
22
|
-
end
|
23
|
-
|
24
|
-
# Return json from scheme
|
25
|
-
|
26
|
-
# @param scheme_name [String] Scheme name
|
27
|
-
# @param options [Hash] Options
|
28
|
-
# @option options [String] :wrap Namespace to wrap json in
|
29
|
-
# @return [Scheme] New Scheme object encoded to json
|
30
|
-
# @see Scheme
|
31
|
-
|
32
|
-
def create(scheme_name, options={})
|
33
|
-
Yajl::Encoder.encode(build(scheme_name, options))
|
34
|
-
end
|
35
|
-
|
36
|
-
# Retun array of Scheme objects
|
37
|
-
|
38
|
-
# @param scheme_name [String] Scheme name
|
39
|
-
# @param count [Integer] Number of objects in array
|
40
|
-
# @param options [Hash] Options
|
41
|
-
# @option options [String] :wrap Namespace to wrap list in
|
42
|
-
# @return [Array] Array of Schemes
|
43
|
-
# @see Scheme
|
44
|
-
|
45
|
-
def build_list(scheme_name, count, options={})
|
46
|
-
list = []
|
47
|
-
count.times {list << build(scheme_name, options)}
|
48
|
-
list
|
49
|
-
end
|
50
|
-
|
51
|
-
# Retun array of Scheme objects encoded to json
|
52
|
-
|
53
|
-
# @param scheme_name [String] Scheme name
|
54
|
-
# @param count [Integer] Number of objects in array
|
55
|
-
# @param options [Hash] Options
|
56
|
-
# @option options [String] :wrap Namespace to wrap json list in
|
57
|
-
# @return [Array] Json array of schemes
|
58
|
-
# @see Scheme
|
59
|
-
|
60
|
-
def create_list(scheme_name, count, options={})
|
61
|
-
Yajl::Encoder.encode(build_list(scheme_name, count, options))
|
62
|
-
end
|
63
|
-
end
|
data/spec/jafry_spec.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Jafry do
|
4
|
-
before(:each) do
|
5
|
-
File.open("test.json", 'w+') {|f| f.write(%Q({"name":"test"})) }
|
6
|
-
Jafry.schemes_path = './'
|
7
|
-
end
|
8
|
-
after(:each) do
|
9
|
-
File.delete("test.json")
|
10
|
-
end
|
11
|
-
it 'builds scheme' do
|
12
|
-
expect(Jafry.build('test')).to eq({"name" => "test"})
|
13
|
-
end
|
14
|
-
it 'builds scheme with options' do
|
15
|
-
expect(Jafry.build('test', {name: "foo", wrap: "data"})).to eq({"data" => {"name" => "foo"}})
|
16
|
-
end
|
17
|
-
it 'creates json from scheme' do
|
18
|
-
expect(Jafry.create('test')).to eq(%Q({"name":"test"}))
|
19
|
-
end
|
20
|
-
it 'builds list' do
|
21
|
-
expect(Jafry.build_list('test', 2)).to eq([{"name" => "test"}, {"name" => "test"}])
|
22
|
-
end
|
23
|
-
it 'creates json list' do
|
24
|
-
expect(Jafry.create_list('test', 2)).to eq("[{\"name\":\"test\"},{\"name\":\"test\"}]")
|
25
|
-
end
|
26
|
-
end
|
data/spec/scheme_spec.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Scheme do
|
4
|
-
before(:all) {Jafry.schemes_path = '.'}
|
5
|
-
context "from existing file" do
|
6
|
-
before(:each) do
|
7
|
-
File.open("test.json", 'w+') {|f| f.write(%Q({"name":"test"})) }
|
8
|
-
@scheme = Scheme.new('test')
|
9
|
-
end
|
10
|
-
after(:each) do
|
11
|
-
File.delete("test.json")
|
12
|
-
end
|
13
|
-
it 'initializes from file' do
|
14
|
-
expect(@scheme.name).to eq('test')
|
15
|
-
end
|
16
|
-
it 'builds json' do
|
17
|
-
expect(@scheme.build).to eq(%Q({"name":"test"}))
|
18
|
-
end
|
19
|
-
it 'wraps json' do
|
20
|
-
expect(@scheme.wrap("user")).to eq({"user" => {"name" => "test"}})
|
21
|
-
end
|
22
|
-
it 'builds schemes path' do
|
23
|
-
expect(@scheme.send(:build_schemes_path, 'foo')).to eq("./foo.json")
|
24
|
-
end
|
25
|
-
end
|
26
|
-
context "from unexisting file" do
|
27
|
-
it "returns `create file` caution" do
|
28
|
-
expect { Scheme.new('foo') }.to output(/Please, create correct scheme file first/).to_stdout
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|