Class: JSONFactory::JSONBuilder
- Inherits:
-
Object
- Object
- JSONFactory::JSONBuilder
- Defined in:
- lib/json_factory/json_builder.rb
Constant Summary collapse
- BUILDER_VARIABLE_NAME =
:json
- TOKEN_LEFT_SQUARE_BRACKET =
'['
- TOKEN_RIGHT_SQUARE_BRACKET =
']'
- TOKEN_LEFT_CURLY_BRACKET =
'{'
- TOKEN_RIGHT_CURLY_BRACKET =
'}'
- TOKEN_COLON =
':'
- TOKEN_COMMA =
','
Instance Method Summary collapse
- #array ⇒ Object
- #cache(key) ⇒ Object
- #element(value = nil) ⇒ Object
- #evaluate(string, local_variables, filename) ⇒ Object
-
#initialize(io, type = :value) ⇒ JSONBuilder
constructor
A new instance of JSONBuilder.
- #member(key, value = nil) ⇒ Object
- #object ⇒ Object
- #render_string(string, local_variables) ⇒ Object
- #render_template(filename, local_variables) ⇒ Object (also: #partial)
- #value(value = nil) ⇒ Object
Constructor Details
#initialize(io, type = :value) ⇒ JSONBuilder
Returns a new instance of JSONBuilder.
14 15 16 17 18 |
# 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 |
Instance Method Details
#array ⇒ Object
27 28 29 30 31 32 33 34 35 |
# 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) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# 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) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# 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) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# 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) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 ⇒ Object
49 50 51 52 53 54 55 56 57 |
# 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) ⇒ Object
102 103 104 |
# 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) ⇒ Object Also known as: partial
96 97 98 99 |
# 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 |
#value(value = nil) ⇒ Object
20 21 22 23 24 25 |
# 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 |