class JSONFactory::JSONBuilder
Constants
- BUILDER_VARIABLE_NAME
- TOKEN_COLON
- TOKEN_COMMA
- TOKEN_LEFT_CURLY_BRACKET
- TOKEN_LEFT_SQUARE_BRACKET
- TOKEN_RIGHT_CURLY_BRACKET
- TOKEN_RIGHT_SQUARE_BRACKET
Public Class Methods
new(io, type = :value)
click to toggle source
# File lib/json_factory/json_builder.rb, line 14 def initialize(io, type = :value) @stack = [State.new(io, type)] @cache = Cache.instance @template_store = TemplateStore.instance end
Public Instance Methods
array() { || ... }
click to toggle source
# File lib/json_factory/json_builder.rb, line 27 def array raise TypeNotAllowedError, 'Can only add array as a value' unless type == :value raise TypeNotAllowedError, 'Cannot add multiple values' unless count.zero? io << TOKEN_LEFT_SQUARE_BRACKET push_type(:array) { yield } if block_given? io << TOKEN_RIGHT_SQUARE_BRACKET increment_count end
cache(key) { || ... }
click to toggle source
# File lib/json_factory/json_builder.rb, line 73 def cache(key) value = @cache.fetch(key) do cache_io = StringIO.new push_io(cache_io) { yield } cache_io.string end raise EmptyValueError if value.empty? add_separator io << value increment_count end
element(value = nil) { || ... }
click to toggle source
# File lib/json_factory/json_builder.rb, line 37 def element(value = nil) raise TypeNotAllowedError, 'Can only add an element within an array' unless type == :array add_separator if block_given? push_type(:value) { yield } else add_value(value) end increment_count end
evaluate(string, local_variables, filename)
click to toggle source
# File lib/json_factory/json_builder.rb, line 86 def evaluate(string, local_variables, filename) dsl = DSL.new(self) binding = jfactory(dsl) local_variables.each_pair do |key, value| binding.local_variable_set(key, value) end binding.local_variable_set(BUILDER_VARIABLE_NAME, dsl) eval(string, binding, filename.to_s) # rubocop:disable Security/Eval end
member(key, value = nil) { || ... }
click to toggle source
# File lib/json_factory/json_builder.rb, line 59 def member(key, value = nil) raise TypeNotAllowedError, 'Can only add a member within an object' unless type == :object add_separator io << Converter.json_key(key) io << TOKEN_COLON if block_given? push_type(:value) { yield } else add_value(value) end increment_count end
object() { || ... }
click to toggle source
# File lib/json_factory/json_builder.rb, line 49 def object raise TypeNotAllowedError, 'Can only add object as a value' unless type == :value raise TypeNotAllowedError, 'Cannot add multiple values' unless count.zero? io << TOKEN_LEFT_CURLY_BRACKET push_type(:object) { yield } if block_given? io << TOKEN_RIGHT_CURLY_BRACKET increment_count end
render_string(string, local_variables)
click to toggle source
# File lib/json_factory/json_builder.rb, line 102 def render_string(string, local_variables) evaluate(string, local_variables, '(inline)') end
render_template(filename, local_variables)
click to toggle source
# File lib/json_factory/json_builder.rb, line 96 def render_template(filename, local_variables) template = @template_store.get(filename) evaluate(template, local_variables, filename) end
Also aliased as: partial
value(value = nil)
click to toggle source
# File lib/json_factory/json_builder.rb, line 20 def value(value = nil) raise TypeNotAllowedError, 'Can only add value as a value' unless type == :value raise TypeNotAllowedError, 'Cannot add multiple values' unless count.zero? add_value(value) increment_count end
Private Instance Methods
add_separator()
click to toggle source
# File lib/json_factory/json_builder.rb, line 112 def add_separator io << TOKEN_COMMA unless count.zero? end
add_value(value)
click to toggle source
# File lib/json_factory/json_builder.rb, line 108 def add_value(value) io << Converter.json_value(value) end
count()
click to toggle source
# File lib/json_factory/json_builder.rb, line 124 def count @stack.last.count end
increment_count()
click to toggle source
# File lib/json_factory/json_builder.rb, line 128 def increment_count @stack.last.count += 1 end
io()
click to toggle source
# File lib/json_factory/json_builder.rb, line 116 def io @stack.last.io end
push_io(io) { || ... }
click to toggle source
# File lib/json_factory/json_builder.rb, line 132 def push_io(io) @stack.push(State.new(io, type)) yield @stack.pop end
push_type(type) { || ... }
click to toggle source
# File lib/json_factory/json_builder.rb, line 138 def push_type(type) @stack.push(State.new(io, type)) yield raise EmptyValueError if type == :value && count.zero? @stack.pop end
type()
click to toggle source
# File lib/json_factory/json_builder.rb, line 120 def type @stack.last.type end