Class: CastOff::Compiler::Configuration::BindingWrapper

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/cast_off/compile/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#bug, #dlog, #todo, #vlog

Constructor Details

#initialize(bind) ⇒ BindingWrapper

Returns a new instance of BindingWrapper.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cast_off/compile/configuration.rb', line 31

def initialize(bind)
  bug() unless bind.instance_of?(Binding)
  @bind = bind
  nest = eval("Module.nesting", @bind)
  pre = ""
  @nest = nest.reverse.map{|n|
    n = n.to_s
    if n[pre]
      replen = pre.length
      replen += 2 unless pre.empty?
      pre = n
      n.slice(replen, n.length - replen)
    else
      n
    end
  }
  begin
    marshal_load(@nest) # validation
  rescue NameError => e
    raise(UnsupportedError.new(<<-EOS))

Failed to construct binding from Module.nesting result (#{nest}).
Currently, CastOff doesn't support binding which cannot construct from Module.nesting result.
CastOff constructs binding with the following process.

--- binding construction process ---
o = Object
#{@nest.map{|n| "o = o.const_get(:#{n})" }.join("\n")}

--- error message ---
#{e.message}
    EOS
  rescue => e
    bug("e = #{e}, @nest = #{@nest}, nest = #{nest}")
  end
end

Instance Attribute Details

#bindObject (readonly)

Returns the value of attribute bind.



29
30
31
# File 'lib/cast_off/compile/configuration.rb', line 29

def bind
  @bind
end

#nestObject (readonly)

Returns the value of attribute nest.



29
30
31
# File 'lib/cast_off/compile/configuration.rb', line 29

def nest
  @nest
end

Instance Method Details

#==(other) ⇒ Object



110
111
112
# File 'lib/cast_off/compile/configuration.rb', line 110

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/cast_off/compile/configuration.rb', line 105

def eql?(other)
  return false unless other.instance_of?(BindingWrapper)
  @nest == other.nest
end

#hashObject



114
115
116
# File 'lib/cast_off/compile/configuration.rb', line 114

def hash()
  BindingWrapper.hash
end

#marshal_dumpObject



68
69
70
# File 'lib/cast_off/compile/configuration.rb', line 68

def marshal_dump()
  @nest
end

#marshal_load(nest) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cast_off/compile/configuration.rb', line 72

def marshal_load(nest)
  o = Object
  ary = []
  nest.each do |n|
    n.split("::").each do |__n|
      o = o.const_get(__n)
    end
    ary << [n, o]
  end
  s0 = []
  s1 = []
  ary.each_with_index do |no, idx|
    n, o = no
    case o
    when Class
      decl = "class"
    when Module
      decl = "module"
    else
      bug("obj = #{o}")
    end
    indent = "  " * idx
    s0 << indent + "#{decl} #{n}"
    s1 << indent + "end"
  end
  indent = "  " * ary.size
  eval_str = s0.join("\n") + "\n#{indent}binding\n" + s1.reverse.join("\n")
  bind = eval(eval_str, TOPLEVEL_BINDING)
  bug() unless bind.instance_of?(Binding)
  @bind = bind
  @nest = nest
end