码迷,mamicode.com
首页 > 其他好文 > 详细

ImLazy(ruby)

时间:2014-05-24 12:51:59      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   

ImLazy是个人使用的小程序,抽时间在公司写了个ruby版本,未完成

bubuko.com,布布扣

equals_verb

bubuko.com,布布扣
# 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
bubuko.com,布布扣

extension_subject

bubuko.com,布布扣
# 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
bubuko.com,布布扣

lexer_condition

bubuko.com,布布扣
# 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
bubuko.com,布布扣

move_action

bubuko.com,布布扣
# 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
bubuko.com,布布扣

addin_info

bubuko.com,布布扣
# 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
bubuko.com,布布扣

folder

bubuko.com,布布扣
# 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
bubuko.com,布布扣

rule

bubuko.com,布布扣
# 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
bubuko.com,布布扣

cache_map

bubuko.com,布布扣
# To change this template, choose Tools | Templates
# and open the template in the editor.

module ImLazy
  class CacheMap < Hash
    def initialize
      
    end
  end
end
bubuko.com,布布扣

data_storage

bubuko.com,布布扣
# 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
bubuko.com,布布扣

executor

bubuko.com,布布扣
# 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
bubuko.com,布布扣

action

bubuko.com,布布扣
# 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
bubuko.com,布布扣

common

bubuko.com,布布扣
# To change this template, choose Tools | Templates
# and open the template in the editor.

require "Runtime/executor"
require "SDK/condition"
require "Utils/log"
bubuko.com,布布扣

condition

bubuko.com,布布扣
# 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
bubuko.com,布布扣

lexer_term

bubuko.com,布布扣
# 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
bubuko.com,布布扣

subject

bubuko.com,布布扣
# 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
bubuko.com,布布扣

verb

bubuko.com,布布扣
# 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
bubuko.com,布布扣

log

bubuko.com,布布扣
# 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
bubuko.com,布布扣

main

bubuko.com,布布扣
# 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
bubuko.com,布布扣

 

ImLazy(ruby),布布扣,bubuko.com

ImLazy(ruby)

标签:des   style   class   blog   c   code   

原文地址:http://www.cnblogs.com/ornithopter/p/3748222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!