Class: Cmsimple::Snippet

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
app/models/cmsimple/snippet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, snippet_hash) ⇒ Snippet

Returns a new instance of Snippet.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/cmsimple/snippet.rb', line 6

def initialize(id, snippet_hash)
  @id = id

  @snippet_hash = snippet_hash.dup
  # @snippet_hash.symbolize_keys!

  @name = @snippet_hash[:name]

  # the options prefix is to handle legacy snippets mercury
  # changed how it serializes snippets
  @options = flatten_options(@snippet_hash[:options].presence || @snippet_hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



29
30
31
# File 'app/models/cmsimple/snippet.rb', line 29

def method_missing method, *args, &block
  @options[method]
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'app/models/cmsimple/snippet.rb', line 4

def id
  @id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'app/models/cmsimple/snippet.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'app/models/cmsimple/snippet.rb', line 4

def options
  @options
end

Instance Method Details

#errorsObject



56
57
58
# File 'app/models/cmsimple/snippet.rb', line 56

def errors
  ActiveModel::Errors.new(self)
end

#flatten_options(options) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'app/models/cmsimple/snippet.rb', line 19

def flatten_options(options)
  return {} unless options.present?
  options = options.dup
  if options.key?(:snippet)
    options.merge(options[:snippet])
  else
    options
  end
end

#matcherObject



33
34
35
# File 'app/models/cmsimple/snippet.rb', line 33

def matcher
  /\[#{self.id}(\/\d)?\]/
end

#persisted?Boolean

ActiveModel API

Returns:

  • (Boolean)


40
41
42
# File 'app/models/cmsimple/snippet.rb', line 40

def persisted?
  false
end

#to_keyObject



48
49
50
# File 'app/models/cmsimple/snippet.rb', line 48

def to_key
  nil
end

#to_paramObject



44
45
46
# File 'app/models/cmsimple/snippet.rb', line 44

def to_param
  nil
end

#to_partial_pathObject



52
53
54
# File 'app/models/cmsimple/snippet.rb', line 52

def to_partial_path
  "cmsimple/snippets/#{name}/display"
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/cmsimple/snippet.rb', line 60

def valid?
  true
end