Module: SimpleAMS::DSL::ClassMethods

Defined in:
lib/simple_ams/dsl.rb

Instance Method Summary collapse

Instance Method Details

#_default_type_nameObject

TODO: Add tests !!



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/simple_ams/dsl.rb', line 76

def _default_type_name
  if to_s.end_with?('::Collection_')
    _name = to_s.gsub(
      'Serializer', ''
    ).gsub(
      '::Collection_', ''
    ).downcase.split('::')[-1]

    "#{_name}_collection".to_sym
  else
    to_s.gsub('Serializer', '').downcase.split('::')[-1].to_sym
  end
end

#adapter(value = nil, options = {}) ⇒ Object



117
118
119
# File 'lib/simple_ams/dsl.rb', line 117

def adapter(value = nil, options = {})
  @_adapter = _value_hash_for(@_adapter, value, options, :adapter)
end

#attributes(*args) ⇒ Object Also known as: attribute, fields



129
130
131
132
133
134
# File 'lib/simple_ams/dsl.rb', line 129

def attributes(*args)
  @_attributes ||= []
  return @_attributes.uniq if args&.empty? || args.nil?

  append_attributes(args)
end

#attributes=(*args) ⇒ Object



138
139
140
141
142
# File 'lib/simple_ams/dsl.rb', line 138

def attributes=(*args)
  @_attributes = []

  attributes(args)
end

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



156
157
158
159
160
# File 'lib/simple_ams/dsl.rb', line 156

def belongs_to(name, options = {}, &block)
  append_relationship(
    [__method__, name, options, embedded_class_for(name, options, block)]
  )
end

#collection(name = nil, &block) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/simple_ams/dsl.rb', line 233

def collection(name = nil, &block)
  if block
    self::Collection_.class_eval do
      instance_exec(&block)
    end
  end

  self::Collection_.type(name) if name

  self::Collection_
end

#default_optionsObject



67
68
69
70
71
72
73
# File 'lib/simple_ams/dsl.rb', line 67

def default_options
  @_default_options ||= {
    adapter: [SimpleAMS::Adapters::AMS, {}],
    primary_id: [:id, {}],
    type: [_default_type_name, {}]
  }
end

#embedded_class_for(name, options, block) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/simple_ams/dsl.rb', line 166

def embedded_class_for(name, options, block)
  embedded = Class.new(self)
  klass_name = "Embedded#{name.to_s.capitalize}Options_"
  const_set(klass_name, embedded)
  embedded.with_options(
    default_options.merge(options.reject { |k| k == :serializer })
  )
  embedded.instance_exec(&block) if block

  embedded
end

#form(name, value, options = {}) ⇒ Object



193
194
195
# File 'lib/simple_ams/dsl.rb', line 193

def form(name, value, options = {})
  append_form([name, value, options])
end

#forms(forms = []) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/simple_ams/dsl.rb', line 217

def forms(forms = [])
  return @_forms ||= [] if forms.empty?

  forms.map { |key, value| append_form([key, value].flatten(1)) } if forms.is_a?(Hash)

  @_forms ||= forms
end

#generic(name, value, options = {}) ⇒ Object



197
198
199
# File 'lib/simple_ams/dsl.rb', line 197

def generic(name, value, options = {})
  append_generic([name, value, options])
end

#generics(generics = []) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/simple_ams/dsl.rb', line 225

def generics(generics = [])
  return @_generics ||= [] if generics.empty?

  generics.map { |key, value| append_generic([key, value].flatten(1)) } if generics.is_a?(Hash)

  @_generics ||= generics
end

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



144
145
146
147
148
# File 'lib/simple_ams/dsl.rb', line 144

def has_many(name, options = {}, &block)
  append_relationship(
    [__method__, name, options, embedded_class_for(name, options, block)]
  )
end

#has_one(name, options = {}, &block) ⇒ Object



150
151
152
153
154
# File 'lib/simple_ams/dsl.rb', line 150

def has_one(name, options = {}, &block)
  append_relationship(
    [__method__, name, options, embedded_class_for(name, options, block)]
  )
end

#includes(*_args) ⇒ Object

TODO: there is no memoization here, hence we ignore includes manually set !! Consider fixing it by employing an observer that will clean the instance var each time @_relations is updated



181
182
183
# File 'lib/simple_ams/dsl.rb', line 181

def includes(*_args)
  relations.map { |rel| rel[1] }
end

#inherited(subclass) ⇒ Object

TODO: Shouldn’t we call here super to presever user’s behavior ?



30
31
32
33
34
35
36
37
38
39
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
# File 'lib/simple_ams/dsl.rb', line 30

def inherited(subclass)
  subclass.with_options(
    options.merge(
      # TODO: maybe add another group of elements under dsl?
      # this could be DSL::Type.new(type).explicit?
      type[-1][:_explicit] ? {} : { type: nil }
    )
  )

  _klass = Class.new(Object).extend(ClassMethods)
  _klass.instance_eval do
    def options
      {
        adapter: adapter,
        primary_id: primary_id,
        type: type,
        fields: fields,
        relations: relations,
        includes: includes,
        links: links,
        metas: metas,
        forms: forms,
        generics: generics
      }
    end
  end

  _klass.with_options(
    self::Collection_.options.merge(
      # TODO: maybe add another group of elements under dsl?
      # this could be DSL::Type.new(type).explicit?
      type[-1][:_explicit] ? {} : { type: nil }
    )
  )
  subclass.const_set('Collection_', _klass)
end


185
186
187
# File 'lib/simple_ams/dsl.rb', line 185

def link(name, value, options = {})
  append_link([name, value, options])
end


201
202
203
204
205
206
207
# File 'lib/simple_ams/dsl.rb', line 201

def links(links = [])
  return @_links ||= [] if links.empty?

  links.map { |key, value| append_link([key, value].flatten(1)) } if links.is_a?(Hash)

  @_links ||= links
end

#meta(name = nil, value = nil, options = {}) ⇒ Object



189
190
191
# File 'lib/simple_ams/dsl.rb', line 189

def meta(name = nil, value = nil, options = {})
  append_meta([name, value, options])
end

#metas(metas = []) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/simple_ams/dsl.rb', line 209

def metas(metas = [])
  return @_metas ||= [] if metas.empty?

  metas.map { |key, value| append_meta([key, value].flatten(1)) } if metas.is_a?(Hash)

  @_metas ||= metas
end

#optionsObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/simple_ams/dsl.rb', line 245

def options
  {
    adapter: adapter,
    primary_id: primary_id,
    type: type,
    fields: fields,
    relations: relations,
    includes: includes,
    links: links,
    metas: metas,
    forms: forms,
    generics: generics,
    collection: collection
  }
end

#primary_id(value = nil, options = {}) ⇒ Object



121
122
123
# File 'lib/simple_ams/dsl.rb', line 121

def primary_id(value = nil, options = {})
  @_primary_id = _value_hash_for(@_primary_id, value, options, :primary_id)
end

#relationsObject



162
163
164
# File 'lib/simple_ams/dsl.rb', line 162

def relations
  @_relations || []
end

#simple_ams?Boolean

Returns:

  • (Boolean)


261
262
263
# File 'lib/simple_ams/dsl.rb', line 261

def simple_ams?
  true
end

#type(value = nil, options = {}) ⇒ Object



125
126
127
# File 'lib/simple_ams/dsl.rb', line 125

def type(value = nil, options = {})
  @_type = _value_hash_for(@_type, value, options.merge(_explicit: true), :type)
end

#with_options(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/simple_ams/dsl.rb', line 90

def with_options(options = {})
  @_options = options
  meths = SimpleAMS::DSL::ClassMethods.instance_methods(false)
  @_options.each do |key, value|
    if key == :relations
      (value || []).each do |rel_value|
        append_relationship(rel_value)
      end
    elsif key.to_sym == :collection
      # TODO: Add proc option maybe?
      collection {}.with_options(value) if value.is_a?(Hash)
    elsif meths.include?(key)
      if (value.is_a?(Array) && value[0].is_a?(Array)) || value.is_a?(Hash)
        send(key, value)
      else
        send(key, *value)
      end
    else
      SimpleAMS.configuration.logger.info(
        "SimpeAMS: #{key} is not recognized, ignoring (from #{self})"
      )
    end
  end

  @_options
end