Class: Scaffolding::ClassNamesTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffolding/class_names_transformer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(child, parent, namespace = "account") ⇒ ClassNamesTransformer

Returns a new instance of ClassNamesTransformer.



4
5
6
7
8
# File 'lib/scaffolding/class_names_transformer.rb', line 4

def initialize(child, parent, namespace = "account")
  self.child = child
  self.parent = parent
  self.namespace = namespace
end

Instance Attribute Details

#childObject

Returns the value of attribute child.



2
3
4
# File 'lib/scaffolding/class_names_transformer.rb', line 2

def child
  @child
end

#namespaceObject

Returns the value of attribute namespace.



2
3
4
# File 'lib/scaffolding/class_names_transformer.rb', line 2

def namespace
  @namespace
end

#parentObject

Returns the value of attribute parent.



2
3
4
# File 'lib/scaffolding/class_names_transformer.rb', line 2

def parent
  @parent
end

Instance Method Details

#all_parts_in_contextObject

“In context” here means all of the parts in a namespace without reference to the parts that overlap.

For example: Returns everything because there is no overlap.

Parent: Team
Child: Projects::Site
Return value: [["Projects", "Site"], ["Team"]]

Returns the namespaces/classes without the parts that overlap.

Parent: Projects::Site
Child:  Projects::Sites::Url
Return value: [["Url"], ["Site"]]


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/scaffolding/class_names_transformer.rb', line 53

def all_parts_in_context
  working_parts = parts
  working_parent_parts = parent_parts

  # e.g. Webhooks::Incoming::Event vs. Webhooks::Incoming::Delivery
  while working_parts.first == working_parent_parts.first
    # get rid of 'Webhooks::' and 'Incoming::'
    working_parts.shift
    working_parent_parts.shift
  end

  # e.g. Conversation vs. Conversations::Subscription
  while working_parts.first == working_parent_parts.first.pluralize
    # get rid of 'Conversations::'
    working_parts.shift
  end

  [working_parts, working_parent_parts]
end

#belongs_to_needs_class_definition?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
# File 'lib/scaffolding/class_names_transformer.rb', line 10

def belongs_to_needs_class_definition?
  return false if parent_namespace_parts.empty?
  return false if parent_namespace_parts == namespace_parts
  namespace_parts.first(parent_namespace_parts.count) != parent_namespace_parts
end

#class_name_in_parent_contextObject



81
82
83
# File 'lib/scaffolding/class_names_transformer.rb', line 81

def class_name_in_parent_context
  parts_in_context.join("::")
end

#in_namespace_class_nameObject



36
37
38
# File 'lib/scaffolding/class_names_transformer.rb', line 36

def in_namespace_class_name
  parts.last
end

#namespace_partsObject



16
17
18
# File 'lib/scaffolding/class_names_transformer.rb', line 16

def namespace_parts
  parts[0..-2]
end

#parent_class_name_in_contextObject



85
86
87
# File 'lib/scaffolding/class_names_transformer.rb', line 85

def parent_class_name_in_context
  parent_parts_in_context.join("::")
end

#parent_in_namespace_class_nameObject



32
33
34
# File 'lib/scaffolding/class_names_transformer.rb', line 32

def parent_in_namespace_class_name
  parent_parts.last
end

#parent_namespace_partsObject



20
21
22
# File 'lib/scaffolding/class_names_transformer.rb', line 20

def parent_namespace_parts
  parent_parts[0..-2]
end

#parent_partsObject



28
29
30
# File 'lib/scaffolding/class_names_transformer.rb', line 28

def parent_parts
  parent.empty? ? [""] : parent.split("::")
end

#parent_parts_in_contextObject



77
78
79
# File 'lib/scaffolding/class_names_transformer.rb', line 77

def parent_parts_in_context
  all_parts_in_context.last
end

#parent_table_nameObject



93
94
95
# File 'lib/scaffolding/class_names_transformer.rb', line 93

def parent_table_name
  parent.underscore.tr("/", "_").pluralize
end

#parent_variable_name_in_contextObject



89
90
91
# File 'lib/scaffolding/class_names_transformer.rb', line 89

def parent_variable_name_in_context
  parent_parts_in_context.join("::").underscore.tr("/", "_")
end

#partsObject



24
25
26
# File 'lib/scaffolding/class_names_transformer.rb', line 24

def parts
  child.split("::")
end

#parts_in_contextObject



73
74
75
# File 'lib/scaffolding/class_names_transformer.rb', line 73

def parts_in_context
  all_parts_in_context.first
end

#replacement_for(string) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/scaffolding/class_names_transformer.rb', line 101

def replacement_for(string)
  case string

  when "Scaffolding::AbsolutelyAbstract::CreativeConcepts"
    parent.pluralize
  when "Scaffolding::CompletelyConcrete::TangibleThings"
    child.pluralize
  when "ScaffoldingAbsolutelyAbstractCreativeConcepts"
    parent.pluralize.gsub("::", "")
  when "ScaffoldingCompletelyConcreteTangibleThings"
    child.pluralize.gsub("::", "")
  when "Scaffolding Absolutely Abstract Creative Concepts"
    parent.pluralize.titlecase.tr("/", " ")
  when "Scaffolding Completely Concrete Tangible Things"
    child.pluralize.titlecase.tr("/", " ")

  # This is a specific example for locales where we only leave tangible_things.
  when "scaffolding.completely_concrete.tangible_things"
    child.underscore.pluralize

  when "Scaffolding/Absolutely Abstract/Creative Concepts"
    parent.pluralize.titlecase
  when "Scaffolding/Completely Concrete/Tangible Things"
    child.pluralize.titlecase
  when "scaffolding/absolutely_abstract/creative_concepts"
    parent.underscore.pluralize
  when "scaffolding/completely_concrete/tangible_things"
    child.underscore.pluralize
  when "scaffolding/completely_concrete/_tangible_things"
    parts = child.underscore.split("/")
    parts.push "_#{parts.pop}"
    parts.join("/").pluralize
  when "scaffolding_absolutely_abstract_creative_concepts"
    parent.underscore.tr("/", "_").pluralize
  when "scaffolding_completely_concrete_tangible_things"
    child.underscore.tr("/", "_").pluralize
  when "scaffolding-absolutely-abstract-creative-concepts"
    parent.underscore.gsub(/[\/_]/, "-").pluralize
  when "scaffolding-completely-concrete-tangible-things"
    child.underscore.gsub(/[\/_]/, "-").pluralize

  when "Scaffolding::AbsolutelyAbstract::CreativeConcept"
    parent
  when "Scaffolding::CompletelyConcrete::TangibleThing"
    child
  when "ScaffoldingAbsolutelyAbstractCreativeConcept"
    parent.gsub("::", "")
  when "ScaffoldingCompletelyConcreteTangibleThing"
    child.gsub("::", "")
  when "Scaffolding Absolutely Abstract Creative Concept"
    parent.titlecase.tr("/", " ")
  when "Scaffolding Completely Concrete Tangible Thing"
    child.titlecase.tr("/", " ")
  when "Scaffolding/Absolutely Abstract/Creative Concept"
    parent.titlecase
  when "Scaffolding/Completely Concrete/Tangible Thing"
    child.titlecase
  when "scaffolding/absolutely_abstract/creative_concept"
    parent.underscore
  when "scaffolding/completely_concrete/tangible_thing"
    child.underscore
  when "scaffolding_absolutely_abstract_creative_concept"
    parent.underscore.tr("/", "_")
  when "scaffolding_completely_concrete_tangible_thing"
    child.underscore.tr("/", "_")
  when "scaffolding-absolutely-abstract-creative-concept"
    parent.underscore.gsub(/[\/_]/, "-")
  when "scaffolding-completely-concrete-tangible-thing"
    child.underscore.gsub(/[\/_]/, "-")

  when "absolutely_abstract_creative_concepts"
    parent_class_name_in_context.underscore.tr("/", "_").pluralize
  when "completely_concrete_tangible_things"
    class_name_in_parent_context.underscore.tr("/", "_").pluralize
  when "absolutely_abstract/creative_concepts"
    parent_class_name_in_context.underscore.pluralize
  when "completely_concrete/tangible_things"
    class_name_in_parent_context.underscore.pluralize
  when "absolutely-abstract-creative-concepts"
    parent.underscore.gsub(/[\/_]/, "-").pluralize
  when "completely-concrete-tangible-things"
    child.underscore.gsub(/[\/_]/, "-").pluralize

  when "absolutely_abstract_creative_concept"
    parent_class_name_in_context.underscore.tr("/", "_")
  when "completely_concrete_tangible_thing"
    class_name_in_parent_context.underscore.tr("/", "_")
  when "absolutely_abstract/creative_concept"
    parent_class_name_in_context.underscore
  when "completely_concrete/tangible_thing"
    class_name_in_parent_context.underscore
  when "absolutely-abstract-creative-concept"
    parent.underscore.gsub(/[\/_]/, "-")
  when "completely-concrete-tangible-thing"
    child.underscore.gsub(/[\/_]/, "-")

  when "creative_concepts"
    parent_in_namespace_class_name.underscore.pluralize
  when "tangible_things"
    in_namespace_class_name.underscore.pluralize
  when "Creative Concepts"
    parent_in_namespace_class_name.titlecase.pluralize
  when "Tangible Things"
    in_namespace_class_name.titlecase.pluralize
  when "Creative concepts"
    parent_in_namespace_class_name.titlecase.downcase.capitalize.pluralize
  when "Tangible things"
    in_namespace_class_name.titlecase.downcase.capitalize.pluralize
  when "creative concepts"
    parent_in_namespace_class_name.titlecase.downcase.pluralize
  when "tangible things"
    in_namespace_class_name.titlecase.downcase.pluralize
  when "creative-concepts"
    parent_in_namespace_class_name.underscore.gsub(/[\/_]/, "-").pluralize
  when "tangible-things"
    in_namespace_class_name.underscore.gsub(/[\/_]/, "-").pluralize

  when "creative_concept"
    parent_in_namespace_class_name.underscore
  when "tangible_thing"
    in_namespace_class_name.underscore
  when "Creative Concept"
    parent_in_namespace_class_name.titlecase
  when "Tangible Thing"
    in_namespace_class_name.titlecase
  when "Creative concept"
    parent_in_namespace_class_name.titlecase.downcase.capitalize
  when "Tangible thing"
    in_namespace_class_name.titlecase.downcase.capitalize
  when "creative concept"
    parent_in_namespace_class_name.titlecase.downcase
  when "tangible thing"
    in_namespace_class_name.titlecase.downcase
  when "creative-concept"
    parent_in_namespace_class_name.underscore.gsub(/[\/_]/, "-")
  when "tangible-thing"
    in_namespace_class_name.underscore.gsub(/[\/_]/, "-")

  when ":account"
    ":#{namespace}"
  when "/account/"
    "/#{namespace}/"

  else
    "🛑"
  end
end

#table_nameObject



97
98
99
# File 'lib/scaffolding/class_names_transformer.rb', line 97

def table_name
  child.underscore.tr("/", "_").pluralize
end