Class: SimpleAMS::Options

Inherits:
Object
  • Object
show all
Includes:
Concerns::TrackedProperties
Defined in:
lib/simple_ams/options.rb,
lib/simple_ams/options/type.rb,
lib/simple_ams/options/forms.rb,
lib/simple_ams/options/links.rb,
lib/simple_ams/options/metas.rb,
lib/simple_ams/options/fields.rb,
lib/simple_ams/options/adapter.rb,
lib/simple_ams/options/generics.rb,
lib/simple_ams/options/includes.rb,
lib/simple_ams/options/relations.rb,
lib/simple_ams/options/primary_id.rb,
lib/simple_ams/options/concerns/mod.rb,
lib/simple_ams/options/concerns/filterable.rb,
lib/simple_ams/options/concerns/value_hash.rb,
lib/simple_ams/options/concerns/name_value_hash.rb,
lib/simple_ams/options/concerns/tracked_properties.rb

Direct Known Subclasses

Collection

Defined Under Namespace

Modules: Concerns Classes: Adapter, Collection, EmptySerializer, Fields, Forms, Generics, Includes, Links, Metas, PrimaryId, Relations, Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::TrackedProperties

#clean_volatile_properties!, #initialize_tracking!, #tracked

Constructor Details

#initialize(resource = nil, injected_options: {}, allowed_options: nil) ⇒ Options

injected_options is always a Hash object



11
12
13
14
15
16
17
# File 'lib/simple_ams/options.rb', line 11

def initialize(resource = nil, injected_options: {}, allowed_options: nil)
  initialize_tracking!
  @resource = resource
  @injected_options = injected_options || {}
  @_internal = @injected_options[:_internal] || {}
  @allowed_options = allowed_options || fetch_allowed_options
end

Instance Attribute Details

#allowed_optionsObject (readonly)

Returns the value of attribute allowed_options.



8
9
10
# File 'lib/simple_ams/options.rb', line 8

def allowed_options
  @allowed_options
end

#injected_optionsObject (readonly)

Returns the value of attribute injected_options.



8
9
10
# File 'lib/simple_ams/options.rb', line 8

def injected_options
  @injected_options
end

#resourceObject (readonly) Also known as: collection

Returns the value of attribute resource.



8
9
10
# File 'lib/simple_ams/options.rb', line 8

def resource
  @resource
end

Instance Method Details

#adapter(_serializer: nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/simple_ams/options.rb', line 107

def adapter(_serializer: nil)
  return @adapter if defined?(@adapter) && _serializer.nil?

  serializer = _serializer || serializer

  @adapter = Adapter.new(
    *injected_options.fetch(:adapter, [nil]),
    resource: resource, serializer: serializer
  )
  if @adapter.value.nil?
    @adapter = Adapter.new(
      *allowed_options.fetch(:adapter, [nil]),
      resource: resource, serializer: serializer
    )
  end
  #       if @adapter.value.nil?
  #         @adapter = Adapter.new(SimpleAMS::Adapters::AMS, {
  #           resource: resource, serializer: serializer
  #         })
  #       end

  @adapter
end

#as_hashObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/simple_ams/options.rb', line 136

def as_hash
  {
    adapter: adapter.raw,
    primary_id: primary_id.raw,
    type: type.raw,
    name: name,
    fields: fields.raw,
    serializer: serializer_class,
    # relations: relations.raw, #TODO: why have I commented that out ?
    includes: includes.raw,
    links: links.raw,
    metas: metas.raw,
    expose: expose,
    _internal: _internal
  }
end

#collection_optionsObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/simple_ams/options.rb', line 153

def collection_options
  return @collection_options if defined?(@collection_options)

  # TODO: Do we need that merge ?
  _injected_options = @injected_options.fetch(:collection, {}).merge({
    serializer: collection_serializer_class,
    adapter: adapter(_serializer: collection_serializer_class).raw,
    expose: expose
  })
  _allowed_options = @allowed_options.fetch(:collection).options

  @collection_options = self.class::Collection.new(
    resource,
    injected_options: _injected_options,
    allowed_options: _allowed_options
  )
end

#collection_serializer_classObject

TODO: maybe have that inside :collection? (isomorphism)



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/simple_ams/options.rb', line 182

def collection_serializer_class
  return @collection_serializer_class if defined?(@collection_serializer_class)

  if serializer_class.is_a?(Proc) # TODO: maybe we should do duck typing instead?
    @collection_serializer_class = injected_options[:collection_serializer]
    if @collection_serializer_class.nil?
      raise 'In case of a proc serializer, you need to specify a collection_serializer'
    end
  else
    @collection_serializer_class = serializer_class
  end

  @collection_serializer_class
end

#exposeObject

the following should be the same for all (nested) serializers of the same document



132
133
134
# File 'lib/simple_ams/options.rb', line 132

def expose
  @expose ||= injected_options.fetch(:expose, {})
end

#fieldsObject

TODO: optimize for nested fields?



50
51
52
53
54
# File 'lib/simple_ams/options.rb', line 50

def fields
  return @fields if defined?(@fields)

  @fields = array_of_items_for(Fields, :fields)
end

#formsObject



84
85
86
87
88
# File 'lib/simple_ams/options.rb', line 84

def forms
  return tracked(:forms).value if tracked(:forms).value

  tracked(:forms).value = array_of_name_value_hash_for(Forms, Forms::Form, :forms)
end

#genericsObject



90
91
92
93
94
95
96
# File 'lib/simple_ams/options.rb', line 90

def generics
  return tracked(:generics).value if tracked(:generics).value

  tracked(:generics).value = array_of_name_value_hash_for(
    Generics, Generics::Option, :generics
  )
end

#includesObject



56
57
58
59
60
# File 'lib/simple_ams/options.rb', line 56

def includes
  return @includes if defined?(@includes)

  @includes = array_of_items_for(Includes, :includes)
end


72
73
74
75
76
# File 'lib/simple_ams/options.rb', line 72

def links
  return tracked(:links).value if tracked(:links).value

  tracked(:links).value = array_of_name_value_hash_for(Links, Links::Link, :links)
end

#metasObject



78
79
80
81
82
# File 'lib/simple_ams/options.rb', line 78

def metas
  return tracked(:metas).value if tracked(:metas).value

  tracked(:metas).value = array_of_name_value_hash_for(Metas, Metas::Meta, :metas)
end

#nameObject



45
46
47
# File 'lib/simple_ams/options.rb', line 45

def name
  @name ||= injected_options[:name] || allowed_options[:name] || type.name
end

#primary_idObject



33
34
35
36
37
# File 'lib/simple_ams/options.rb', line 33

def primary_id
  return tracked(:primary_id).value if tracked(:primary_id).value

  tracked(:primary_id).value = array_of_value_hash_for(PrimaryId, :primary_id)
end

#relation_options_for(relation_name) ⇒ Object



29
30
31
# File 'lib/simple_ams/options.rb', line 29

def relation_options_for(relation_name)
  _relation_options[relation_name] || {}
end

#relationsObject

TODO: correctly loop over injected relations, although should be a rarely used feature



63
64
65
66
67
68
69
70
# File 'lib/simple_ams/options.rb', line 63

def relations
  return @relations if defined?(@relations)

  relations = injected_options.fetch(:relations, nil)
  relations = allowed_options.fetch(:relations, []) if relations.nil?

  @relations = Relations.new(relations, includes)
end

#serializerObject

TODO: handle case of proc



99
100
101
102
103
104
105
# File 'lib/simple_ams/options.rb', line 99

def serializer
  return @serializer if defined?(@serializer)

  _serializer = injected_options.fetch(:serializer, serializer_class)

  @serializer = instantiated_serializer_for(_serializer)
end

#serializer_classObject



171
172
173
174
175
176
177
178
179
# File 'lib/simple_ams/options.rb', line 171

def serializer_class
  return @serializer_class if defined?(@serializer_class)

  @serializer_class = injected_options.fetch(:serializer, nil)

  return @serializer_class if @serializer_class

  @serializer_class = infer_serializer
end

#typeObject



39
40
41
42
43
# File 'lib/simple_ams/options.rb', line 39

def type
  return tracked(:type).value if tracked(:type).value

  tracked(:type).value = array_of_value_hash_for(Type, :type)
end

#with_resource(resource) ⇒ Object

performance enchancement method for non-polymorphic collections



21
22
23
24
25
26
27
# File 'lib/simple_ams/options.rb', line 21

def with_resource(resource)
  @resource = resource
  remove_instance_variable(:@serializer) if defined?(@serializer)
  clean_volatile_properties!

  self
end