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.



11
12
13
# File 'lib/crm/helpers/attributes.rb', line 11

def crm_type
  @crm_type
end

Instance Method Details

#crm_attr_accessor(*attributes) ⇒ Object



86
87
88
89
# File 'lib/crm/helpers/attributes.rb', line 86

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

#crm_attr_reader(*attributes) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/crm/helpers/attributes.rb', line 47

def crm_attr_reader(*attributes)
  @crm_attr_readers ||= Set.new

  attributes.each do |attribute|
    @crm_attr_readers << attribute
    next if instance_methods.include?(attribute.to_sym)

    check_attribute(attribute)

    define_method attribute do
      crm_attributes[attribute]
    end
  end
end

#crm_attr_readersObject



62
63
64
# File 'lib/crm/helpers/attributes.rb', line 62

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

#crm_attr_writer(*attributes) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/crm/helpers/attributes.rb', line 66

def crm_attr_writer(*attributes)
  @crm_attr_writers ||= Set.new

  attributes.each do |attribute|
    method_name = "#{attribute}=".to_sym
    @crm_attr_writers << method_name
    next if instance_methods.include?(method_name)

    check_attribute(attribute)

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

#crm_attr_writersObject



82
83
84
# File 'lib/crm/helpers/attributes.rb', line 82

def crm_attr_writers
  @crm_attr_writers.sort || []
end

#crm_attributesObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/crm/helpers/attributes.rb', line 36

def crm_attributes
  return @crm_attributes if @crm_attributes.present?

  if crm_type.blank?
    raise "#{name}.represents_crm_type(type) needs to be called " \
          'to fetch its CRM attributes.'
  end

  collect_crm_attributes_data(crm_type)
end

#crm_classObject



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

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



30
31
32
33
34
# File 'lib/crm/helpers/attributes.rb', line 30

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

#represents_crm_type(crm_type) ⇒ Object



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

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