Class: Web::Scope

Inherits:
Object show all
Defined in:
lib/web/template.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: VarNotFoundException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, rootname = "", parent = nil, anAlias = "") ⇒ Scope

Returns a new instance of Scope.



46
47
48
49
50
51
# File 'lib/web/template.rb', line 46

def initialize hash, rootname ="", parent = nil, anAlias = ""
  @hash = hash
  @rootname = rootname
  @parent = parent
  @anAlias = anAlias
end

Instance Attribute Details

#anAliasObject (readonly)

Returns the value of attribute anAlias.



44
45
46
# File 'lib/web/template.rb', line 44

def anAlias
  @anAlias
end

#rootnameObject (readonly)

Returns the value of attribute rootname.



44
45
46
# File 'lib/web/template.rb', line 44

def rootname
  @rootname
end

Instance Method Details

#concat_name(rootname, name) ⇒ Object



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

def concat_name rootname, name
  if rootname != ""
	"#{rootname}.#{name}" 
  else
	"#{name}"
  end
end

#pull_value(name, collection) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/web/template.rb', line 56

def pull_value( name, collection )
  value = if collection.kind_of? Hash
		raise(VarNotFoundException.new("'#{name}' not found in #{collection.inspect}")) unless collection.has_key? name
		collection[name]
   elsif collection.kind_of? Array
		raise(VarNotFoundException.new("'#{name}' not found in #{collection.inspect}")) unless collection.has_key? name.to_i
		collection[name.to_i]
   elsif collection.respond_to? name.intern
		collection.send name.intern
   else
		raise(VarNotFoundException.new("'#{name}' not found in #{collection.inspect}"))
		#			collection.instance_eval { eval "@" + name }
   end
  
  #	    raise(VarNotFoundException.new("'#{name}' not found in #{collection.inspect}")) if value == nil

  value
end

#resolve(varname, anAlias = "") ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/web/template.rb', line 95

def resolve varname, anAlias = ""
  # strip alias off front of varname
  varname = varname.sub(/\A#{@anAlias}\./, "")
  raise "Varname is empty" if varname.empty? 
  node = begin
    scanner = StringScanner.new(varname)
    resolve_recurse scanner, @hash, @rootname
  rescue VarNotFoundException => error
    if @parent
		 @parent.resolve varname
    else
		 raise error
    end
  end
  node.anAlias = anAlias
  node.scope = self
  node
end

#resolve_recurse(scanner, hash, rootname) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/web/template.rb', line 75

def resolve_recurse scanner, hash, rootname
  scanner.scan(SEPARATOR)
  if scanner.scan(VAR)
	nodename = scanner[1]
	return Node.new(concat_name(rootname,nodename),pull_value( nodename, hash ),hash,nodename)
  elsif scanner.scan(NODE)
	nodename = scanner[1]
	return resolve_recurse(scanner, pull_value( nodename, hash ), concat_name(rootname, nodename))
  elsif scanner.scan(ARRAY_VAR)
	nodename = scanner[1]
	return Node.new(concat_name(rootname,"[" + nodename + "]"),pull_value( nodename, hash ),hash,nodename)
  elsif scanner.scan(ARRAY)
	#TODO check is hash->array
	index = scanner[1]
	return resolve_recurse(scanner, hash[index.to_i], "#{rootname}[#{index}]")
  else
	raise(scanner.rest)
  end
end

#update(submitted) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/web/template.rb', line 114

def update 
  .keys.each { |param|
	begin
	  var = resolve param
	  if var.parent.kind_of? Hash
 var.parent[var.name] = [param]
	  elsif var.parent.kind_of? Array
 var.parent[var.name.to_i] = [param]
	  else
 var.parent.send( (var.name + "=").intern, [param] )
	  end
	rescue VarNotFoundException
	end
  }
  @hash
end