标签:des style class blog c code
ImLazy是个人使用的小程序,抽时间在公司写了个ruby版本,未完成
equals_verb
# To change this template, choose Tools | Templates # and open the template in the editor. require "SDK/verb" require "Addins/lexer_condition" module ImLazy class EqualsVerb < Verb def initialize self.name = "Equals" end def match?(left, right) return left.eql?(right) end end LexerCondition.register_lexer(EqualsVerb.new) end
extension_subject
# To change this template, choose Tools | Templates # and open the template in the editor. require "SDK/subject" require "Addins/lexer_condition" module ImLazy class ExtensionSubject < Subject def initialize self.name = "Extension" end def property (path) return File.extname(path) end end LexerCondition.register_lexer(ExtensionSubject.new) end
lexer_condition
# To change this template, choose Tools | Templates # and open the template in the editor. require "SDK/common" require "Runtime/cache_map" module ImLazy class LexerCondition < Condition @@lexers = CacheMap.new # LexerCondition.register_lexer(lexer) => nil # # # === Parameters # * +lexer+ a subject or verb # # This method will register a lexer # def self.register_lexer lexer key = lexer.class.name logd "lexer [#{key}] is registed" @@lexers[key] = lexer end def initialize self.name = "LexerCondition" end def match? (path, config) subject_addin_name = config["subject"] verb_addin_name = config["verb"] object_value = config["object"] return false unless(check_param(subject_addin_name, verb_addin_name)) subject = @@lexers[subject_addin_name] verb = @@lexers[verb_addin_name] return false unless(check_param(subject, verb)) property = subject.property(path) result = verb.match?(property, object_value) logd "matched : #{property} #{verb.name} #{object_value}" if result return result end def check_param(*params) params.each do |param| if(param == nil || param == "") loge "[#{param}] is empty!" return false end end end end # register this condition addin Executor.register_condition LexerCondition.new end
move_action
# To change this template, choose Tools | Templates # and open the template in the editor. require "SDK/common" require "SDK/action" module ImLazy class MoveAction < Action def initialize end def perform(path, config) destination = config["TargetValue"] File.move(path, destination, true) end end # register this condition addin Executor.register_action MoveAction.new end
addin_info
# To change this template, choose Tools | Templates # and open the template in the editor. module ImLazy class AddinInfo attr_accessor :addin_name attr_accessor :config def initialize end end end
folder
# To change this template, choose Tools | Templates # and open the template in the editor. module ImLazy class Folder attr_accessor :path, :rule_ids def initialize _path self.path = _path self.rule_ids = [] end end end
rule
# To change this template, choose Tools | Templates # and open the template in the editor. require ‘securerandom‘ module ImLazy class Rule attr_accessor :condition, :action , :guid, :name def initialize self.guid = SecureRandom.hex(13) end end end
cache_map
# To change this template, choose Tools | Templates # and open the template in the editor. module ImLazy class CacheMap < Hash def initialize end end end
data_storage
# To change this template, choose Tools | Templates # and open the template in the editor. require "Data/rule" require "Data/addin_info" module ImLazy class DataStorage attr_accessor :rules, :conditions, :actions, :folders def initialize self.rules = [] self.conditions = [] self.actions = [] self.folders = [] end def load_test_data rule = Rule.new; rule.name = "rule 1" addin = AddinInfo.new addin.addin_name = "ImLazy::LexerCondition"; addin.config = { "subject"=>"ImLazy::ExtensionSubject", "verb"=>"ImLazy::EqualsVerb", "object"=>".bat" } rule.condition = addin; self.rules.push(rule) folder = Folder.new("C:/") folder.rule_ids.push rule.guid self.folders.push(folder) end def self.json_create(o) new(*o[‘data‘]) end def to_json(*a) { ‘json_class‘ => self.class.name, ‘data‘ => [rules] }.to_json(*a) end end end
executor
# To change this template, choose Tools | Templates # and open the template in the editor. require "Runtime/cache_map" require "Data/folder" require "SDK/common" require "thread" module ImLazy class Executor @@conditions = CacheMap.new @@actions = CacheMap.new @@rules = CacheMap.new def initialize load_addins end def self.register_rule rule key = rule.guid logd("rule [#{rule.name}] is registed") @@rules[key] = rule end def self.register_condition addin key = addin.class.name logd("condition addin [#{key}] is registed") @@conditions[key] = addin end def self.register_action addin key = addin.class.name logd("action addin [#{key}] is registed") @@actions[key] = addin end def load_addins Dir.foreach("Addins") do | path | require("Addins/" + path) if File.extname(path) == ".rb" end end def execute (folders) logd "executing..." folders.each do |folder| logd "enumerating folder #{folder.path}" Dir.foreach(folder.path) do | path | Thread.new do folder.rule_ids.each do | rule_id | rule = @@rules[rule_id] key = rule.condition.addin_name config = rule.condition.config if @@conditions.has_key?(key) if @@conditions[key].match?(path, config) logd "mock action has been performed." end else loge "condition witk key [#{key}] not found!" end end end end end logd "done executing." end end end
action
# To change this template, choose Tools | Templates # and open the template in the editor. module ImLazy class Action attr_accessor :name def initialize end def perform(path, config) end end end
common
# To change this template, choose Tools | Templates # and open the template in the editor. require "Runtime/executor" require "SDK/condition" require "Utils/log"
condition
# To change this template, choose Tools | Templates # and open the template in the editor. module ImLazy class Condition attr_accessor :name def initialize end def match? (path, config) return false end end end
lexer_term
# To change this template, choose Tools | Templates # and open the template in the editor. module ImLazy class LexerTerm attr_accessor :name def initialize end end end
subject
# To change this template, choose Tools | Templates # and open the template in the editor. require "SDK/lexer_term" module ImLazy class Subject < LexerTerm def initialize end def property (path) end end end
verb
# To change this template, choose Tools | Templates # and open the template in the editor. require "SDK/lexer_term" module ImLazy class Verb < LexerTerm def initialize end def match?(left, right) return false end end end
log
# To change this template, choose Tools | Templates # and open the template in the editor. require "logger" class Object @@logger = Logger.new(STDOUT) def logd msg @@logger.debug(self.class.name) { msg } end def loge msg @@logger.error(self.class.name) { msg } end end
main
# To change this template, choose Tools | Templates # and open the template in the editor. require "Runtime/executor" require "Runtime/data_storage" module ImLazy ds = DataStorage.new ds.load_test_data ds.rules.each do |rule| Executor.register_rule(rule) end exe = Executor.new exe.execute ds.folders end
标签:des style class blog c code
原文地址:http://www.cnblogs.com/ornithopter/p/3748222.html