Class: OdataDuty::EntitySet::Metadata

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_set) ⇒ Metadata

Returns a new instance of Metadata.



41
42
43
# File 'lib/odata_duty.rb', line 41

def initialize(entity_set)
  @entity_set = entity_set
end

Instance Attribute Details

#entity_setObject (readonly)

Returns the value of attribute entity_set.



39
40
41
# File 'lib/odata_duty.rb', line 39

def entity_set
  @entity_set
end

Instance Method Details

#collection(set_builder, context:, selected:) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/odata_duty.rb', line 74

def collection(set_builder, context:, selected:)
  begin
    values = set_builder.collection
  rescue NoMethodError
    raise NoImplementationError, "collection not implemented for #{entity_set}"
  end

  mapper = entity_type.mapper(context, selected: selected)

  values.map { |v| mapper.obj_to_hash(v, context) }
end

#create(context:) ⇒ Object



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

def create(context:)
  wrapper = CreateComplexTypeHashWrapper.new(context.query_options, entity_type,
                                             operation: :create, context: context)
  result = entity_set.new(context: context)
                     .create(wrapper)
  mapper = entity_type.mapper(context, selected: nil)
  mapper.obj_to_hash(result, context)
end

#delete(id, context:) ⇒ Object



116
117
118
119
# File 'lib/odata_duty.rb', line 116

def delete(id, context:)
  result = entity_set.new(context: context).delete(converted_id(id))
  raise ResourceNotFoundError, "No such entity #{id}" unless result
end

#entity_typeObject



54
55
56
# File 'lib/odata_duty.rb', line 54

def entity_type
  entity_set.entity_type
end

#entity_type_nameObject



58
59
60
# File 'lib/odata_duty.rb', line 58

def entity_type_name
  entity_type..name
end

#individual(set_builder, id, context:, selected:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/odata_duty.rb', line 86

def individual(set_builder, id, context:, selected:)
  begin
    result = set_builder.individual(converted_id(id))
  rescue NoMethodError
    raise NoImplementationError, "individual not implemented for #{entity_set}"
  end

  raise ResourceNotFoundError, "No such entity #{id}" unless result

  entity_type.mapper(context, selected: selected).obj_to_hash(result, context)
end

#kindObject



62
63
64
# File 'lib/odata_duty.rb', line 62

def kind
  'EntitySet'
end

#metadata_typesObject



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

def 
  [entity_type].map(&:__metadata).flat_map(&:metadata_types)
end

#nameObject



49
50
51
52
# File 'lib/odata_duty.rb', line 49

def name
  entity_set.name ||
    entity_set.to_s.split('::').last.sub(/EntitySet\z/, '').sub(/Set\z/, '')
end

#new_entity_set(context:) ⇒ Object



70
71
72
# File 'lib/odata_duty.rb', line 70

def new_entity_set(context:)
  entity_set.new(context: context)
end

#non_insertable_property_namesObject



160
161
162
163
# File 'lib/odata_duty.rb', line 160

def non_insertable_property_names
  @non_insertable_property_names ||=
    entity_type.properties.select(&:non_insertable?).map(&:name)
end

#supports_collection?Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/odata_duty.rb', line 130

def supports_collection?
  # Check if the entity set class supports read by looking for the collection method
  entity_set.method_defined?(:collection)
end

#supports_count?Boolean

Returns:

  • (Boolean)


140
141
142
143
# File 'lib/odata_duty.rb', line 140

def supports_count?
  # Check if the entity set class supports counting via the count method
  entity_set.method_defined?(:count)
end

#supports_create?Boolean

Returns:

  • (Boolean)


145
146
147
148
# File 'lib/odata_duty.rb', line 145

def supports_create?
  # Check if the entity set class supports create by looking for the create method
  entity_set.method_defined?(:create)
end

#supports_delete?Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/odata_duty.rb', line 155

def supports_delete?
  # Check if the entity set class supports delete by looking for the delete method
  entity_set.method_defined?(:delete)
end

#supports_filter_or?Boolean

Returns:

  • (Boolean)


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

def supports_filter_or?
  entity_set.method_defined?(:od_filter_or)
end

#supports_individual?Boolean

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/odata_duty.rb', line 135

def supports_individual?
  # Check if the entity set class supports read-by-id via the individual method
  entity_set.method_defined?(:individual)
end

#supports_search?Boolean

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/odata_duty.rb', line 121

def supports_search?
  # Check if the entity set class supports search by looking for the od_search method
  entity_set.method_defined?(:od_search)
end

#supports_update?Boolean

Returns:

  • (Boolean)


150
151
152
153
# File 'lib/odata_duty.rb', line 150

def supports_update?
  # Check if the entity set class supports update by looking for the update method
  entity_set.method_defined?(:update)
end

#update(id, context:) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/odata_duty.rb', line 107

def update(id, context:)
  wrapper = CreateComplexTypeHashWrapper.new(context.query_options, entity_type,
                                             operation: :update, context: context)
  result = entity_set.new(context: context).update(converted_id(id), wrapper)
  raise ResourceNotFoundError, "No such entity #{id}" unless result

  entity_type.mapper(context, selected: nil).obj_to_hash(result, context)
end

#urlObject



66
67
68
# File 'lib/odata_duty.rb', line 66

def url
  entity_set.url || name
end