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.
- #commands_to_json!(commands) ⇒ Object
- #handle_collection(collection, options) ⇒ Object
- #handle_collection_item(collection, item, index, options) ⇒ Object
- #handle_set_block(key, options) ⇒ Object
-
#initialize ⇒ 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 ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 |
# File 'lib/props_template/base.rb', line 11 def initialize @commands = [[:push_object]] @stream = Oj::StringWriter.new(mode: :rails) @scope = nil end |
Instance Method Details
#array!(collection, options = {}) ⇒ Object
todo, add ability to define contents of array
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/props_template/base.rb', line 80 def array!(collection, = {}) if @scope.nil? @scope = :array else raise InvalidScopeForArrayError.new('array! expects exclusive use of this block') end @commands[-1] = [:push_array] if !collection.empty? handle_collection(collection, ) do |item, index| yield item, index end end @scope = :array nil end |
#commands_to_json!(commands) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/props_template/base.rb', line 99 def commands_to_json!(commands) commands.each do |command| @stream.send(*command) end json = @stream.to_s @stream.reset json end |
#handle_collection(collection, options) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/props_template/base.rb', line 64 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
54 55 56 57 58 |
# File 'lib/props_template/base.rb', line 54 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 |
# File 'lib/props_template/base.rb', line 24 def handle_set_block(key, ) @commands.push([:push_key, key.to_s]) set_block_content!() do yield end end |
#refine_all_item_options(all_options) ⇒ Object
60 61 62 |
# File 'lib/props_template/base.rb', line 60 def () end |
#refine_item_options(item, options) ⇒ Object
50 51 52 |
# File 'lib/props_template/base.rb', line 50 def (item, ) end |
#result! ⇒ Object
108 109 110 111 |
# File 'lib/props_template/base.rb', line 108 def result! @commands.push([:pop]) commands_to_json!(@commands) end |
#set!(key, value = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/props_template/base.rb', line 31 def set!(key, value = nil) @scope ||= :object if @scope == :array raise InvalidScopeForObjError.new('Attempted to set! on an array! scope') end if block_given? handle_set_block(key, value) do yield end else @commands.push([:push_key, key.to_s]) @commands.push([:push_value, value]) end @scope = :object nil end |
#set_block_content!(options = {}) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/props_template/base.rb', line 17 def set_block_content!( = {}) @commands.push([:push_object]) @scope = nil yield @commands.push([:pop]) end |