Class: Props::Base
- Inherits:
-
Object
- Object
- Props::Base
- Defined in:
- lib/props_template/base.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#array!(collection, options = {}) ⇒ Object
todo, add ability to define contents of array.
- #format_key(key) ⇒ Object
- #handle_collection(collection, options) ⇒ Object
- #handle_collection_item(collection, item, index, options) ⇒ Object
- #handle_set_block(key, options) ⇒ Object
-
#initialize(encoder = nil) ⇒ Base
constructor
A new instance of Base.
- #refine_all_item_options(all_options) ⇒ Object
- #refine_item_options(item, options) ⇒ Object
- #result! ⇒ Object
- #set!(key, value = nil) ⇒ Object
- #set_block_content!(options = {}) ⇒ Object
Constructor Details
#initialize(encoder = nil) ⇒ Base
Returns a new instance of Base.
10 11 12 13 |
# File 'lib/props_template/base.rb', line 10 def initialize(encoder = nil) @stream = Oj::StringWriter.new(mode: :rails) @scope = nil end |
Instance Method Details
#array!(collection, options = {}) ⇒ Object
todo, add ability to define contents of array
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/props_template/base.rb', line 91 def array!(collection, = {}) if @scope.nil? @scope = :array @stream.push_array else raise InvalidScopeForArrayError.new("array! expects exclusive use of this block") end handle_collection(collection, ) do |item, index| yield item, index end @scope = :array nil end |
#format_key(key) ⇒ Object
32 33 34 |
# File 'lib/props_template/base.rb', line 32 def format_key(key) key.to_s end |
#handle_collection(collection, options) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/props_template/base.rb', line 74 def handle_collection(collection, ) all_opts = collection.map do |item| (item, .clone) end all_opts = (all_opts) collection.each_with_index do |item, index| pass_opts = all_opts[index] handle_collection_item(collection, item, index, pass_opts) do # todo: remove index? yield item, index end end end |
#handle_collection_item(collection, item, index, options) ⇒ Object
64 65 66 67 68 |
# File 'lib/props_template/base.rb', line 64 def handle_collection_item(collection, item, index, ) set_block_content!() do yield end end |
#handle_set_block(key, options) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/props_template/base.rb', line 24 def handle_set_block(key, ) key = format_key(key) @stream.push_key(key) set_block_content!() do yield end end |
#refine_all_item_options(all_options) ⇒ Object
70 71 72 |
# File 'lib/props_template/base.rb', line 70 def () end |
#refine_item_options(item, options) ⇒ Object
60 61 62 |
# File 'lib/props_template/base.rb', line 60 def (item, ) end |
#result! ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/props_template/base.rb', line 108 def result! if @scope.nil? @stream.push_object end @stream.pop json = @stream.raw_json @stream.reset @scope = nil json end |
#set!(key, value = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/props_template/base.rb', line 36 def set!(key, value = nil) if @scope == :array raise InvalidScopeForObjError.new("Attempted to set! on an array! scope") end if @scope.nil? @scope = :object @stream.push_object end if block_given? handle_set_block(key, value) do yield end else key = format_key(key) @stream.push_value(value, key) end @scope = :object nil end |
#set_block_content!(options = {}) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/props_template/base.rb', line 15 def set_block_content!( = {}) @scope = nil yield if @scope.nil? @stream.push_object end @stream.pop end |