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 self.to_s.end_with?('::Collection_')
    _name = self.to_s.gsub(
      'Serializer',''
    ).gsub(
      '::Collection_', ''
    ).downcase.split('::')[-1]

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

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



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

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

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



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

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

  append_attributes(args)
end

#attributes=(*args) ⇒ Object



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

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

  attributes(args)
end

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



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

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

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



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

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

  self::Collection_.type(name) if name

  return 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



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

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

  return embedded
end

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



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

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

#forms(forms = []) ⇒ Object



217
218
219
220
221
222
# 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



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

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

#generics(generics = []) ⇒ Object



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

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



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

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

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



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

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



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

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


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

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


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

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



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

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



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

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



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

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

#relationsObject



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

def relations
  @_relations || []
end

#simple_ams?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/simple_ams/dsl.rb', line 259

def simple_ams?
  true
end

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



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

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

#with_options(options = {}) ⇒ Object



89
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
116
# File 'lib/simple_ams/dsl.rb', line 89

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

  return @_options
end