Module: SimpleAMS::DSL::ClassMethods
- Defined in:
- lib/simple_ams/dsl.rb
Instance Method Summary collapse
-
#_default_type_name ⇒ Object
TODO: Add tests !!.
- #adapter(value = nil, options = {}) ⇒ Object
- #attributes(*args) ⇒ Object (also: #attribute, #fields)
- #attributes=(*args) ⇒ Object
- #belongs_to(name, options = {}, &block) ⇒ Object
- #collection(name = nil, &block) ⇒ Object
- #default_options ⇒ Object
- #embedded_class_for(name, options, block) ⇒ Object
- #form(name, value, options = {}) ⇒ Object
- #forms(forms = []) ⇒ Object
- #generic(name, value, options = {}) ⇒ Object
- #generics(generics = []) ⇒ Object
- #has_many(name, options = {}, &block) ⇒ Object
- #has_one(name, options = {}, &block) ⇒ Object
-
#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.
-
#inherited(subclass) ⇒ Object
TODO: Shouldn’t we call here super to presever user’s behavior ?.
- #link(name, value, options = {}) ⇒ Object
- #links(links = []) ⇒ Object
- #meta(name = nil, value = nil, options = {}) ⇒ Object
- #metas(metas = []) ⇒ Object
- #options ⇒ Object
- #primary_id(value = nil, options = {}) ⇒ Object
- #relations ⇒ Object
- #simple_ams? ⇒ Boolean
- #type(value = nil, options = {}) ⇒ Object
- #with_options(options = {}) ⇒ Object
Instance Method Details
#_default_type_name ⇒ Object
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, = {}) @_adapter = _value_hash_for(@_adapter, value, , :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, = {}, &block) append_relationship( [__method__, name, , (name, , 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_options ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/simple_ams/dsl.rb', line 67 def @_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 (name, , block) = Class.new(self) klass_name = "Embedded#{name.to_s.capitalize}Options_" const_set(klass_name, ) .( .merge(.reject { |k| k == :serializer }) ) .instance_exec(&block) if block end |
#form(name, value, options = {}) ⇒ Object
193 194 195 |
# File 'lib/simple_ams/dsl.rb', line 193 def form(name, value, = {}) append_form([name, value, ]) 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, = {}) append_generic([name, value, ]) 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, = {}, &block) append_relationship( [__method__, name, , (name, , 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, = {}, &block) append_relationship( [__method__, name, , (name, , 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.( .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 { adapter: adapter, primary_id: primary_id, type: type, fields: fields, relations: relations, includes: includes, links: links, metas: , forms: forms, generics: generics } end end _klass.( self::Collection_..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 |
#link(name, value, options = {}) ⇒ Object
185 186 187 |
# File 'lib/simple_ams/dsl.rb', line 185 def link(name, value, = {}) append_link([name, value, ]) end |
#links(links = []) ⇒ Object
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 (name = nil, value = nil, = {}) ([name, value, ]) end |
#metas(metas = []) ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/simple_ams/dsl.rb', line 209 def ( = []) return @_metas ||= [] if .empty? .map { |key, value| ([key, value].flatten(1)) } if .is_a?(Hash) @_metas ||= end |
#options ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/simple_ams/dsl.rb', line 245 def { adapter: adapter, primary_id: primary_id, type: type, fields: fields, relations: relations, includes: includes, links: links, 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, = {}) @_primary_id = _value_hash_for(@_primary_id, value, , :primary_id) end |
#relations ⇒ Object
162 163 164 |
# File 'lib/simple_ams/dsl.rb', line 162 def relations @_relations || [] end |
#simple_ams? ⇒ 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, = {}) @_type = _value_hash_for(@_type, value, .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 ( = {}) @_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 {}.(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 |