Module: Signable::Concerns::Model

Extended by:
ActiveSupport::Concern
Includes:
Column, Embed
Included in:
Base, Document, MergeField, Party
Defined in:
lib/signable/concerns/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Column

#find_column, #required_column

Methods included from Embed

#find_embed

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/signable/concerns/model.rb', line 40

def method_missing(method, *args, &block)
  get_method = method.to_s.gsub('=', '')
  object = find_column(get_method) || find_embed(get_method)
  if object
    if get_method.to_sym == method
      @attributes[object.name]
    else
      @attributes[object.name] = args.first
    end
  else
    super
  end
end

Instance Method Details

#attributes=(attributes = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/signable/concerns/model.rb', line 13

def attributes=(attributes = {})
  attributes.each do |key, value|
    if column = find_column(key)
      @attributes[column.name] = value
    elsif embed = find_embed(key)
      values = value.map { |hash| embed.embed_class.new(hash) }
      @attributes[embed.name] = values
    end
  end
end

#form_dataObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/signable/concerns/model.rb', line 24

def form_data
  hash = HashWithIndifferentAccess.new

  @attributes.each do |key, value|
    next if key == :id

    if (column = self.find_column(key))
      hash[column.name_with_prefix] = value
    elsif (embed = self.find_embed(key))
      hash[embed.name_with_prefix] = value.map(&:form_data)
    end
  end

  hash
end

#initialize(attributes = {}) ⇒ Object



8
9
10
11
# File 'lib/signable/concerns/model.rb', line 8

def initialize(attributes = {})
  @attributes     = HashWithIndifferentAccess.new
  self.attributes = attributes
end

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/signable/concerns/model.rb', line 54

def valid?
  required_column.all? do |column|
    @attributes[column.name].present?
  end
end