Class: EOTS::Email

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

Overview

an instance, as opposed to the whole kind

Constant Summary collapse

MissingRequiredFieldsError =
Class.new(RuntimeError)
MismatchedFieldError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, values = {}) ⇒ Email

Returns a new instance of Email.



12
13
14
15
# File 'app/models/eots/email.rb', line 12

def initialize(kind, values={})
  @kind = kind
  @values = values.dup
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



6
7
8
# File 'app/models/eots/email.rb', line 6

def kind
  @kind
end

#valuesObject (readonly)

Returns the value of attribute values.



6
7
8
# File 'app/models/eots/email.rb', line 6

def values
  @values
end

Class Method Details

.create_from_params(params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/eots/email.rb', line 17

def self.create_from_params(params)
  kind_name = params[:kind]
  kind = EOTS::find_kind(kind_name)
  unless kind
    raise(EOTS::EmailKind::NotFoundError,
          "Unknown email kind '#{kind_name}': #{list}")
  end
  # TEMPORARY KLUGE, UNTIL I MAKE THE VALUES A SUB-HASH:
  values = params.dup
  %w(action controller authenticity_token commit kind utf8).each { |k| values.delete k }
  check_value_names(values, kind) if values.any?
  check_required_fields(values, kind)
  check_required_matches(values, kind)
  self.new(kind, values)
end

Instance Method Details

#bodyObject



33
34
35
36
37
38
39
40
41
# File 'app/models/eots/email.rb', line 33

def body
  parts = [kind.name.titleize]
  kind.form_fields.values.flatten.each do |field|
    parts << "> #{"* " if field.required?}#{field.label}"
    reply = values[field.name]
    parts << (reply.present? ? reply : "(not answered)")
  end
  parts.join("\n\n")
end

#fromObject



43
44
45
# File 'app/models/eots/email.rb', line 43

def from
  kind.from || "\"#{values[:name]}\" <#{values[:email]}>"
end