Class: Sheaf::Stack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sheaf/stack.rb

Direct Known Subclasses

RootClass

Defined Under Namespace

Classes: RootClass

Constant Summary collapse

Root =
RootClass.new
SWYM =
proc {}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stackable = Root, parent = Root) ⇒ Stack

Returns a new instance of Stack.



49
50
51
52
# File 'lib/sheaf/stack.rb', line 49

def initialize(stackable = Root, parent = Root)
  @stackable = stackable
  @parent = parent
end

Class Method Details

.[](*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/sheaf/stack.rb', line 36

def [](*args)
  return new if args.empty?

  args[1..-1].inject(new(args[0])) do |stack, stackable|
    stack.add(stackable)
  end
end

.build(&block) ⇒ Object



44
45
46
# File 'lib/sheaf/stack.rb', line 44

def build(&block)
  DSLProxy.apply(new, &block)
end

.new(*args, &block) ⇒ Object



32
33
34
# File 'lib/sheaf/stack.rb', line 32

def new(*args, &block)
  args.empty? ? Root : super
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
# File 'lib/sheaf/stack.rb', line 83

def ==(other)
  stackable == other.stackable && parent == other.parent
end

#add(stackable) ⇒ Object



79
80
81
# File 'lib/sheaf/stack.rb', line 79

def add(stackable)
  self.class.new(stackable, self)
end

#call(*args, &block) ⇒ Object



54
55
56
57
58
# File 'lib/sheaf/stack.rb', line 54

def call(*args, &block)
  parent.(*args) do |*args|
    stackable.(*args, &block || SWYM)
  end
end

#each(&block) ⇒ Object



60
61
62
63
# File 'lib/sheaf/stack.rb', line 60

def each(&block)
  parent.each(&block)
  stackable.kind_of?(self.class) ? stackable.each(&block) : yield(stackable)
end

#fmap(&block) ⇒ Object



65
66
67
# File 'lib/sheaf/stack.rb', line 65

def fmap(&block)
  self.class[*map(&block)]
end

#to_aObject



69
70
71
# File 'lib/sheaf/stack.rb', line 69

def to_a
  map { |stackable| stackable }
end

#to_sObject Also known as: inspect



73
74
75
# File 'lib/sheaf/stack.rb', line 73

def to_s
  "#<#{self.class.name}: #{to_a}>"
end