seed_formatter 0.0.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -105,3 +105,35 @@ def my_custom_output message, options={}
105
105
  end
106
106
  ```
107
107
 
108
+ ## Example: Parsing yaml file
109
+
110
+ SeedFormatter provides a function for parsing [YAML files](http://www.yaml.org/spec/1.2/spec.html).
111
+
112
+ The function `load_yaml_file` accepts a single argument `path` that is the path to a .yml file.
113
+
114
+ `load_yaml_file` also accepts a block that provides an argument `hash` representing the contents of each YAML structure in the .yml file.
115
+
116
+ For example, a .yml file that has a structure such as:
117
+
118
+ ```YAML
119
+ -
120
+ something: "Other Thing"
121
+ ruby: "Programming Language"
122
+ readme: "Time Consuming"
123
+ ```
124
+
125
+ will yield a hash as such:
126
+
127
+ ```ruby
128
+ {:something => "Other Thing", :ruby => "Programming Language", :readme => "Time Consuming"}
129
+ ```
130
+
131
+ As an example, if you wanted to print the contents of each structure in a .yml file you could call:
132
+
133
+ ```ruby
134
+ load_yaml_file file_path do |hash|
135
+ puts hash
136
+ end
137
+ ```
138
+
139
+ This will call the block passed through on each structure in the .yml file
@@ -1,3 +1,3 @@
1
1
  module SeedFormatter
2
- VERSION = "0.0.3"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require "seed_formatter/version"
2
2
  require "colored"
3
+ require "active_support/core_ext/hash/indifferent_access"
3
4
 
4
5
  module SeedFormatter
5
6
 
@@ -51,7 +52,7 @@ module SeedFormatter
51
52
  begin
52
53
  items = YAML::load(File.read(path))
53
54
  items.each do |hash|
54
- yield(hash)
55
+ yield(hash.with_indifferent_access)
55
56
  end
56
57
  rescue
57
58
  error "Unable to load YAML file #{path}, exception: #{$!}"
@@ -17,4 +17,5 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = ["lib"]
18
18
 
19
19
  s.add_dependency 'colored'
20
+ s.add_dependency 'active_support'
20
21
  end
@@ -0,0 +1,174 @@
1
+ require 'spec_helper'
2
+
3
+ class TestClass
4
+ include SeedFormatter
5
+ end
6
+
7
+ describe SeedFormatter do
8
+
9
+ let(:tc) { TestClass.new }
10
+
11
+ describe '#output' do
12
+ subject { tc.output message, options }
13
+ let(:message) { "message" }
14
+ let(:options) { {} }
15
+
16
+ context 'options[:prefix] is provided' do
17
+ let(:options) { {:prefix => "a prefix "} }
18
+ it "prefixes the message with options[:prefix]" do
19
+ $stdout.should_receive(:puts).with(/a prefix message/)
20
+ subject
21
+ end
22
+ end
23
+ context 'options[:suffix] is provided' do
24
+ let(:options) { {:suffix => " a suffix"} }
25
+ it "suffixes the message with options[:suffix]" do
26
+ $stdout.should_receive(:puts).with(/message a suffix/)
27
+ subject
28
+ end
29
+ end
30
+ context 'options[:color] is provided' do
31
+ let(:options) { {:color => :blue} }
32
+ it "prints the message with the provided color" do
33
+ $stdout.should_receive(:puts).with("\e[34mmessage\e[0m")
34
+ subject
35
+ end
36
+ end
37
+ context 'options[:color] is not provided' do
38
+ let(:options) { {:color => nil} }
39
+ it "prints the message in white by default" do
40
+ $stdout.should_receive(:puts).with("\e[37mmessage\e[0m")
41
+ subject
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#message' do
47
+ subject { tc.message message, options }
48
+ let(:message) { "message" }
49
+ let(:options) { {:prefix => prefix, :color => color} }
50
+ let(:prefix) { nil }
51
+ let(:color) { nil }
52
+ context 'options[:prefix] is provided' do
53
+ let(:prefix) { "a prefix " }
54
+ it 'passes options[:prefix] to output' do
55
+ tc.should_receive(:output).with(anything, hash_including(:prefix => prefix))
56
+ subject
57
+ end
58
+ end
59
+ context 'options[:prefix] is not provided' do
60
+ it 'passes a default prefix through to output' do
61
+ tc.should_receive(:output).with(anything, hash_including(:prefix => "*** "))
62
+ subject
63
+ end
64
+ end
65
+ context 'options[:color] is provided' do
66
+ let(:color) { :blue }
67
+ it 'passes options[:color] to output' do
68
+ tc.should_receive(:output).with(anything, hash_including(:color => color))
69
+ subject
70
+ end
71
+ end
72
+ context 'options[:color] is not provided' do
73
+ it 'passes a default color through to output' do
74
+ tc.should_receive(:output).with(anything, hash_including(:color => :white))
75
+ subject
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '#success' do
81
+ subject { tc.success message, options }
82
+ let(:message) { "message" }
83
+ let(:options) { {:prefix => prefix, :color => color} }
84
+ let(:prefix) { nil }
85
+ let(:color) { nil }
86
+ context 'options[:prefix] is provided' do
87
+ let(:prefix) { "a prefix " }
88
+ it 'passes options[:prefix] to output' do
89
+ tc.should_receive(:output).with(anything, hash_including(:prefix => prefix))
90
+ subject
91
+ end
92
+ end
93
+ context 'options[:prefix] is not provided' do
94
+ it 'passes a default prefix through to output' do
95
+ tc.should_receive(:output).with(anything, hash_including(:prefix => " + "))
96
+ subject
97
+ end
98
+ end
99
+ context 'options[:color] is provided' do
100
+ let(:color) { :blue }
101
+ it 'passes options[:color] to output' do
102
+ tc.should_receive(:output).with(anything, hash_including(:color => color))
103
+ subject
104
+ end
105
+ end
106
+ context 'options[:color] is not provided' do
107
+ it 'passes a default color through to output' do
108
+ tc.should_receive(:output).with(anything, hash_including(:color => :green))
109
+ subject
110
+ end
111
+ end
112
+ end
113
+
114
+ describe '#error' do
115
+ subject { tc.error message, options }
116
+ let(:message) { "message" }
117
+ let(:options) { {:prefix => prefix, :color => color} }
118
+ let(:prefix) { nil }
119
+ let(:color) { nil }
120
+ context 'options[:prefix] is provided' do
121
+ let(:prefix) { "a prefix " }
122
+ it 'passes options[:prefix] to output' do
123
+ tc.should_receive(:output).with(anything, hash_including(:prefix => prefix))
124
+ subject
125
+ end
126
+ end
127
+ context 'options[:prefix] is not provided' do
128
+ it 'passes a default prefix through to output' do
129
+ tc.should_receive(:output).with(anything, hash_including(:prefix => " - "))
130
+ subject
131
+ end
132
+ end
133
+ context 'options[:color] is provided' do
134
+ let(:color) { :blue }
135
+ it 'passes options[:color] to output' do
136
+ tc.should_receive(:output).with(anything, hash_including(:color => color))
137
+ subject
138
+ end
139
+ end
140
+ context 'options[:color] is not provided' do
141
+ it 'passes a default color through to output' do
142
+ tc.should_receive(:output).with(anything, hash_including(:color => :red))
143
+ subject
144
+ end
145
+ end
146
+ end
147
+
148
+ describe '#spacer' do
149
+ subject { tc.spacer }
150
+ it 'prints a blank line' do
151
+ $stdout.should_receive(:puts).with(/^$/)
152
+ subject
153
+ end
154
+ end
155
+
156
+ describe '#load_yaml_file' do
157
+ context 'valid .yml file' do
158
+ it 'allows a block to process the contents of each section in the .yml file' do
159
+ tc.load_yaml_file File.join(Dir.pwd, "spec", "support", "yml", "spec.yml") do |hash|
160
+ hash["tautology"].should == "tautology"
161
+ end
162
+ end
163
+ end
164
+ context 'invalid file' do
165
+ it 'prints out an error' do
166
+ tc.should_receive(:error)
167
+ tc.load_yaml_file File.join(Dir.pwd, "spec", "support", "yml", "nothing.yml") do |hash|
168
+ # Nothing
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ end
@@ -0,0 +1,10 @@
1
+ -
2
+ tautology: "tautology"
3
+ -
4
+ tautology: "tautology"
5
+ -
6
+ tautology: "tautology"
7
+ -
8
+ tautology: "tautology"
9
+ -
10
+ tautology: "tautology"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed_formatter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 3
10
- version: 0.0.3
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jordan Maguire
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-03 00:00:00 +08:00
18
+ date: 2012-04-12 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,20 @@ dependencies:
32
32
  version: "0"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: active_support
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
35
49
  description: Easily format the output of your seeds and parse YAML files
36
50
  email:
37
51
  - jmaguire@thefrontiergroup.com.au
@@ -51,7 +65,9 @@ files:
51
65
  - lib/seed_formatter.rb
52
66
  - lib/seed_formatter/version.rb
53
67
  - seed_formatter.gemspec
68
+ - spec/lib/seed_formatter_spec.rb
54
69
  - spec/spec_helper.rb
70
+ - spec/support/yml/spec.yml
55
71
  has_rdoc: true
56
72
  homepage: https://github.com/jordanmaguire/seed_formatter
57
73
  licenses: []
@@ -87,4 +103,6 @@ signing_key:
87
103
  specification_version: 3
88
104
  summary: Easily format the output of your seeds and parse YAML files
89
105
  test_files:
106
+ - spec/lib/seed_formatter_spec.rb
90
107
  - spec/spec_helper.rb
108
+ - spec/support/yml/spec.yml