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)


73
74
75
76
77
78
79
80
# File 'lib/odata/model/attributes.rb', line 73

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)


60
61
62
63
64
65
66
67
# File 'lib/odata/model/attributes.rb', line 60

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)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/odata/model/attributes.rb', line 120

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



47
48
49
50
51
52
53
54
55
# File 'lib/odata/model/attributes.rb', line 47

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)


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

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)


110
111
112
113
114
115
# File 'lib/odata/model/attributes.rb', line 110

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)


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

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