class HypertextApplicationLanguage::DataMapper::Grape::API

Public Instance Methods

association() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 129
def association
  resource.__send__(relationship_name)
end
association=(target) click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 133
def association=(target)
  resource.__send__("#{relationship_name}=", target)
end
attribute_names(property_names=property_names) click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 30
def attribute_names(property_names=property_names)
  property_names.map(&:to_s).select do |name|
    !name.end_with?('_id')
  end - %w(id)
end
attributes() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 36
def attributes
  params.slice(*attribute_names)
end
model() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 19
def model
  ::DataMapper::Model.descendants.select do |model|
    model.name == model_name
  end.first
end
model_name() click to toggle source

Takes the model name from the route path. Takes a string and answers a string, but the result is upper camel-case and singular. @return [String] Answers the name of the model based on the route parameter.

# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 15
def model_name
  params[:model].classify
end
property_names(model=model) click to toggle source

@return [Array<Symbol>] Answers the names of the model properties.

# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 26
def property_names(model=model)
  model.properties.map(&:name)
end
query(model=model) click to toggle source

The order parameter is an array of Data Mapper query directions. Each direction specifies a target property and a vector, ascending or descending, space delimited (or plus delimited after URL encoding). However, Grape cannot coerce the directions because directions need access to the model properties in order to construct the direction. The model comes from the helpers who only become available after parameter coercion and validation of parameters. Note that Grape does coerce the order to an array even if the parameters do not specify an array.

# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 49
def query(model=model)
  query = declared(params, include_missing: true)
  if query.order
    query.order.map! do |direction|
      target, operator = direction.split(' ', 2)
      property = model.properties[target]
      error! "no property named #{target}" unless property
      ::DataMapper::Query::Direction.new(property, operator || 'asc')
    end
  end
  query.to_h.symbolize_keys
end
relationship() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 107
def relationship
  resource.model.relationships.select do |relationship|
    relationship.name.to_s == relationship_name
  end.first
end
relationship_name() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 103
def relationship_name
  params[:relationship]
end
resource() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 80
def resource
  model.get(params[:id])
end
target_attribute_names() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 121
def target_attribute_names
  attribute_names(target_property_names)
end
target_attributes() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 125
def target_attributes
  params.slice(*target_attribute_names)
end
target_model() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 113
def target_model
  relationship.target_model
end
target_property_names() click to toggle source
# File lib/hypertext_application_language/data_mapper/grape/api.rb, line 117
def target_property_names
  property_names(target_model)
end