Skip to content

Instantly share code, notes, and snippets.

@dsci
Created December 14, 2011 08:56
Show Gist options
  • Save dsci/1475797 to your computer and use it in GitHub Desktop.
Save dsci/1475797 to your computer and use it in GitHub Desktop.
Decorative and building!
module DecorativeBuildr
class BaseDecorator
def initialize(decorated)
@decorated = decorated
end
def method_missing(method,*args)
@decorated.send(method,args)
end
end
end
module DecorativeBuildr
# A building module which could be included in any
# class to concat multiple strings with a seperator/
# line ending.
#
# @example
# class Sample
# include Util::Builder
# end
#
# sample = Sample.new
# sample.build_info(:line_ending => "<br/>") do |builder|
# builder.add "One"
# builder.add "Two"
# end
#
#
# The result should be something like this:
#
# @example
# One<br/>Two<br/>
#
# @author Daniel Schmidt
module Builder
def add(line)
@infos << rescue_nil_string(line) << @line_ending
end
# Concats multiple string with a line ending. It receives a block.
# @param [Hash] opt Optional configuration hash with :line_ending key, which is \n by default.
# @return [String] The concated String
def build_info(opt={},&block)
raise "Arguments in invalid form" unless opt.is_a?(Hash)
@infos = rescue_nil_string(@infos)
@line_ending = opt.fetch(:line_ending, "\n")
block.call(self)
return @infos
end
private
def rescue_nil_string(string)
string.nil? ? "" : string
end
end
end
Gem::Specification.new do |s|
s.name = 'decorative_buildr'
s.version = '0.1.1'
s.platform = Gem::Platform::RUBY
s.author = 'Daniel Schmidt'
s.email = 'dsci@code79.net'
s.summary = 'DecorativeBuildr!'
s.description = 'Basic decorator pattern and a simple builder.'
s.files = ['decorative_buildr.rb','base_decorator.rb', 'builder.rb']
#s.test_file = 'bang_spec.rb'
s.require_path = '.'
# This will added later.
#s.add_development_dependency('rspec', ["~> 2.0"])
end
Dir[File.expand_path(File.join(File.dirname(__FILE__), '*.rb'))].each{|file| require file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment