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

#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, options = {})
  if @scope.nil?
    @scope = :array
    @stream.push_array
  else
    raise InvalidScopeForArrayError.new('array! expects exclusive use of this block')
  end

  handle_collection(collection, options) do |item, index|
    yield item, index
  end

  @scope = :array

  nil
end

#format_key(key) ⇒ Object



31
32
33
# File 'lib/props_template/base.rb', line 31

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, 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



64
65
66
67
68
# File 'lib/props_template/base.rb', line 64

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)
  @stream.push_key(key)
  set_block_content!(options) do
    yield
  end
end

#refine_all_item_options(all_options) ⇒ Object



70
71
72
# File 'lib/props_template/base.rb', line 70

def refine_all_item_options(all_options)
  all_options
end

#refine_item_options(item, options) ⇒ Object



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

def refine_item_options(item, options)
  options
end

#result!Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/props_template/base.rb', line 108

def result!
  if @scope.nil?
    @stream.push_object
    @stream.pop
  else
    @stream.pop
  end

  json = @stream.raw_json
  @scope = nil
  @key_cache = {}
  json
end

#set!(key, value = nil) ⇒ Object



35
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 35

def set!(key, value = nil)
  key = format_key(key)

  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
    @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!(options = {})
  @scope = nil
  yield
  if @scope.nil?
    @stream.push_object
  end
  @stream.pop
end