module Duplo
Constants
- BRICK
- TOYS
- VERSION
Attributes
default_size[W]
Public Class Methods
add(toy, part)
click to toggle source
# File lib/duplo.rb, line 22 def add(toy, part) case toy when Array, Set toy << part when Hash toy.store(toy.size, part) end end
build(part, path = [], &block)
click to toggle source
# File lib/duplo.rb, line 35 def build(part, path = [], &block) raise %(Don't know how to build "#{part}", sorry) unless can_build? part part = part.dup brick = part.slice! BRICK type, size = crack brick toy = type.new if part.empty? block ||= proc { |path| path.is_a?(Array) ? path.last : path } size.times { |i| add toy, block.call(path.empty? ? i : path + [i]) } else size.times { |i| add toy, build(part, path + [i], &block) } end toy end
can_build?(toy)
click to toggle source
# File lib/duplo.rb, line 31 def can_build?(toy) !!(toy.strip =~ /\A#{BRICK}+\z/) end
crack(brick)
click to toggle source
# File lib/duplo.rb, line 57 def crack(brick) type, size = brick.split("", 2) size = size.empty? ? default_size : size.to_i [TOYS[type], size] end
default_size()
click to toggle source
# File lib/duplo.rb, line 18 def default_size @default_size || 5 end
smash(toy)
click to toggle source
# File lib/duplo.rb, line 53 def smash(toy) toy.scan(BRICK).map { |brick| crack brick } end
spell(toy)
click to toggle source
# File lib/duplo.rb, line 63 def spell(toy) smash(toy).map.with_index do |(type, size), idx| plural = idx == 0 ? "" : type == Hash ? "es" : "s" size.zero? ? "empty #{type}#{plural}" : "#{size}-element #{type}#{plural}" end.join(" containing ") end
Public Instance Methods
method_missing(method_name, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/duplo.rb, line 78 def method_missing(method_name, *arguments, &block) toy = method_name.to_s if Duplo.can_build? toy Duplo.build toy, *arguments, &block else super end end
respond_to_missing?(method_name, *)
click to toggle source
Calls superclass method
# File lib/duplo.rb, line 73 def respond_to_missing?(method_name, *) toy = method_name.to_s Duplo.can_build?(toy) || super end