Class: Cmsimple::Region

Inherits:
Object
  • Object
show all
Defined in:
app/models/cmsimple/region.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region_hash = nil) ⇒ Region

Returns a new instance of Region.



4
5
6
7
8
9
# File 'app/models/cmsimple/region.rb', line 4

def initialize(region_hash=nil)
  @region_hash = (region_hash.presence || {}).dup
  @snippets = []
  @html = value
  build_snippets
end

Instance Attribute Details

#snippetsObject (readonly)

Returns the value of attribute snippets.



3
4
5
# File 'app/models/cmsimple/region.rb', line 3

def snippets
  @snippets
end

Instance Method Details

#as_jsonObject



36
37
38
# File 'app/models/cmsimple/region.rb', line 36

def as_json
  @region_hash
end

#build_snippetsObject



11
12
13
14
# File 'app/models/cmsimple/region.rb', line 11

def build_snippets
  return unless @region_hash.key?(:snippets) && @region_hash[:snippets].is_a?(Hash)
  @snippets = @region_hash[:snippets].collect {|id, hash| Snippet.new(id, hash)}
end

#render_snippetsObject



16
17
18
19
20
21
22
# File 'app/models/cmsimple/region.rb', line 16

def render_snippets
  @html = value
  snippets.each do |snippet|
    snippet_view = yield snippet
    @html.gsub!(snippet.matcher, snippet_view)
  end
end

#to_sObject



32
33
34
# File 'app/models/cmsimple/region.rb', line 32

def to_s
  @html.presence || ""
end

#valueObject



24
25
26
27
28
29
30
# File 'app/models/cmsimple/region.rb', line 24

def value
  begin
    @region_hash[:value]
  rescue => ex
    raise RegionValueError, "Unable to access invalid region hash: #{@region_hash.inspect}"
  end
end