Class: JsonBuilder::JsonBase
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- JsonBuilder::JsonBase
- Defined in:
- lib/json_builder/jsonbase.rb
Overview
JsonBase is a base class for building XML builders. See JsonBuilder::JsonMarkup and JsonBuilder::JsonEvents for examples.
Direct Known Subclasses
Instance Method Summary collapse
-
#<<(text) ⇒ Object
Append text to the output target without escaping any markup.
-
#initialize(encoding = 'utf-8') ⇒ JsonBase
constructor
Create an XML markup builder.
-
#method_missing(sym, *args, &block) ⇒ Object
Create XML markup based on the name of the method.
-
#nil? ⇒ Boolean
For some reason, nil? is sent to the JsonMarkup object.
-
#tag!(sym, *args, &block) ⇒ Object
Create a tag named
sym
. -
#text!(text) ⇒ Object
Append text to the output target.
Methods inherited from BlankSlate
find_hidden_method, hide, reveal
Constructor Details
#initialize(encoding = 'utf-8') ⇒ JsonBase
Create an XML markup builder.
- out
-
Object receiving the markup.
out
must respond to<<
. - indent
-
Number of spaces used for indentation (0 implies no indentation and no line breaks).
- initial
-
Level of initial indentation.
- encoding
-
When
encoding
and $KCODE are set to ‘utf-8’ characters aren’t converted to character entities in the output stream.
22 23 24 |
# File 'lib/json_builder/jsonbase.rb', line 22 def initialize(encoding='utf-8') @encoding = encoding.downcase end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Create XML markup based on the name of the method. This method is never invoked directly, but is called for each markup method in the markup block.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/json_builder/jsonbase.rb', line 36 def method_missing(sym, *args, &block) text = nil sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol) @target = '{' if @target.length == 0 @target.chop! if @target.last == '}' args.each do |arg| case arg when Hash attrs ||= {} attrs.merge!(arg) else text = arg end end if block raise ArgumentError, "JsonMarkup cannot mix a text argument with a block" unless text.nil? _start_tag(sym) block.call(self) _end_tag elsif text.nil? _start_tag(sym) else _start_attr(sym) text! text _end_tag end @target.gsub(/,(\}+)$/, '\1') end |
Instance Method Details
#<<(text) ⇒ Object
Append text to the output target without escaping any markup. May be used within the markup brackets as:
builder.p { |x| x << "<br/>HI" } #=> <p><br/>HI</p>
This is useful when using non-builder enabled software that generates strings. Just insert the string directly into the builder without changing the inserted markup.
It is also useful for stacking builder objects. Builders only use <<
to append to the target, so by supporting this method/operation builders can use other builders as their targets.
100 101 102 |
# File 'lib/json_builder/jsonbase.rb', line 100 def <<(text) _text(text) end |
#nil? ⇒ Boolean
For some reason, nil? is sent to the JsonMarkup object. If nil? is not defined and method_missing is invoked, some strange kind of recursion happens. Since nil? won’t ever be an XML tag, it is pretty safe to define it here. (Note: this is an example of cargo cult programming, cf. fishbowl.pastiche.org/2004/10/13/cargo_cult_programming).
110 111 112 |
# File 'lib/json_builder/jsonbase.rb', line 110 def nil? false end |
#tag!(sym, *args, &block) ⇒ Object
Create a tag named sym
. Other than the first argument which is the tag name, the arguments are the same as the tags implemented via method_missing
.
29 30 31 |
# File 'lib/json_builder/jsonbase.rb', line 29 def tag!(sym, *args, &block) method_missing(sym.to_sym, args, &block) end |
#text!(text) ⇒ Object
Append text to the output target. Escape any markup. May be used within the markup brackets as:
builder.p { |b| b.br; b.text! "HI" } #=> <p><br/>HI</p>
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/json_builder/jsonbase.rb', line 74 def text!(text) value = case text when TrueClass then 'true' when FalseClass then 'false' when NilClass then 'undefined' when Float then text.to_s when Fixnum then text.to_s else "\"#{_escape(text)}\"" end _text(value) end |