TbpgrUtils

TbpgrUtils is Utilities.

Installation

Add this line to your application's Gemfile:

gem 'tbpgr_utils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tbpgr_utils

Usage

List

class/module/method mean
Array#together loop all arrays by block
Object#any_of? if self match any one of items, return true
Object#boolean? data type check for boolean
Object#my_methods return public/protected/private self define methods
String#justify_table justify pipe format table string
AttributesInitializable::ClassMethods.attr_accessor_init generate attr_accessors + initializer
Templatable module get result from template + placeholder
Ghostable module help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name)
Kernel#print_eval Print code + eval result
Kernel#puts_eval Puts code + eval result

Array#together

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together do |first, second|
  print "#{first}:#{second}\n"  # => output one:1, two:2, three:3
end

Object#any_of?

require 'tbpgr_utils'

p 'hoge'.any_of? 'hoge', 'hige'    # =>true
p 'hoge'.any_of?(*%w{hoge hige})    # =>true
p 'hige'.any_of? 'hoge', 'hige'    # =>true
p 'hege'.any_of? 'hoge', 'hige'    # =>false
p 1.any_of? 1, 2, 3                # =>true
p 4.any_of? 1, 2, 3                # =>false

Object#boolean?

require 'tbpgr_utils'

p true.boolean?    # =>true
p false.boolean?   # =>true
p nil.boolean?     # =>false
p "".boolean?      # =>false
p "true".boolean?  # =>false

Object#my_methods

require 'tbpgr_utils'

class Hoge
  def hgoe
  end

  protected
  def hige
  end

  private
  def hege
  end
end

p Hoge.new.my_methods # =>[:hoge, :hige, :hege]

String#justify_table

require 'tbpgr_utils'

str ="|* hogehogehoge|* hege|* hige|\n|test|tester|testest|\n|test|tester|aaaaaaaaaaaaaaaaaaaaaaatestest|\n"

puts str.justify_table

output

|* hogehogehoge|* hage|* hige                        |
|test          |tester|testest                       |
|test          |tester|aaaaaaaaaaaaaaaaaaaaaaatestest|

AttributesInitializable::ClassMethods.attr_accessor_init

require 'attributes_initializable'

class AccessorSample
  include AttributesInitializable
  attr_accessor_init :atr1, :atr2
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
p atr_sample1.atr1 # => atr1
p atr_sample1.atr2 # => atr2

atr_sample2 = AccessorSample.new do |a|
  a.atr1 = 'atr1'
  a.atr2 = 'atr2'
end
p atr_sample2.atr1 # => atr1
p atr_sample2.atr2 # => atr2

same mean code is

class AccessorSample
  attr_accessor :atr1, :atr2

  def initialize(values = nil, &block)
    return yield self if block
    @atr1 = values[:atr1]
    @atr2 = values[:atr2]
  end
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
p atr_sample1.atr1 # => atr1
p atr_sample1.atr2 # => atr2

atr_sample2 = AccessorSample.new do |a|
  a.atr1 = 'atr1'
  a.atr2 = 'atr2'
end
p atr_sample2.atr1 # => atr1
p atr_sample2.atr2 # => atr2

Templatable

  • include Templatable
  • set template by here-document
  • in template, parameter must name 'placeholders[:xxxxx]'. xxxxx is your favorite name.
  • when create instance, you must set materials to create template. after, you can get this value from @materials.
  • you must create manufactured_xxx methods. xxx is each-placeholder name.
  • you can get result by 'result' method.
require 'templatable'

class TemplateUser
  include Templatable
  template "line1:<%=placeholders[:hoge]%>\nline2:<%=placeholders[:hige]%>\n  EOS\n\n  def manufactured_hoge\n    \"hoge-\#{@materials}\"\n  end\n\n  def manufactured_hige\n    \"hige-\#{@materials}\"\n  end\nend\n\np TemplateUser.new('sample').result\n"

output

line1:hoge-sample
line2:hige-sample

Ghostable

  • include Ghostable
  • create ghost method by using Ghostable::ghost_method
  • ghost_method first_args = method_name_pattern
  • ghost_method second_args = method_base_name Symbol(using in Ghostable internal logic)
  • ghost_method third = block. this block is main logic. block can use args[method_name, *args, &block]

sample ghost method define module.

module Checkable
  include Ghostable
  ghost_method /check_range_.*\?$/, :check_range do |method_name, *args, &block|
    method_name.to_s =~ /(check_range_)(\d+)(_to_)(\d*)/
    from = $2.to_i
    to = $4.to_i
    value = args.first
    (from..to).include? value
  end

  ghost_method /^contain_.*\?$/, :check_contain do |method_name, *args, &block|
    method_name.to_s =~ /^(contain_)(.*)(\?)/
    word = $2
    value = args.first
    value.include? word
  end
end
  • use ghost method

sample ghost method use class

class SampleChecker
  include Checkable
end

sample = SampleChecker.new
sample.check_range_3_to_5?(4) # => return true
sample.check_range_3_to_5?(6) # => return false
sample.check_range_3_to_6?(6) # => return true

sample.contain_hoge? "test_hoge_test" # => return true
sample.contain_hoge? "test_hige_test" # => return false
sample.contain_hige? "test_hige_test" # => return true

Kernel#print_eval

This method for sample code. for manual, for blog-entry's-snippet ...etc.

print_eval 8/4, binding  # => 8/4 # => 2

message = 'msg'
print_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"

output

8/4 # => 2"hoge-#{message}" # => "hoge-msg"

Kernel#puts_eval

This method for sample code. for manual, for blog-entry's-snippet ...etc.

puts_eval 8/4, binding

message = 'msg'
puts_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"

output

8/4 # => 2
"hoge-#{message}" # => "hoge-msg"

History

  • version 0.0.7 : add Kernel#print_eval, Kernel#puts_eval
  • version 0.0.6 : add Ghostable
  • version 0.0.5 : add Templatable
  • version 0.0.4 : AttributesInitializable::ClassMethods.attr_accessor_init
  • version 0.0.3 : add Object#any_of?
  • version 0.0.2 : loop all arrays by block.
  • version 0.0.1 : first release.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request