Module: OdataDuty::Property

Defined in:
lib/odata_duty/property.rb,
lib/odata_duty/property/single_prop.rb,
lib/odata_duty/property/collection_prop.rb

Defined Under Namespace

Classes: CollectionProp, SingleProp

Constant Summary collapse

MUTABILITIES =
%i[read_write immutable non_insertable computed].freeze
MUTABILITIES_LIST =
MUTABILITIES.map(&:inspect).join(', ').freeze
NAME_REGEXP =
Regexp.new('\A(?:\p{L}|_)(?:[\p{L}\p{Nd}_])*\z').freeze
CORE_ANNOTATION_TERMS =
{ computed: 'Org.OData.Core.V1.Computed',
immutable: 'Org.OData.Core.V1.Immutable' }.freeze

Class Method Summary collapse

Class Method Details

.computed_to_mutability(name, computed, mutability) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/odata_duty/property.rb', line 34

def self.computed_to_mutability(name, computed, mutability)
  unless mutability == :unset
    raise ArgumentError,
          "#{name}: pass either `mutability:` or `computed:`, not both \u2014 they control " \
          'the same axis'
  end
  computed ? :computed : :read_write
end

.new(name, type = String, line__defined__at: nil, nullable: true, method: nil, computed: :unset, mutability: :unset) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/odata_duty/property.rb', line 10

def self.new(name, type = String, line__defined__at: nil, nullable: true, method: nil,
             computed: :unset, mutability: :unset)
  unless valid_name?(name)
    raise InvalidNCNamesError, "\"#{name}\" is not a valid property name"
  end

  prop_class = type.instance_of?(Array) ? CollectionProp : SingleProp
  prop_class.new(name, type,
                 line__defined__at: line__defined__at,
                 nullable: nullable,
                 method: method,
                 mutability: resolve_mutability(name, computed, mutability))
end

.resolve_mutability(name, computed, mutability) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'lib/odata_duty/property.rb', line 24

def self.resolve_mutability(name, computed, mutability)
  mutability = computed_to_mutability(name, computed, mutability) unless computed == :unset
  mutability = :read_write if mutability == :unset
  return mutability if MUTABILITIES.include?(mutability)

  raise ArgumentError,
        "#{name}: invalid mutability #{mutability.inspect}, " \
        "must be one of #{MUTABILITIES_LIST}"
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.valid_name?(name)
  name.match?(NAME_REGEXP)
end