Module: Crm::Helpers::Attributes::ClassMethods

Defined in:
lib/crm/helpers/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#crm_typeObject (readonly)

Returns the value of attribute crm_type.



9
10
11
# File 'lib/crm/helpers/attributes.rb', line 9

def crm_type
  @crm_type
end

Instance Method Details

#crm_attr_accessor(*attributes) ⇒ Object



76
77
78
79
# File 'lib/crm/helpers/attributes.rb', line 76

def crm_attr_accessor(*attributes)
  crm_attr_reader(*attributes)
  crm_attr_writer(*attributes)
end

#crm_attr_reader(*attributes) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/crm/helpers/attributes.rb', line 39

def crm_attr_reader(*attributes)
  @crm_attr_readers ||= []

  attributes.each do |attribute|
    @crm_attr_readers << attribute unless attribute.in?(@crm_attr_readers)
    next if instance_methods.include?(attribute.to_sym)
    raise "Attribute '#{attribute}' does not exist for a CRM #{crm_type}." if crm_attributes[attribute].blank?

    define_method attribute do
      crm_attributes[attribute]
    end
  end
end

#crm_attr_readersObject



53
54
55
# File 'lib/crm/helpers/attributes.rb', line 53

def crm_attr_readers
  @crm_attr_readers.sort ||= []
end

#crm_attr_writer(*attributes) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/crm/helpers/attributes.rb', line 57

def crm_attr_writer(*attributes)
  @crm_attr_writers ||= []

  attributes.each do |attribute|
    method_name = "#{attribute}=".to_sym
    @crm_attr_writers << method_name unless method_name.in?(@crm_attr_writers)
    next if instance_methods.include?(method_name)
    raise "Attribute '#{attribute}' does not exist for a CRM #{crm_type}." if crm_attributes[attribute].blank?

    define_method method_name do |value|
      crm_attributes[attribute] = value
    end
  end
end

#crm_attr_writersObject



72
73
74
# File 'lib/crm/helpers/attributes.rb', line 72

def crm_attr_writers
  @crm_attr_writers.sort || []
end

#crm_attributesObject



32
33
34
35
36
37
# File 'lib/crm/helpers/attributes.rb', line 32

def crm_attributes
  return @crm_attributes if @crm_attributes.present?
  raise "#{name}.represents_crm_type(type) needs to be called to fetch its CRM attributes." if crm_type.blank?

  collect_crm_attributes_data(crm_type)
end

#crm_classObject



19
20
21
22
23
24
25
26
# File 'lib/crm/helpers/attributes.rb', line 19

def crm_class
  return nil if @crm_type.blank?
  return @crm_class if @crm_class.present?

  type_definition = crm_type_definition(crm_type)
  class_name = "Crm::#{type_definition.item_base_type}"
  @crm_class = class_name.constantize
end

#mandatory_crm_attributesObject



28
29
30
# File 'lib/crm/helpers/attributes.rb', line 28

def mandatory_crm_attributes
  crm_attributes.select { |_, definition| definition[:mandatory] }.keys.sort.map(&:to_sym)
end

#represents_crm_type(crm_type) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/crm/helpers/attributes.rb', line 11

def represents_crm_type(crm_type)
  @crm_type = crm_type
  @crm_class = nil
  @crm_attributes = {}.with_indifferent_access

  crm_attr_accessor(*mandatory_crm_attributes)
end