Class: EOTS::EmailKind

Inherits:
Object
  • Object
show all
Defined in:
app/models/eots/email_kind.rb

Constant Summary collapse

AlreadyDefinedError =
Class.new(RuntimeError)
FieldNotFoundError =
Class.new(RuntimeError)
NotFoundError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent, options = {}) ⇒ EmailKind

Returns a new instance of EmailKind.



18
19
20
21
22
23
24
25
26
27
# File 'app/models/eots/email_kind.rb', line 18

def initialize(name, parent, options={})
  @name = name.to_s
  opts = options.dup
  @fields = {}
  (EOTS::STANDARD_FIELDS + EOTS::NON_FIELD_ITEMS).each do |field|
    instance_variable_set("@#{field}".to_sym, opts.delete(field))
  end
  @options = opts  # what's left
  @parent = parent
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



7
8
9
# File 'app/models/eots/email_kind.rb', line 7

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'app/models/eots/email_kind.rb', line 7

def name
  @name
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'app/models/eots/email_kind.rb', line 7

def options
  @options
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'app/models/eots/email_kind.rb', line 7

def parent
  @parent
end

Instance Method Details

#add_field(field_name, label, field_options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/models/eots/email_kind.rb', line 29

def add_field(field_name, label, field_options)
  field_name = field_name.to_s
  if @fields[field_name]
    raise(EOTS::Field::AlreadyDefinedError,
          "Field #{field_name} is already defined on email kind #{self.name}")
  end
  @form_fields = nil # invalidate it... can't reach down to children tho :-(
  # also return the new field, in case caller wants to do something with it
  @fields[field_name] = EOTS::Field.new(field_name, label, field_options)
end

#body_fieldsObject



40
41
42
# File 'app/models/eots/email_kind.rb', line 40

def body_fields
  form_fields[:body]
end


44
45
46
# File 'app/models/eots/email_kind.rb', line 44

def footer_fields
  form_fields[:footer]
end

#footersObject



48
49
50
51
# File 'app/models/eots/email_kind.rb', line 48

def footers
  below = @parent ? @parent.footers : []
  ([footer] + below).compact
end

#form_fieldsObject



53
54
55
56
57
58
59
60
61
62
# File 'app/models/eots/email_kind.rb', line 53

def form_fields
  unless @form_fields
    @form_fields = @parent ? @parent.form_fields : { header: [], body: [], footer: [] }
    mine = fields.values.group_by &:section
    @form_fields[:header] += mine[:header] if mine[:header]
    @form_fields[:body]   += mine[:body]   if mine[:body]
    @form_fields[:footer]  = mine[:footer] + @form_fields[:footer] if mine[:footer]
  end
  @form_fields.dup  # dup so children don't add stuff
end

#header_fieldsObject



64
65
66
# File 'app/models/eots/email_kind.rb', line 64

def header_fields
  form_fields[:header]
end

#headersObject



68
69
70
71
# File 'app/models/eots/email_kind.rb', line 68

def headers
  above = @parent ? @parent.headers : []
  (above << header).compact
end

#html_footersObject



73
74
75
# File 'app/models/eots/email_kind.rb', line 73

def html_footers
  wrap_in_html_paragraphs(footers)
end

#html_headersObject



77
78
79
# File 'app/models/eots/email_kind.rb', line 77

def html_headers
  wrap_in_html_paragraphs(headers)
end