seed_formatter 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --debug
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use tfg@seed_formatter --create
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in seed_formatter.gemspec
4
4
  gemspec
5
+ gem 'rspec'
6
+ gem 'ruby-debug'
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # SeedFormatter
2
+
3
+ SeedFormatter is a small gem I created while I was working on various projects with terrible, terrible Seed files.
4
+
5
+ ## Purpose
6
+
7
+ The goal of SeedFormatter is to provide some commonly required functionality to be used in Seed files.
8
+
9
+ ## Dependencies
10
+
11
+ SeedFormatter uses the [colored](https://github.com/defunkt/colored) gem to present output.
12
+
13
+ ## Example: Seed file layout
14
+
15
+ I like my seed files to follow this format:
16
+
17
+ ```ruby
18
+ class Seed
19
+ class << self
20
+ def create_categories
21
+ # create some categories here
22
+ end
23
+
24
+ def create_admin_user
25
+ # create an admin user here
26
+ end
27
+ end
28
+ end
29
+
30
+ Seed.create_categories
31
+ Seed.create_admin_user
32
+ ```
33
+
34
+ I find this to be the neatest way to write Seed files.
35
+
36
+ ## Example: Output
37
+
38
+ SeedFormatter provides four methods of showing output:
39
+
40
+ - `message` A general purpose message, generally used as a header. White by default.
41
+ - `success` Indicates a seed function was successful. Green by default.
42
+ - `error` Indicates a seed function has failed. Red by default.
43
+ - `spacer` Just to add a single space to separate groups of functionality.
44
+
45
+ Using these functions is straight forward (if we use the above example Seed file layout):
46
+
47
+ ```ruby
48
+ class Seed
49
+ class << self
50
+ include SeedFormatter
51
+
52
+ def create_categories
53
+ message "Creating some categories"
54
+
55
+ ["Sedan", "SUV", "Hatchback"].each do |category_name|
56
+ category = Category.create :name => category_name
57
+ if category.valid?
58
+ success "Created #{category_name}"
59
+ else
60
+ error "Failed to create #{category_name}. Errors: #{category.errors.full_messages}"
61
+ end
62
+ end
63
+
64
+ spacer
65
+ end
66
+ end
67
+ end
68
+ ```
69
+
70
+ ## Example: Overriding the display of output
71
+
72
+ You can override the display of output with the following options:
73
+
74
+ - `:prefix` Pass in a string to prefix the output.
75
+ - `:suffix` Pass in a string to suffix the output.
76
+ - `:color` Pass in a symbol representing the color your want the ouput to be. The list of acceptable colors can be found in colored gem's [documentation](https://github.com/defunkt/colored/blob/master/lib/colored.rb).
77
+
78
+ So, there are three methods of customizing the output of SeedFormatter.
79
+
80
+ 1: Pass in the options
81
+
82
+ ```ruby
83
+ success "I did it…they're all trees", {:prefix => "!!!", :suffix => "'''", :color => :magenta}
84
+ ```
85
+
86
+ Which will yield the string `!!!I did it…they're all trees'''` in magenta.
87
+
88
+ 2: Override the default function in your file or similarly,
89
+
90
+ 3: Write your own
91
+
92
+ ```ruby
93
+ def success message, options={}
94
+ options[:prefix] ||= "!!!"
95
+ options[:suffix] ||= "'''"
96
+ options[:color] ||= :magenta
97
+ output message, options
98
+ end
99
+
100
+ def my_custom_output message, options={}
101
+ options[:prefix] ||= "> "
102
+ options[:suffix] ||= " <"
103
+ options[:color] ||= :yellow
104
+ output message, options
105
+ end
106
+ ```
107
+
data/Rakefile CHANGED
@@ -1,8 +1 @@
1
1
  require 'bundler/gem_tasks'
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << 'test'
5
- end
6
-
7
- desc "Run tests"
8
- task :default => :test
@@ -1,3 +1,3 @@
1
1
  module SeedFormatter
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,6 +2,7 @@ require "seed_formatter/version"
2
2
  require "colored"
3
3
 
4
4
  module SeedFormatter
5
+
5
6
  # Outputs a message with a set of given options
6
7
  #
7
8
  # @param [String] message: The message to format
@@ -13,8 +14,9 @@ module SeedFormatter
13
14
  # @example Print out an error message
14
15
  # SeedFormatter.output "Some error", {:prefix => "!!! ", :color => :red}
15
16
  # # outputs "!!! Some error" in red text
16
- def output message, options
17
- puts "#{options[:prefix]}#{message}#{options[:suffix]}".send(options[:color])
17
+ def output message, options = {}
18
+ options[:color] ||= :white
19
+ $stdout.puts "#{options[:prefix]}#{message}#{options[:suffix]}".send(options[:color])
18
20
  end
19
21
 
20
22
  # A preset formatter with overridable options
@@ -39,6 +41,20 @@ module SeedFormatter
39
41
  end
40
42
 
41
43
  def spacer
42
- puts ""
44
+ $stdout.puts ""
45
+ end
46
+
47
+ # Allows you to run a block on each section of a .yml file.
48
+ #
49
+ # @param [String] path: The full path of the .yml file
50
+ def load_yaml_file path
51
+ begin
52
+ items = YAML::load(File.read(path))
53
+ items.each do |hash|
54
+ yield(hash)
55
+ end
56
+ rescue
57
+ error "Unable to load YAML file #{path}, exception: #{$!}"
58
+ end
43
59
  end
44
60
  end
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
7
7
  s.version = SeedFormatter::VERSION
8
8
  s.authors = ["Jordan Maguire"]
9
9
  s.email = ["jmaguire@thefrontiergroup.com.au"]
10
- s.homepage = "https://github.com/jordanmaguire/jordanmaguire.github.com"
11
- s.summary = "summary"
10
+ s.homepage = "https://github.com/jordanmaguire/seed_formatter"
11
+ s.summary = "Easily format the output of your seeds and parse YAML files"
12
12
  s.description = "Easily format the output of your seeds and parse YAML files"
13
13
 
14
14
  s.files = `git ls-files`.split("\n")
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'seed_formatter'
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed_formatter
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 25
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 2
9
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
10
11
  platform: ruby
11
12
  authors:
12
13
  - Jordan Maguire
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2012-01-31 00:00:00 +08:00
18
+ date: 2012-02-03 00:00:00 +08:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: colored
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
@@ -40,13 +43,17 @@ extra_rdoc_files: []
40
43
 
41
44
  files:
42
45
  - .gitignore
46
+ - .rspec
47
+ - .rvmrc
43
48
  - Gemfile
49
+ - README.md
44
50
  - Rakefile
45
51
  - lib/seed_formatter.rb
46
52
  - lib/seed_formatter/version.rb
47
53
  - seed_formatter.gemspec
54
+ - spec/spec_helper.rb
48
55
  has_rdoc: true
49
- homepage: https://github.com/jordanmaguire/jordanmaguire.github.com
56
+ homepage: https://github.com/jordanmaguire/seed_formatter
50
57
  licenses: []
51
58
 
52
59
  post_install_message:
@@ -55,25 +62,29 @@ rdoc_options: []
55
62
  require_paths:
56
63
  - lib
57
64
  required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
67
  - - ">="
60
68
  - !ruby/object:Gem::Version
69
+ hash: 3
61
70
  segments:
62
71
  - 0
63
72
  version: "0"
64
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
65
75
  requirements:
66
76
  - - ">="
67
77
  - !ruby/object:Gem::Version
78
+ hash: 3
68
79
  segments:
69
80
  - 0
70
81
  version: "0"
71
82
  requirements: []
72
83
 
73
84
  rubyforge_project:
74
- rubygems_version: 1.3.6
85
+ rubygems_version: 1.5.2
75
86
  signing_key:
76
87
  specification_version: 3
77
- summary: summary
78
- test_files: []
79
-
88
+ summary: Easily format the output of your seeds and parse YAML files
89
+ test_files:
90
+ - spec/spec_helper.rb