Module: MotionMigrate::MotionGenerate::Relationship::ClassMethods

Defined in:
lib/motion_migrate/motion_generate/relationship.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}) ⇒ Object



9
10
11
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 9

def belongs_to(name, options={})
  relationship(name, :belongs_to, options)
end

#core_data_relationship_attributes(type, options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 121

def core_data_relationship_attributes(type, options)
  attributes = {}

  options.each do |key, value|
    case key
    when :required
      attributes[:optional] = core_data_boolean(value != true)
    when :inverse_of
      attributes[:inverseName] = value
    when :class_name
      attributes[:inverseEntity] = value
      attributes[:destinationEntity] = value
    when :deletion_rule
      attributes[:deletionRule] = core_data_string(value)
    when :transient
      attributes[:transient] = core_data_boolean(value)
    when :spotlight
      attributes[:spotlightIndexingEnabled] = core_data_boolean(value)
    when :truth_file
      attributes[:storedInTruthFile] = core_data_boolean(value)
    when :min
      attributes[:minCount] = value
    when :max
      attributes[:maxCount] = value
    end
  end
  attributes[:toMany] = core_data_boolean(type == :has_many)
  attributes
end

#deletion_rule_allowed?(options) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 79

def deletion_rule_allowed?(options)
  allowed_deletion_rules = [
    :nullify,
    :cascade,
    :deny
  ]
  !options[:deletion_rule] || allowed_deletion_rules.include?(options[:deletion_rule])
end

#has_many(name, options = {}) ⇒ Object



13
14
15
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 13

def has_many(name, options={})
  relationship(name, :has_many, options)
end

#raise_if_deletion_rule_not_allowed(options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 88

def raise_if_deletion_rule_not_allowed(options)
  unless deletion_rule_allowed?(options)
    raise <<-ERROR
! One of these deletion rules are allowed:
!   :nullify 
!   :cascade
!   :deny
    ERROR
  end
end

#raise_if_relationship_option_not_allowed(type, option) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 40

def raise_if_relationship_option_not_allowed(type, option)
  unless relationship_option_allowed?(type, option)
    raise <<-ERROR
! The option must be one of the following: 
!                                          
!   For type :belongs_to:                  
!     :required                          
!     :deletion_rule                     
!     :class_name                        
!     :inverse_of                        
!     :spotlight                      
!     :truth_file                    
!     :transient                    
!                                  
!   For type :has_many:           
!     :required                  
!     :min                      
!     :max                     
!     :deletion_rule          
!     :class_name            
!     :inverse_of           
!     :ordered            
!     :spotlight         
!     :truth_file       
!     :transient       
    ERROR
  end
end

#raise_if_relationship_options_missing(options) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 69

def raise_if_relationship_options_missing(options)
  unless relationship_options_complete?(options)
    raise <<-ERROR
! One of these options are required: 
!   :class_name
!   :inverse_of
    ERROR
  end
end

#relationship(name, type, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 17

def relationship(name, type, options={})
  options.each { |key, value| raise_if_relationship_option_not_allowed(type, key) }

  raise_if_relationship_options_missing(options)
  raise_if_deletion_rule_not_allowed(options)

  attributes = {
    name: name,
    optional: core_data_boolean(true),
    deletionRule: core_data_string(:no_action),
    syncable: core_data_boolean(true)
  }
  attributes.merge!({ minCount: 1, maxCount: 1 }) if type == :belongs_to
  attributes.merge!(core_data_relationship_attributes(type, options))
  relationships[self.entity_name] = {} if relationships[self.entity_name].nil?
  relationships[self.entity_name][name] = attributes
  attributes
end

#relationship_option_allowed?(type, option) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 104

def relationship_option_allowed?(type, option)
  allowed_options = {
    has_many: [:ordered, :min, :max],
  }[type] || []

  allowed_options += [
    :required,
    :spotlight,
    :truth_file,
    :transient,
    :inverse_of,
    :class_name,
    :deletion_rule
  ]
  allowed_options.include?(option)
end

#relationship_options_complete?(options) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 99

def relationship_options_complete?(options)
  required_options = [:class_name, :inverse_of]
  (required_options.uniq - options.keys.uniq).empty?
end

#relationshipsObject



36
37
38
# File 'lib/motion_migrate/motion_generate/relationship.rb', line 36

def relationships
  @@relationships ||= {}
end