class Compile
Public Class Methods
new(argument)
click to toggle source
# File lib/aml/Compile.rb, line 3 def initialize(argument) @argument = argument file = @argument.get('build') @prepare = Prepare.new(file) @inline = [ {:type=> :attribute, :regex=> /@\(\:(?<name>[\w|\-]+)\)/}, {:type=> :method, :regex=> /::((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)(\{(?<attribute>.+)\})?/}, {:type=> :variable, :regex=> /\@\(((?<bundle>[\w|\-]+)\.)?(?<name>[\w|\-]+)\)/} ] @prepare.cluster.variables['false'] = {} if @prepare.cluster.variables.include?('false') == false local = [ {:name=> 'file-created', :value=> Time.new.strftime('%Y-%m-%d %H:%M:%S')}, {:name=> 'file-name', :value=> File.basename(file)}, {:name=> 'file-path', :value=> File.expand_path(file)} ].each do |variable| @prepare.cluster.variables['false'][variable[:name].to_s] = [{:number=>0, :value=>variable[:value].to_s}] end @log = [] @prepare.log.each do |log| if log[:type] == "mixin" @log << log if log[:bundle] != false else @log << log end end if @log.count == 0 process @prepare.cluster.post_process end end
Public Instance Methods
log()
click to toggle source
# File lib/aml/Compile.rb, line 33 def log @log end
post_process()
click to toggle source
# File lib/aml/Compile.rb, line 49 def post_process @prepare.cluster.definition.each do |definition| definition.self[:hash].each_with_index do |line, index| process_variable_and_method(line, index, definition, true) end end end
process()
click to toggle source
# File lib/aml/Compile.rb, line 39 def process @prepare.cluster.definition.each do |definition| definition.self[:hash].each_with_index do |line, index| process_variable_and_method(line, index, definition) process_mixin(line, index, definition) process_partial(line, index, definition) end end process if @prepare.cluster.definition.select{|k|k.self[:type] == "base"}.first.self[:hash].select{|k|k[:type] == :mixin or k[:type] == :partial}.count > 0 end
structure()
click to toggle source
# File lib/aml/Compile.rb, line 36 def structure @prepare.cluster.definition.select{|k|k.self[:type] == "base"}.first.self[:hash] end
Private Instance Methods
has_match?(string, regex)
click to toggle source
# File lib/aml/Compile.rb, line 57 def has_match?(string, regex) return string.to_s.match(regex) != nil end
process_attribute(line, mixin)
click to toggle source
# File lib/aml/Compile.rb, line 201 def process_attribute(line, mixin) if line[:type] == :string line[:value] = process_attribute_find(line[:value], mixin[:attribute]) else line[:text] = process_attribute_find(line[:text], mixin[:attribute]) if line[:attribute] != nil line[:attribute] = recursive_attribute_replace_attribute(line[:attribute],mixin[:attribute]) end end line end
process_attribute_find(string, attribute)
click to toggle source
# File lib/aml/Compile.rb, line 213 def process_attribute_find(string, attribute) regex = @inline.select{|k|k[:type] == :attribute}[0][:regex] string = string.to_s.gsub(regex).each do attribute[$1.to_sym].to_s end string = process_attribute_find(string,attribute) if has_match?(string, regex) return string end
process_method_find(string, line, definition)
click to toggle source
# File lib/aml/Compile.rb, line 125 def process_method_find(string, line, definition) regex = @inline.select{|k|k[:type] == :method}[0][:regex] string = string.to_s.gsub(regex).each do process_method_replace($1, $2, $3, line[:index], definition) end string = process_method_find(string, line, definition) if has_match?(string, regex) return string end
process_method_line(line, definition)
click to toggle source
# File lib/aml/Compile.rb, line 104 def process_method_line(line, definition) if line[:type] == :string line[:value] = process_method_find(line[:value], line, definition) parse = Parse.new(definition.self[:bundle]) line = parse.line("#{"\t" * line[:index]}#{line[:value]}",line[:number]) else line[:text] = process_method_find(line[:text], line, definition) if line.key? :attribute and line[:attribute].count > 0 line[:attribute] = recursive_attribute_replace_method(line[:attribute], line, definition) end end line end
process_method_replace(bundle, name, attribute, index, definition)
click to toggle source
# File lib/aml/Compile.rb, line 143 def process_method_replace(bundle, name, attribute, index, definition) bundle = 'core' if bundle == nil begin file = bundle == 'core' ? File.join(File.dirname(File.expand_path(__FILE__)),'core','method.rb') : File.join(File.dirname(File.expand_path(definition.self[:file])),bundle,'method.rb') Compile.class_eval File.read(file) method_attribute = Line.new(bundle, :string, @inline.select{|k|k[:type] == :method}[0][:regex]) method_attribute = method_attribute.match?("::#{bundle}.#{name}{#{attribute}}", index) method_attribute[:attribute] = {:value=>attribute.to_s} if method_attribute[:attribute].count == 0 attribute = method_attribute[:attribute] Compile.const_get(bundle.split('-').map{|k|k.capitalize}.join).method(name).call(index, attribute) rescue Exception => e return nil end end
process_mixin(line, index, definition)
click to toggle source
# File lib/aml/Compile.rb, line 157 def process_mixin(line, index, definition) if line[:type] == :mixin definition.self[:hash].delete_at index mixin = process_mixin_find(line, line[:index], definition) if mixin.is_a? Hash mixin[:structure].each_with_index do |mixin_line, mixin_index| definition.self[:hash].insert(index+mixin_index, mixin_line) end else @log << {:fail=>false, :file=>definition.self[:file], :bundle=>definition.self[:bundle], :line=>line[:number], :message=>"#{line[:bundle] ? line[:bundle]+ '.' : nil}#{line[:name]} mixin does not exist; ignored."} end end end
process_mixin_find(line, offset=0, definition)
click to toggle source
# File lib/aml/Compile.rb, line 170 def process_mixin_find(line, offset=0, definition) begin mixin = Marshal.load(Marshal.dump(@prepare.cluster.mixins.reject{|k|k != line[:bundle].to_s}.first.last.reject{|k|k != line[:name]}.first.last)) mixin[:attribute] = mixin[:attribute].merge(line[:attribute]) mixin[:attribute].each do |attribute, value| mixin[:attribute][attribute] = process_variable_find(value, line, definition) end mixin[:structure].each do |mixin_line| mixin_line[:index] += offset mixin_line[:number] = line[:number] mixin_line = process_attribute(mixin_line, mixin) mixin_line = process_variable_line(mixin_line, definition) end return mixin rescue return false end end
process_partial(line, index, definition)
click to toggle source
# File lib/aml/Compile.rb, line 221 def process_partial(line, index, definition) if line[:type] == :partial definition.self[:hash].delete_at index partial = process_partial_find(line, line[:index], definition) if partial.is_a? Array partial.each_with_index do |partial_line,partial_index| definition.self[:hash].insert(index+partial_index,partial_line) end end end end
process_partial_find(line, offset, definition)
click to toggle source
# File lib/aml/Compile.rb, line 232 def process_partial_find(line, offset, definition) path = line[:bundle].to_s == false.to_s ? "" : line[:bundle].to_s partial_number = line[:number] patrial_attribute = line[:attribute] partial = Definition.new(File.join(@argument.get('path'),path, "partial", line[:name]+'.aml'), line[:type].to_s, line[:bundle]) partial.self[:hash].each_with_index do |line, index| line[:number] = partial_number line = process_attribute(line, {:attribute=>patrial_attribute}) if patrial_attribute.count > 0 line = process_variable_line(line, definition) end partial.self[:hash].each do|line| line[:index] += offset end partial.self[:hash] end
process_variable_and_method(line, index, definition, methods=false)
click to toggle source
# File lib/aml/Compile.rb, line 60 def process_variable_and_method(line, index, definition, methods=false) definition.self[:hash][index] = process_variable_line(line, definition) definition.self[:hash][index] = process_method_line(line, definition) if methods end
process_variable_find(string, line, definition)
click to toggle source
# File lib/aml/Compile.rb, line 117 def process_variable_find(string, line, definition) regex = @inline.select{|k|k[:type] == :variable}[0][:regex] string = string.to_s.gsub(regex).each do process_variable_replace($1, $2, line[:number], definition) end string = process_variable_find(string, line, definition) if has_match?(string, regex) return string end
process_variable_line(line, definition)
click to toggle source
# File lib/aml/Compile.rb, line 77 def process_variable_line(line, definition) if line[:type] == :string line[:value] = process_variable_find(line[:value], line, definition) parse = Parse.new(definition.self[:bundle]) line = parse.line("#{"\t" * line[:index]}#{line[:value]}",line[:number]) else line[:text] = process_variable_find(line[:text], line, definition) line[:value] = process_variable_find(line[:value], line, definition) if line.key? :attribute and line[:attribute].count > 0 line[:attribute] = recursive_attribute_replace_variable(line[:attribute], line, definition) end end line end
process_variable_replace(bundle, name, number, definition)
click to toggle source
# File lib/aml/Compile.rb, line 133 def process_variable_replace(bundle, name, number, definition) bundle = false.to_s if bundle == nil number = 0 if bundle != "false" begin @prepare.cluster.variables.reject{|k| k != bundle}.first.last.reject{|k| k != name}.first.last.select{|k| if number != 0 then k[:number] < number else k[:number] == k[:number] end}.last[:value] rescue @log << {:fail=>false, :file=>definition.self[:file], :bundle=>definition.self[:bundle], :line=>number, :message=>"#{bundle.to_s != false.to_s ? bundle+"." : nil}#{name} variable does not exist; ignored."} return nil end end
recursive_attribute_replace_attribute(attributes, mixin)
click to toggle source
# File lib/aml/Compile.rb, line 189 def recursive_attribute_replace_attribute(attributes, mixin) attributes.each do |attribute, value| if value.is_a? Hash value = recursive_attribute_replace_attribute(value, mixin) else value = process_attribute_find(value.to_s, mixin) end attributes[attribute] = value end attributes end
recursive_attribute_replace_method(attributes, line, definition)
click to toggle source
# File lib/aml/Compile.rb, line 92 def recursive_attribute_replace_method(attributes, line, definition) attributes.each do |attribute, value| if value.is_a? Hash value = recursive_attribute_replace_method(value, line, definition) else value = process_method_find(value.to_s, line, definition) end attributes[attribute] = value end attributes end
recursive_attribute_replace_variable(attributes, line, definition)
click to toggle source
# File lib/aml/Compile.rb, line 65 def recursive_attribute_replace_variable(attributes, line, definition) attributes.each do |attribute, value| if value.is_a? Hash value = recursive_attribute_replace_variable(value, line, definition) else value = process_variable_find(value.to_s, line, definition) end attributes[attribute] = value end attributes end