Module: Typed::Scala::Variables

Defined in:
lib/typed/scala.rb

Overview

class schema

Constant Summary collapse

ParseError =
Class.new(SyntaxError)

Class Method Summary collapse

Class Method Details

.apply(klass, type, caller, obj) ⇒ Object



59
60
61
62
# File 'lib/typed/scala.rb', line 59

def self.apply(klass, type, caller, obj)
  name = parse(klass, type, caller)
  define(klass, type, name, obj)
end

.build_attrs(klass) ⇒ Object



93
94
95
96
97
98
# File 'lib/typed/scala.rb', line 93

def self.build_attrs(klass)
  attrs = Typed::Hash.new
  klass.vals.each_pair{|name, obj| attrs[name] = obj}
  klass.vars.each_pair{|name, obj| attrs[name] = obj}
  return attrs
end

.build_variables(klass, type) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/typed/scala.rb', line 100

def self.build_variables(klass, type)
  variables = ActiveSupport::OrderedHash.new

  klass.ancestors[1 .. -1].select{|k| k < Typed::Scala}.reverse.each do |k|
    k.instance_eval("[@typed_scala_#{type}s].compact").each do |hash|
      variables.merge!(hash)
    end
  end

  return variables
end

.define(klass, type, name, obj) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/typed/scala.rb', line 64

def self.define(klass, type, name, obj)
  vars = klass.__send__("#{type}s")
  vars[name] = obj
  
  klass.class_eval do
    define_method(name) { self[name.to_s] }
    define_method("#{name}=") {|v| self[name.to_s] = v }
  end
end

.parse(klass, type, caller) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/typed/scala.rb', line 74

def self.parse(klass, type, caller)
  # "/tmp/adsvr/dsl.rb:23"
  case caller
  when %r{^(.*?):(\d+)}o
    file   = $1
    lineno = $2.to_i

    lines = (@lines ||= {})[klass] ||= File.readlines(file)
    case lines[lineno-1].to_s
    when /^\s*#{type}\s+(\S+)\s+=/
      return $1
    else
      raise ParseError, "#{self} from #{caller}"
    end
  else
    raise ParseError, "#{self} from caller:#{caller}"
  end
end