Module: OData::Model::Attributes::ClassMethods

Defined in:
lib/odata/model/attributes.rb

Overview

Methods mixed in at the class level.

Instance Method Summary collapse

Instance Method Details

#attribute_optionsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a hash keyed to the attribute name of passed options from the property definitions.

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
# File 'lib/odata/model/attributes.rb', line 63

def attribute_options
  if self.class_variable_defined?(:@@attribute_options)
    class_variable_get(:@@attribute_options)
  else
    class_variable_set(:@@attribute_options, {})
    class_variable_get(:@@attribute_options)
  end
end

#attributesArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array of all registered attributes

Returns:

  • (Array)


50
51
52
53
54
55
56
57
# File 'lib/odata/model/attributes.rb', line 50

def attributes
  if self.class_variable_defined?(:@@attributes)
    class_variable_get(:@@attributes)
  else
    class_variable_set(:@@attributes, [])
    class_variable_get(:@@attributes)
  end
end

#create_accessors(attribute_name) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create attribute accessors for the supplied attribute name.

Returns:

  • (nil)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/odata/model/attributes.rb', line 110

def create_accessors(attribute_name)
  class_eval do
    define_method(attribute_name) do
      odata_entity[property_map[attribute_name]]
    end

    define_method("#{attribute_name}=") do |value|
      # unless entity[property_map[attribute_name]] == value
      #   send("#{attribute_name}_will_change!") if defined?(::ActiveModel)
      # end

      odata_entity[property_map[attribute_name]] = value
    end
  end

  nil
end

#property(literal_name, options = {}) ⇒ Object

Defines a property from this model’s related OData::Entity you want mapped to an attribute.

Parameters:

  • literal_name (to_s)

    the literal Entity property name

  • options (Hash) (defaults to: {})

    hash of options

Returns:

  • nil



36
37
38
39
40
41
42
43
44
45
# File 'lib/odata/model/attributes.rb', line 36

def property(literal_name, options = {})
  attribute_name = (options[:as] || literal_name.to_s.underscore).to_sym

  validate_attribute(literal_name)
  register_attribute(attribute_name, literal_name, options)
  create_accessors(attribute_name)
  #define_attribute_methods [attribute_name] if defined?(::ActiveModel)

  nil
end

#property_mapHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a hash keyed to the attribute name with the values being the literal OData property name to use when accessing entity data.

Returns:

  • (Hash)


76
77
78
79
80
81
82
83
# File 'lib/odata/model/attributes.rb', line 76

def property_map
  if self.class_variable_defined?(:@@property_map)
    class_variable_get(:@@property_map)
  else
    class_variable_set(:@@property_map, {})
    class_variable_get(:@@property_map)
  end
end

#register_attribute(attribute_name, literal_name, options) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers a supplied attribute with its literal property name and any provided options.

Returns:

  • (nil)


100
101
102
103
104
105
# File 'lib/odata/model/attributes.rb', line 100

def register_attribute(attribute_name, literal_name, options)
  attributes << attribute_name
  attribute_options[attribute_name] = options
  property_map[attribute_name] = literal_name
  nil
end

#validate_attribute(property_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates the existence of the supplied attribute on the relevant Entity.

Parameters:

  • property_name (to_s)


89
90
91
92
93
94
# File 'lib/odata/model/attributes.rb', line 89

def validate_attribute(property_name)
  valid_properties = odata_service.properties_for_entity(odata_entity_set.type)
  unless valid_properties[property_name.to_s]
    raise ArgumentError, "property #{property_name} does not exist for #{odata_entity_set.type} entity"
  end
end