Class: Props::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/props_template/base.rb

Direct Known Subclasses

BaseWithExtensions

Instance Method Summary collapse

Constructor Details

#initializeBase

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, options = {})
  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, options) 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, options)
  all_opts = collection.map do |item|
    refine_item_options(item, options.clone)
  end

  all_opts = refine_all_item_options(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, options)
  set_block_content!(options) 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, options)
  @commands.push([:push_key, key.to_s])
  set_block_content!(options) do
    yield
  end
end

#refine_all_item_options(all_options) ⇒ Object



60
61
62
# File 'lib/props_template/base.rb', line 60

def refine_all_item_options(all_options)
  all_options
end

#refine_item_options(item, options) ⇒ Object



50
51
52
# File 'lib/props_template/base.rb', line 50

def refine_item_options(item, options)
  options
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!(options = {})
  @commands.push([:push_object])
  @scope = nil
  yield
  @commands.push([:pop])
end