Module: XRay::Rule

Constant Summary collapse

STYLE_TYPES =
[:css, :js, :html]
RULE_PATH =
File.expand_path '../rules.d', File.dirname(__FILE__)
KEYWORDS =
%w(file merge_importing merge_file)
@@common_rules =
[]
@@context =
nil
@@imported =
[]

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/rule.rb', line 128

def method_missing( name , *args, &block )
  if cmd = cmd_name(name) and self.respond_to? cmd 
    send(cmd, name, &block )
    def_rule_cmd(cmd, name, &block) if block_given?
  else
    super
  end
end

Class Method Details

.import_allObject



196
197
198
# File 'lib/rule.rb', line 196

def self.import_all
  "".extend(self).import_all
end

.methods_to_keywords(klass) ⇒ Object



14
15
16
# File 'lib/rule.rb', line 14

def self.methods_to_keywords(klass)
  klass.instance_methods.grep( /^parse_/ ).map {|m| m[/^parse_(.*)/,1]}
end

Instance Method Details

#add_common_rule(name, &block) ⇒ Object Also known as: add_rule



166
167
168
# File 'lib/rule.rb', line 166

def add_common_rule( name, &block )
  @@common_rules << { :name => name , :block => block }
end

#clear_all_rulesObject



108
109
110
111
112
# File 'lib/rule.rb', line 108

def clear_all_rules
  @@imported.clear
  @@common_rules.clear
  STYLE_TYPES.each { |syn| send(:"clear_#{syn}_rules") }
end

#cmd_name(name) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rule.rb', line 151

def cmd_name(name)
  name = name.to_s
  KEYWORDS.each do |kw| 
    if name.start_with? "check_#{kw}" 
      return :"check_#{kw}"
    else 
      STYLE_TYPES.each do |s| 
        cmd = "check_#{s}_#{kw}"
        return cmd if name.start_with? cmd
      end
    end
  end
  nil
end

#common(&block) ⇒ Object



36
37
38
39
# File 'lib/rule.rb', line 36

def common(&block)
  syntax nil
  yield if block_given?
end

#contextObject



124
125
126
# File 'lib/rule.rb', line 124

def context
  @@context
end

#do_check(rules, *args) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/rule.rb', line 114

def do_check( rules, *args )
  @@context = self
  target = args.first
  rules.inject([]) do |results, r|
    result = r[:block].call(*args)
    results << result if result
    results
  end
end

#import(name) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rule.rb', line 176

def import( name )
  if name.is_a? Symbol
    path = File.join(RULE_PATH, "#{name}.rule")
  else
    path = name
  end
  path = File.expand_path path
  unless imported? path
    @@imported << path
    src, enc = ::XRay::Helper::FileReader.readfile path
    instance_eval src
  end
end

#import_allObject



190
191
192
193
194
# File 'lib/rule.rb', line 190

def import_all
  Find.find( RULE_PATH ) do |rule|
    import rule if rule.end_with? '.rule'
  end
end

#importedObject



204
205
206
# File 'lib/rule.rb', line 204

def imported
  @@imported
end

#imported?(path) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/rule.rb', line 200

def imported?(path)
  @@imported.include? path
end

#syntax(*type) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rule.rb', line 28

def syntax(*type)
  if type.empty?
    @syntax || 'common'
  else
    @syntax = type.first
  end
end