Class: MarkUS
- Inherits:
-
Object
- Object
- MarkUS
- Defined in:
- lib/markus.rb
Overview
Markup UnderScore —- - -
Class Attribute Summary collapse
-
.__markus_file ⇒ Object
Returns the value of attribute __markus_file.
-
.__markus_includes ⇒ Object
Returns the value of attribute __markus_includes.
-
.__markus_indent ⇒ Object
Returns the value of attribute __markus_indent.
-
.__markus_reload ⇒ Object
Returns the value of attribute __markus_reload.
-
.__markus_reload_timestamp ⇒ Object
Returns the value of attribute __markus_reload_timestamp.
-
.__markus_templates ⇒ Object
Returns the value of attribute __markus_templates.
Instance Attribute Summary collapse
-
#__markus_indent ⇒ Object
Returns the value of attribute __markus_indent.
-
#__markus_reload ⇒ Object
Returns the value of attribute __markus_reload.
Class Method Summary collapse
-
.__markus_do_reload ⇒ Object
}}}.
-
.indent ⇒ Object
}}}.
-
.inherited(subclass) ⇒ Object
}}}.
-
.reload ⇒ Object
}}}.
-
.template(name, &p) ⇒ Object
}}}.
-
.templates(some) ⇒ Object
}}}.
Instance Method Summary collapse
-
#__markus_json(tname, *args, &blk) ⇒ Object
}}}.
-
#__markus_method_missing(name, *args, &blk) ⇒ Object
}}}.
-
#__markus_xml(tname, *args) ⇒ Object
}}}.
-
#element_!(name = nil, *args, &blk) ⇒ Object
}}}.
-
#html_!(name, params = {}) ⇒ Object
}}}.
-
#json_!(name, params = {}) ⇒ Object
}}}.
-
#markus_!(name, type, params = {}) ⇒ Object
}}}.
-
#method_missing(name, *args, &blk) ⇒ Object
}}}.
-
#template_!(name, *args) ⇒ Object
}}}.
-
#xml_!(name, params = {}) ⇒ Object
}}}.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &blk) ⇒ Object
}}}
79 80 81 82 83 84 85 |
# File 'lib/markus.rb', line 79 def method_missing(name,*args, &blk) #{{{ # :nodoc: if name.to_s =~ /(.*)(__)$/ || name.to_s =~ /(.*)(_)$/ __markus_method_missing $1, *args, &blk else super end end |
Class Attribute Details
.__markus_file ⇒ Object
Returns the value of attribute __markus_file.
24 25 26 |
# File 'lib/markus.rb', line 24 def __markus_file @__markus_file end |
.__markus_includes ⇒ Object
Returns the value of attribute __markus_includes.
27 28 29 |
# File 'lib/markus.rb', line 27 def __markus_includes @__markus_includes end |
.__markus_indent ⇒ Object
Returns the value of attribute __markus_indent.
26 27 28 |
# File 'lib/markus.rb', line 26 def __markus_indent @__markus_indent end |
.__markus_reload ⇒ Object
Returns the value of attribute __markus_reload.
22 23 24 |
# File 'lib/markus.rb', line 22 def __markus_reload @__markus_reload end |
.__markus_reload_timestamp ⇒ Object
Returns the value of attribute __markus_reload_timestamp.
23 24 25 |
# File 'lib/markus.rb', line 23 def end |
.__markus_templates ⇒ Object
Returns the value of attribute __markus_templates.
25 26 27 |
# File 'lib/markus.rb', line 25 def __markus_templates @__markus_templates end |
Instance Attribute Details
#__markus_indent ⇒ Object
Returns the value of attribute __markus_indent.
30 31 32 |
# File 'lib/markus.rb', line 30 def __markus_indent @__markus_indent end |
#__markus_reload ⇒ Object
Returns the value of attribute __markus_reload.
30 31 32 |
# File 'lib/markus.rb', line 30 def __markus_reload @__markus_reload end |
Class Method Details
.__markus_do_reload ⇒ Object
}}}
280 281 282 283 284 285 286 287 288 |
# File 'lib/markus.rb', line 280 def self:: __markus_do_reload #{{{ # :nodoc: if self.__markus_reload && self. if File.stat(self.__markus_file).mtime > self. load self.__markus_file return true end end false end |
.indent ⇒ Object
}}}
294 295 296 |
# File 'lib/markus.rb', line 294 def self::indent #{{{ self.__markus_indent = true end |
.inherited(subclass) ⇒ Object
}}}
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/markus.rb', line 251 def self::inherited(subclass) #{{{ # :nodoc: subclass.instance_eval do |i| # This array contains strings like "/path/to/a.rb:3:in `instance_eval'". strings_ary = caller # We look for the last string containing "<top (required)>". # only works in > 1.9 val = "<top (required)>" until require_index = strings_ary.index {|x| x.include?(val) } val = "<main>" end require_string = strings_ary[require_index] filepath = File.(require_string[/^(.*):\d+:in/, 1]) # We use a regex to extract the filepath from require_string. Other defaults. self.__markus_reload = false self. = nil self.__markus_file = filepath self.__markus_templates = {} self.__markus_indent = false self.__markus_includes = [] end end |
.reload ⇒ Object
}}}
276 277 278 279 |
# File 'lib/markus.rb', line 276 def self::reload #{{{ self.__markus_reload = true self. = File.stat(self.__markus_file).mtime end |
.template(name, &p) ⇒ Object
}}}
290 291 292 293 |
# File 'lib/markus.rb', line 290 def self::template(name,&p) #{{{ self.__markus_templates ||= {} self.__markus_templates[name] = p end |
.templates(some) ⇒ Object
}}}
298 299 300 301 |
# File 'lib/markus.rb', line 298 def self::templates(some) self.__markus_includes << some self.__markus_templates.merge! some.__markus_templates end |
Instance Method Details
#__markus_json(tname, *args, &blk) ⇒ Object
}}}
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/markus.rb', line 147 def __markus_json(tname,*args,&blk) #{{{ # :nodoc: attrs = nil content = "null" args.each do |a| case a when Array attrs = "[ " + a.collect { |value| case value when Integer, Float value.nil? ? nil : value.to_s else value.nil? ? nil : "\"#{value.to_s.gsub(/"/,'\\\"')}\"" end }.compact.join(", ").strip + " ]" attrs = '[]' if attrs == "[ ]" when Hash attrs = "{ " + a.collect { |key,value| case value when Integer, Float value.nil? ? nil : "\"#{key}\": #{value}" else value.nil? ? nil : "\"#{key}\": \"#{value.to_s.gsub(/"/,'\\\"')}\"" end }.compact.join(", ").strip + " }" attrs = '{}' if attrs == "{ }" when String content = "\"#{a.gsub(/"/,'\\\"')}\"" when Integer, Float content = a else content = "\"#{a.to_s}\"" end end if blk @__markus_level += 1 mpsic = @__markus_parent if mpsic == :a && !tname.nil? @__markus_parent = nil if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}{" else @__markus_buffer << "{" end __markus_json tname, *args, &blk @__markus_buffer.last.chomp!(',') if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}}," else @__markus_buffer << "}," end else @__markus_parent = type = blk.parameters.length == 1 && blk.parameters[0][1] == :array ? :a : :h if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}" else @__markus_buffer << "#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}" end c1 = @__markus_buffer.length res = blk.call c2 = @__markus_buffer.length if c1 == c2 @__markus_buffer.last << "#{type == :a ? ']' : '}'}," else @__markus_buffer << res + ',' if type == :a && res.is_a?(String) @__markus_buffer.last.chomp!(',') if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}#{type == :a ? ']' : '}'}," else @__markus_buffer << "#{type == :a ? ']' : '}'}," end end end @__markus_level -= 1 @__markus_parent = mpsic else if @__markus_parent == :a @__markus_level += 1 if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}{" else @__markus_buffer << "{" end if self.class.__markus_indent @__markus_buffer << "#{" " * (@__markus_level+1)}\"#{tname}\": #{attrs || content}" else @__markus_buffer << "\"#{tname}\": #{attrs || content}" end if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}}," else @__markus_buffer << "}," end @__markus_level -= 1 else if self.class.__markus_indent @__markus_buffer << "#{" " * (@__markus_level+1)}\"#{tname}\": #{attrs || content}," else @__markus_buffer << "\"#{tname}\": #{attrs || content}," end end end end |
#__markus_method_missing(name, *args, &blk) ⇒ Object
}}}
86 87 88 89 90 91 92 |
# File 'lib/markus.rb', line 86 def __markus_method_missing(name,*args, &blk) #{{{ # :nodoc: if @__markus_mode == :json __markus_json name, *args, &blk else __markus_xml name, *args, &blk end end |
#__markus_xml(tname, *args) ⇒ Object
}}}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/markus.rb', line 93 def __markus_xml(tname,*args) #{{{ # :nodoc: attrs = "" content = nil args.each do |a| case a when Hash attrs << " " + a.collect { |key,value| value.nil? ? nil : "#{key}=\"#{value.to_s.gsub(/"/,""")}\"" }.compact.join(" ") when String content = a.gsub(/\&(?!amp;|quot;|apos;|gt;|lt;)/,"&").gsub(/\"/,""").gsub(/>/,">").gsub(/</,"<").gsub(/'/,"'") when Integer content = a end end attrs = '' if attrs == ' ' if block_given? @__markus_level += 1 if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}<#{tname}#{attrs}>" else @__markus_buffer << "<#{tname}#{attrs}>" end unless content.nil? if self.class.__markus_indent @__markus_buffer << "#{" " * (@__markus_level+1)}#{content}" else @__markus_buffer << "#{content}" end end res = yield @__markus_buffer << res if String === res if self.class.__markus_indent @__markus_buffer << "#{" " * @__markus_level}</#{tname}>" else @__markus_buffer << "</#{tname}>" end @__markus_level -= 1 else if @__markus_mode == :xml && content.nil? if self.class.__markus_indent @__markus_buffer << "#{" " * (@__markus_level+1)}<#{tname}#{attrs}/>" else @__markus_buffer << "<#{tname}#{attrs}/>" end else if self.class.__markus_indent @__markus_buffer << "#{" " * (@__markus_level+1)}<#{tname}#{attrs}>#{content}</#{tname}>" else @__markus_buffer << "<#{tname}#{attrs}>#{content}</#{tname}>" end end end end |
#element_!(name = nil, *args, &blk) ⇒ Object
}}}
72 73 74 |
# File 'lib/markus.rb', line 72 def element_!(name=nil, *args, &blk) #{{{ __markus_method_missing name, *args, &blk end |
#html_!(name, params = {}) ⇒ Object
}}}
44 45 46 |
# File 'lib/markus.rb', line 44 def html_!(name,params={}) #{{{ markus_! name, :html, params end |
#json_!(name, params = {}) ⇒ Object
}}}
38 39 40 |
# File 'lib/markus.rb', line 38 def json_!(name,params={}) #{{{ markus_! name, :json, params end |
#markus_!(name, type, params = {}) ⇒ Object
}}}
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/markus.rb', line 48 def markus_!(name,type,params={}) #{{{ @__markus = [] @__markus_buffer = [] @__markus_level = -1 @__markus_parent = nil params.each do |k,v| self.instance_variable_set(("@" + k.to_s).to_sym, v) end if params.is_a? Hash self.class.__markus_do_reload self.class.__markus_includes.each do |some| if some.__markus_do_reload self.class.__markus_templates.merge! some.__markus_templates end end @__markus_mode = type template_!(name) @__markus_buffer.last.chomp!(',') if @__markus_mode == :json self.class.__markus_indent ? @__markus_buffer.join("\n") : @__markus_buffer.join end |
#template_!(name, *args) ⇒ Object
}}}
75 76 77 |
# File 'lib/markus.rb', line 75 def template_!(name,*args) #{{{ instance_exec *args, &self.class.__markus_templates[name] end |
#xml_!(name, params = {}) ⇒ Object
}}}
41 42 43 |
# File 'lib/markus.rb', line 41 def xml_!(name,params={}) #{{{ markus_! name, :xml, params end |