data_magic 1.1 → 1.2

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
  SHA1:
3
- metadata.gz: e67b1c5429e8be13be3e344f6ab716a89a26fef2
4
- data.tar.gz: 31127c9fe43eb08a7c92abf59a11516d97fce262
3
+ metadata.gz: e231dc9f35e689aab2f2e5558e962db909db8a54
4
+ data.tar.gz: 5756c7b84233f4bec53aa8b99bf1cf932cc55ba3
5
5
  SHA512:
6
- metadata.gz: 625b4ddb46b5f191f1e1dd6f8daa547368385596253bccf090ed704a9e1b79701bce94135dadbba670c805726e1285e7b79461492e5b8db628c58cf7866625e4
7
- data.tar.gz: 18ccb17574943c342f5cbe5aad0fb66f364ef2b1a48dd7c94ef66dedec822a9c3de97f31e15b1a8fabed4d6de4135c387063859f0539a13e72cbd15a8c8ade4b
6
+ metadata.gz: 67be3b00d42169891f063df7f0ec40cdf26811958855cb094e2a345e25ea6afe7f642aa7c55ce671f8cad2250b74fa1d92a71bc238796dde343b0eb203902a82
7
+ data.tar.gz: 3735825cd564df1788fe528b2bfca277afc147b859d9d91a26eadf3dcf8da8dc6916b39bd1cb5433eed35e91446b7546b6ebb08f066dad9d7eedb764fc506c11
@@ -1 +1 @@
1
- ruby-2.1.2
1
+ ruby-2.2.7
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ == Version 1.2 / 2017-09-23
2
+ * Enhancements
3
+ * Data is translated each time a request is made for data (Thanks Vveliev)
4
+ * Array handling and translation (Thanks Vveliev)
5
+
1
6
  == Version 1.1 / 2016-08-03
2
7
  * Fixes
3
8
  * Update so it works well on Ruby 2.4 (thanks Robert MacCracken)
data/README.md CHANGED
@@ -136,7 +136,9 @@ The rdocs for this project can be found at [rubydoc.info](http://rubydoc.info/gi
136
136
 
137
137
  To see the changes from release to release please look at the [ChangeLog](https://raw.github.com/cheezy/data_magic/master/ChangeLog)
138
138
 
139
+ ## Older versions of Ruby:
139
140
 
141
+ This gem only works with Ruby >= 2.2. Use versions 1.1 or below if you have an older Ruby.
140
142
 
141
143
  ## Known Issues
142
144
 
@@ -11,12 +11,14 @@ Gem::Specification.new do |gem|
11
11
  gem.homepage = "http://github.com/cheezy/data_magic"
12
12
  gem.summary = %q{Provides datasets to application via YAML files}
13
13
  gem.description = %q{Provides datasets to application stored in YAML files}
14
-
14
+
15
15
  gem.files = `git ls-files`.split("\n")
16
16
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  gem.require_paths = ["lib"]
19
-
19
+
20
+ gem.required_ruby_version = '>= 2.2'
21
+
20
22
  gem.add_dependency 'faker', '>= 1.1.2'
21
23
  gem.add_dependency 'yml_reader', '>= 0.6'
22
24
 
@@ -1,7 +1,8 @@
1
1
  require 'data_magic/core_ext/string'
2
2
  require 'data_magic/core_ext/integer'
3
- require "data_magic/version"
4
- require "data_magic/translation"
3
+ require 'data_magic/core_ext/hash'
4
+ require 'data_magic/version'
5
+ require 'data_magic/translation'
5
6
  require 'data_magic/date_translation'
6
7
  require 'data_magic/standard_translation'
7
8
  require 'yml_reader'
@@ -19,7 +20,7 @@ module DataMagic
19
20
  def self.locale=(value)
20
21
  Faker::Config.locale = value
21
22
  end
22
-
23
+
23
24
  def self.included(cls)
24
25
  @parent = cls
25
26
  translators.each do |translator|
@@ -37,7 +38,7 @@ module DataMagic
37
38
  end
38
39
  data = DataMagic.yml[record]
39
40
  raise ArgumentError, "Undefined key #{key}" unless data
40
- additional.key?(record) ? prep_data(data.merge(additional[record]).clone) : prep_data(data.merge(additional).clone)
41
+ prep_data(data.merge(additional.key?(record) ? additional[record] : additional).deep_copy)
41
42
  end
42
43
 
43
44
  # Given a scenario, load any fixture it needs.
@@ -54,25 +55,25 @@ module DataMagic
54
55
  private
55
56
 
56
57
  def the_file
57
- ENV['DATA_MAGIC_FILE'] ? ENV['DATA_MAGIC_FILE'] : 'default.yml'
58
+ ENV['DATA_MAGIC_FILE'] ? ENV['DATA_MAGIC_FILE'] : 'default.yml'
58
59
  end
59
60
 
60
61
  def prep_data(data)
61
- data.each do |key, value|
62
- unless value.nil?
63
- next if !value.respond_to?('[]') || value.is_a?(Numeric)
64
- if value.is_a?(Hash)
65
- data[key] = prep_data(value)
66
- else
67
- data[key] = translate(value[1..-1]) if value[0,1] == "~"
68
- end
69
- end
62
+ case data
63
+ when Hash
64
+ data.each {|key, value| data[key] = prep_data(value)}
65
+ when Array
66
+ data.each_with_index{|value, i| data[i] = prep_data(value)}
67
+ when String
68
+ return translate(data[1..-1]) if data[0, 1] == '~'
70
69
  end
71
70
  data
72
71
  end
73
72
 
74
73
  def translate(value)
75
74
  translation.send :process, value
75
+ rescue => error
76
+ fail "Failed to translate: #{value}\n Reason: #{error.message}\n"
76
77
  end
77
78
 
78
79
  def translation
@@ -88,12 +89,12 @@ module DataMagic
88
89
  def self.fixture_files_on(scenario)
89
90
  # tags for cuke 2, source_tags for cuke 1
90
91
  tags = scenario.send(scenario.respond_to?(:tags) ? :tags : :source_tags)
91
- tags.map(&:name).select { |t| t =~ /@datamagic_/ }.map { |t| t.gsub('@datamagic_', '').to_sym }
92
+ tags.map(&:name).select {|t| t =~ /@datamagic_/}.map {|t| t.gsub('@datamagic_', '').to_sym}
92
93
  end
93
94
 
94
95
  class << self
95
96
  attr_accessor :yml
96
-
97
+
97
98
  def default_directory
98
99
  'config/data'
99
100
  end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def deep_copy
3
+ Marshal.load(Marshal.dump(self))
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module DataMagic
2
- VERSION = "1.1"
2
+ VERSION = "1.2"
3
3
  end
@@ -1,17 +1,12 @@
1
1
  require 'spec_helper'
2
-
3
- class TestSubject
4
- include DataMagic
5
- end
6
-
7
2
  describe "DataMagic translations" do
8
3
  context "when delivering data" do
9
- let(:example) { TestSubject.new }
4
+ let(:example) { (Class.new { include DataMagic }).new }
10
5
 
11
6
  def set_field_value(value)
12
7
  expect(DataMagic).to receive(:yml).twice.and_return({'key' => {'field' => value}})
13
8
  end
14
-
9
+
15
10
  it "should deliver the hash from the yaml" do
16
11
  set_field_value 'value'
17
12
  expect(example.data_for('key')).to have_field_value 'value'
@@ -39,7 +34,7 @@ describe "DataMagic translations" do
39
34
  expect(yaml).to receive(:merge).and_return(yaml)
40
35
  expect(DataMagic).to receive(:yml).twice.and_return(yaml)
41
36
  expect(yaml).to receive(:[]).and_return(yaml)
42
- expect(yaml).to receive(:clone).and_return({'field' => 'value'})
37
+ expect(yaml).to receive(:deep_copy).and_return({'field' => 'value'})
43
38
  expect(example.data_for('key')).to have_field_value 'value'
44
39
  end
45
40
 
@@ -48,7 +43,7 @@ describe "DataMagic translations" do
48
43
  expect(DataMagic).to receive(:yml).twice.and_return(yaml)
49
44
  expect(yaml).to receive(:[]).and_return(yaml)
50
45
  expect(yaml).to receive(:merge).and_return(yaml)
51
- expect(yaml).to receive(:clone).and_return({'field' => 'value'})
46
+ expect(yaml).to receive(:deep_copy).and_return({'field' => 'value'})
52
47
  expect(example.data_for('key')).to have_field_value 'value'
53
48
  end
54
49
 
@@ -170,7 +165,7 @@ describe "DataMagic translations" do
170
165
  expect(example.data_for('key')).to have_field_value 'very_cheezy'
171
166
  end
172
167
  end
173
-
168
+
174
169
  context "translating phone numbers" do
175
170
  it "shold add a phone number" do
176
171
  expect(Faker::PhoneNumber).to receive(:phone_number).and_return('555-555-5555')
@@ -312,5 +307,18 @@ describe "DataMagic translations" do
312
307
  expect(example.data_for('key')).to have_field_value the_date.strftime('%D')
313
308
  end
314
309
  end
310
+ context "should fail when translation call methos is not defined" do
311
+ it "should fail if method does not exist" do
312
+ set_field_value '~non_existing_method'
313
+ expect{example.data_for('key')}.to raise_error(/non_existing_method/)
314
+ end
315
+ end
316
+
317
+ context "array translation test" do
318
+ it "should be able to translate " do
319
+ set_field_value ["~'user' + 'name'", 'second']
320
+ expect(example.data_for('key')).to have_field_value ['username','second']
321
+ end
322
+ end
315
323
  end
316
324
  end
@@ -14,8 +14,9 @@ require 'rspec'
14
14
  require 'data_magic'
15
15
 
16
16
  RSpec::Matchers.define :have_field_value do |expected|
17
+ supports_block_expectations
17
18
  match do |actual|
18
- actual['field'] == expected
19
+ actual['field'] === expected
19
20
  end
20
21
 
21
22
  failure_message do |actual|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Morgan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-03 00:00:00.000000000 Z
11
+ date: 2017-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -95,6 +95,7 @@ files:
95
95
  - features/yaml/another.yml
96
96
  - features/yaml/example.yml
97
97
  - lib/data_magic.rb
98
+ - lib/data_magic/core_ext/hash.rb
98
99
  - lib/data_magic/core_ext/integer.rb
99
100
  - lib/data_magic/core_ext/string.rb
100
101
  - lib/data_magic/date_translation.rb
@@ -116,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
117
  requirements:
117
118
  - - ">="
118
119
  - !ruby/object:Gem::Version
119
- version: '0'
120
+ version: '2.2'
120
121
  required_rubygems_version: !ruby/object:Gem::Requirement
121
122
  requirements:
122
123
  - - ">="