developwithpassion_expander 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/.gitignore +3 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +5 -0
  5. data/Rakefile +1 -0
  6. data/bin/dwp_expand +4 -0
  7. data/develpwithpassion_expander.gemspec +29 -0
  8. data/lib/developwithpassion_expander.rb +32 -0
  9. data/lib/developwithpassion_expander/array.rb +8 -0
  10. data/lib/developwithpassion_expander/array_dsl.rb +34 -0
  11. data/lib/developwithpassion_expander/array_dsl_module_factory.rb +14 -0
  12. data/lib/developwithpassion_expander/array_mutator.rb +12 -0
  13. data/lib/developwithpassion_expander/array_mutator_step.rb +17 -0
  14. data/lib/developwithpassion_expander/array_readable_step.rb +15 -0
  15. data/lib/developwithpassion_expander/array_visitor.rb +12 -0
  16. data/lib/developwithpassion_expander/array_visitor_step.rb +16 -0
  17. data/lib/developwithpassion_expander/array_writeable_step.rb +15 -0
  18. data/lib/developwithpassion_expander/cli_interface.rb +41 -0
  19. data/lib/developwithpassion_expander/copy.rb +29 -0
  20. data/lib/developwithpassion_expander/copy_to_target.rb +13 -0
  21. data/lib/developwithpassion_expander/enumerable_extensions.rb +11 -0
  22. data/lib/developwithpassion_expander/erb_template_file.rb +33 -0
  23. data/lib/developwithpassion_expander/expansion.rb +84 -0
  24. data/lib/developwithpassion_expander/file.rb +32 -0
  25. data/lib/developwithpassion_expander/file_merge.rb +54 -0
  26. data/lib/developwithpassion_expander/kernel.rb +35 -0
  27. data/lib/developwithpassion_expander/log.rb +17 -0
  28. data/lib/developwithpassion_expander/mustache_template_file.rb +11 -0
  29. data/lib/developwithpassion_expander/object_extensions.rb +28 -0
  30. data/lib/developwithpassion_expander/shell.rb +9 -0
  31. data/lib/developwithpassion_expander/shell_action_against_file.rb +12 -0
  32. data/lib/developwithpassion_expander/string.rb +5 -0
  33. data/lib/developwithpassion_expander/template_processors.rb +24 -0
  34. data/lib/developwithpassion_expander/template_visitor.rb +20 -0
  35. data/lib/developwithpassion_expander/version.rb +5 -0
  36. data/spec/spec_helper.rb +77 -0
  37. data/spec/specs/array_dsl_spec.rb +38 -0
  38. data/spec/specs/array_mutator_step_spec.rb +47 -0
  39. data/spec/specs/array_readable_step_spec.rb +26 -0
  40. data/spec/specs/array_spec.rb +22 -0
  41. data/spec/specs/array_visitor_step_spec.rb +78 -0
  42. data/spec/specs/array_writeable_step_spec.rb +30 -0
  43. data/spec/specs/copy_spec.rb +89 -0
  44. data/spec/specs/copy_to_target_spec.rb +24 -0
  45. data/spec/specs/enumerable_extensions_spec.rb +27 -0
  46. data/spec/specs/erb_template_file_spec.rb +65 -0
  47. data/spec/specs/expansion_spec.rb +146 -0
  48. data/spec/specs/file_merge_spec.rb +133 -0
  49. data/spec/specs/file_spec.rb +81 -0
  50. data/spec/specs/kernel_spec.rb +56 -0
  51. data/spec/specs/mustache_template_file_spec.rb +50 -0
  52. data/spec/specs/object_spec.rb +202 -0
  53. data/spec/specs/sample.rb +5 -0
  54. data/spec/specs/shell_action_against_file_spec.rb +24 -0
  55. data/spec/specs/string_spec.rb +13 -0
  56. data/spec/specs/template_processors_spec.rb +64 -0
  57. data/spec/specs/template_visitor_spec.rb +86 -0
  58. metadata +202 -0
@@ -0,0 +1,32 @@
1
+ class File
2
+ def self.open_for_read(file)
3
+ File.open(file,'r').each do|line|
4
+ yield line
5
+ end
6
+ end
7
+
8
+ def self.read_all_text(file)
9
+ File.read_all_text_after_skipping_lines(file,0)
10
+ end
11
+
12
+ def self.read_all_text_after_skipping_lines(file,number_of_lines_to_skip)
13
+ index = 1
14
+ contents = ''
15
+
16
+ if File.exist?(file)
17
+ File.open_for_read(file) do |line|
18
+ contents += line if index > number_of_lines_to_skip
19
+ index+=1
20
+ end
21
+ end
22
+
23
+ return contents
24
+ end
25
+
26
+ def self.open_for_write(file)
27
+ File.open(file,'w') do|new_file|
28
+ yield new_file
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,54 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class FileMerge
4
+ attr_accessor :before_files,:after_files,:read_original_contents
5
+
6
+ def initialize(output_file)
7
+ array :before_files do|a|
8
+ a.read_and_write
9
+ a.mutator :add_before_original_contents do|file|
10
+ add_merge_file(@before_files,file)
11
+ end
12
+ a.mutator :add do|file|
13
+ add_before_original_contents(file)
14
+ end
15
+ end
16
+ array :after_files do|a|
17
+ a.readable
18
+ a.writable
19
+ a.mutator :add_after_original_contents do|file|
20
+ add_merge_file(@after_files,file)
21
+ end
22
+ end
23
+ @output_file = output_file
24
+ @read_original_contents = true
25
+ end
26
+
27
+ def dont_read_original_file_contents
28
+ @read_original_contents = false
29
+ end
30
+
31
+ def run
32
+ original_contents = File.read_all_text(@output_file)
33
+ FileUtils.rm_f @output_file
34
+ File.open_for_write(@output_file) do |file|
35
+ merge_files(@before_files,file)
36
+ file.write original_contents if @read_original_contents
37
+ merge_files(@after_files,file)
38
+ end
39
+ end
40
+
41
+ def merge_files(source_files,target)
42
+ source_files.each do|source|
43
+ target.write File.read_all_text(source)
44
+ end
45
+ end
46
+
47
+ :private
48
+ def add_merge_file(items,file)
49
+ return if items.include?(file)
50
+ items << file if File.exists?(file)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,35 @@
1
+ module Kernel
2
+ def expand(title = "Expansion",&block)
3
+ DevelopWithPassion::Expander::Expansion.instance.instance_eval(&block)
4
+ end
5
+
6
+ def glob(path)
7
+ items = Dir.glob(path,File::FNM_DOTMATCH)
8
+ items.each{|item| yield item if block_given?}
9
+ return items
10
+ end
11
+
12
+ def disable_logging
13
+ DevelopWithPassion::Expander::Log.disable
14
+ end
15
+
16
+ def enable_logging
17
+ DevelopWithPassion::Expander::Log.enable
18
+ end
19
+
20
+ def delayed
21
+ Configatron::Delayed.new do
22
+ yield
23
+ end
24
+ end
25
+
26
+ def dynamic
27
+ Configatron::Dynamic.new do
28
+ yield
29
+ end
30
+ end
31
+
32
+ def log(message)
33
+ DevelopWithPassion::Expander::Log.message(message)
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class Log
4
+ class << self
5
+ def message(item)
6
+ puts item if @enabled
7
+ end
8
+ def enable
9
+ @enabled = true
10
+ end
11
+ def disable
12
+ @enabled = false
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class MustacheTemplateFile
4
+ def process(args)
5
+ template = File.read_all_text(args[:input])
6
+
7
+ File.open_for_write(args[:output]){|file| file << Mustache.render(template,configatron.to_hash)}
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ class Object
2
+ def initialize_defaults(factory,*items)
3
+ items.each do|item|
4
+ instance_variable_set("@#{item}",factory.call)
5
+ end
6
+ end
7
+
8
+ def initialize_arrays(*items)
9
+ initialize_defaults(lambda{[]},*items)
10
+ end
11
+
12
+ def initialize_hashes(*items)
13
+ initialize_defaults(lambda{{}},*items)
14
+ end
15
+
16
+ def initialize_false(*items)
17
+ initialize_defaults(lambda{false},*items)
18
+ end
19
+
20
+ def array(name,&block)
21
+ initialize_arrays(name)
22
+ dsl = DevelopWithPassion::Expander::ArrayDSL.new(name)
23
+ yield dsl if block_given?
24
+ modules = DevelopWithPassion::Expander::ArrayDSLModuleFactory.new.create_modules_from(dsl)
25
+ modules.each{|new_module| self.extend(new_module)}
26
+ end
27
+
28
+ end
@@ -0,0 +1,9 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class Shell
4
+ def run(cmd)
5
+ return `#{cmd}`
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ShellActionAgainstFile
4
+ def initialize(cmd)
5
+ @cmd = cmd
6
+ end
7
+ def run_using(file_name)
8
+ Shell.instance.run("#{@cmd} #{file_name}")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def as_home_file
3
+ File.join(ENV["HOME"],self)
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class TemplateProcessors
4
+ def initialize(processors = {})
5
+ @processors = processors
6
+ end
7
+
8
+ def get_processor_for(file_name)
9
+ template_type = File.extname(file_name).gsub(/\./,'').to_sym
10
+ raise "There is no processor for #{file_name}" unless processor_exists_for(template_type)
11
+ return @processors[template_type]
12
+ end
13
+
14
+ def register_processor(template_type,processor)
15
+ raise "The processor for the template already exists" if processor_exists_for(template_type)
16
+ @processors[template_type.to_sym] = processor
17
+ end
18
+
19
+ def processor_exists_for(template_type)
20
+ return @processors.has_key?(template_type.to_sym)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class TemplateVisitor
4
+ def initialize(args = {})
5
+ @processor_registry = args.fetch(:processor_registry,TemplateProcessors.instance)
6
+ @file = args.fetch(:file,File)
7
+ end
8
+
9
+ def run_using(file_name)
10
+ processor = @processor_registry.get_processor_for(file_name)
11
+ generated_name = File.basename(file_name,File.extname(file_name))
12
+ generated_name = generated_name.gsub(/\.dotfile/,"")
13
+ generated_name = ".#{generated_name}" if (/\.dotfile/ =~ file_name)
14
+ output = File.join(File.dirname(file_name),generated_name)
15
+ @file.delete(output) if @file.exists?(output)
16
+ processor.process(:input => file_name,:output => output)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,77 @@
1
+ require 'rspec'
2
+ require 'fileutils'
3
+ require 'developwithpassion_fakes'
4
+
5
+ Dir.chdir(File.join(File.dirname(__FILE__),"..,lib".split(','))) do
6
+ Dir.glob("**/*.rb").each do |file|
7
+ full_path = File.expand_path(file)
8
+ $:.unshift File.expand_path(File.dirname(full_path))
9
+ require full_path
10
+ end
11
+ end
12
+
13
+ class RelativeFileSystem
14
+ class << self
15
+ def base_path
16
+ path = File.join(File.expand_path(File.dirname(__FILE__)),"spec_filesytem")
17
+
18
+ return path
19
+ end
20
+
21
+ def file_name(name)
22
+ item = File.join(base_path,name)
23
+ return item
24
+ end
25
+ end
26
+
27
+ def initialize
28
+ @files = []
29
+ FileUtils.mkdir_p(RelativeFileSystem.base_path)
30
+ end
31
+
32
+
33
+ def teardown
34
+ FileUtils.rm_rf(RelativeFileSystem.base_path)
35
+ end
36
+
37
+ def write_file(name,contents)
38
+ FileUtils.mkdir_p File.dirname(RelativeFileSystem.file_name(name))
39
+ File.open(RelativeFileSystem.file_name(name),'w') do |file|
40
+ file << contents
41
+ end
42
+ end
43
+ end
44
+
45
+ def fake
46
+ DevelopWithPassion::Fakes::Fake.new
47
+ end
48
+
49
+ def catch_exception
50
+ begin
51
+ yield
52
+ rescue Exception => e
53
+ exception = e
54
+ end
55
+ exception
56
+ end
57
+
58
+ module RSpec
59
+ Matchers.define :have_received do|symbol,*args|
60
+ match do|fake|
61
+ fake.received(symbol).called_with(*args) != nil
62
+ end
63
+ end
64
+
65
+ Matchers.define :never_receive do|symbol,*args|
66
+ match do|fake|
67
+ item = fake.received(symbol)
68
+ result = true
69
+ if (item == nil)
70
+ result = (args.count == 0)
71
+ else
72
+ result = (item.called_with(*args) == nil)
73
+ end
74
+ result
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Expander
5
+ describe ArrayDSL do
6
+ context "when a mutator is specified" do
7
+ let(:mutator){fake}
8
+ let(:name){"sdfsdf"}
9
+ let(:sut){ArrayDSL.new("sdf")}
10
+ before (:each) do
11
+ ArrayMutator.stub(:new).with(name,nil).and_return(mutator)
12
+ end
13
+ before (:each) do
14
+ sut.mutator(name)
15
+ end
16
+ it "should be added to the list of mutators for the list" do
17
+ sut.mutators[0].should == mutator
18
+ end
19
+ end
20
+
21
+ context "when a visitor is specified" do
22
+ let(:visitor){fake}
23
+ let(:the_visitor){fake}
24
+ let(:name){"sdfsdf"}
25
+ let(:sut){ArrayDSL.new("sdf")}
26
+ before (:each) do
27
+ ArrayVisitor.stub(:new).with(name,the_visitor).and_return(visitor)
28
+ end
29
+ before (:each) do
30
+ sut.process_using(name,the_visitor)
31
+ end
32
+ it "should be added to the list of visitors for the list" do
33
+ sut.visitors[0].should == visitor
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Expander
5
+ describe ArrayMutatorStep do
6
+ context "when run" do
7
+ let(:numbers){[]}
8
+ let(:target){Sample.new(numbers)}
9
+ let(:mutators){[]}
10
+ let(:sut){ArrayMutatorStep.new}
11
+ let(:builder){ArrayDSL.new(:numbers)}
12
+
13
+ context "using a dsl fragment that contains no block usage" do
14
+ before (:each) do
15
+ builder.mutator(:add_a_number)
16
+ end
17
+ before (:each) do
18
+ target.extend(sut.run_using(builder))
19
+ end
20
+ it "should create a member on the target that allows mutation of the original list" do
21
+ target.respond_to?(:add_a_number).should be_true
22
+ target.add_a_number(2)
23
+ numbers[0].should == 2
24
+ end
25
+ end
26
+ context "using a dsl fragment that contains block usage" do
27
+ before (:each) do
28
+ builder.mutator(:add_a_number) do|value|
29
+ @value_attempted_to_be_added = value
30
+ end
31
+ end
32
+ before (:each) do
33
+ target.extend(sut.run_using(builder))
34
+ end
35
+ it "should create a member on the target that intercepts mutation using the provided block" do
36
+ target.respond_to?(:add_a_number).should be_true
37
+ target.add_a_number(2)
38
+ numbers.count.should == 0
39
+ @value_attempted_to_be_added.should == 2
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module DevelopWithPassion
4
+ module Expander
5
+ describe ArrayReadableStep do
6
+ context "when run" do
7
+ let(:target){Sample.new}
8
+ let(:mutators){[]}
9
+ let(:sut){ArrayReadableStep.new}
10
+ let(:builder){ArrayDSL.new(:numbers)}
11
+
12
+ context "using a dsl fragment that contains no block usage" do
13
+ before (:each) do
14
+ builder.readable
15
+ end
16
+ before (:each) do
17
+ target.extend(sut.run_using(builder))
18
+ end
19
+ it "should create a member on the target that allows reading of the array" do
20
+ target.numbers.should be_a(Array)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end