Class: DataMapper::PropertySet

Inherits:
Object
  • Object
show all
Includes:
Assertions, Enumerable
Defined in:
lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb

Instance Method Summary collapse

Methods included from Enumerable

#to_json

Methods included from Assertions

#assert_kind_of

Instance Method Details

#[](name) ⇒ Object



6
7
8
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 6

def [](name)
  property_for(name) || raise(ArgumentError, "Unknown property '#{name}'", caller)
end

#[]=(name, property) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 10

def []=(name, property)
  @key, @defaults = nil
  if existing_property = detect { |p| p.name == name }
    property.hash
    @entries[@entries.index(existing_property)] = property
  else
    add(property)
  end
  property
end

#add(*properties) ⇒ Object Also known as: <<



37
38
39
40
41
42
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 37

def add(*properties)
  @key, @defaults = nil
  @entries.push(*properties)
  properties.each { |property| property.hash }
  self
end

#clearObject



32
33
34
35
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 32

def clear
  @key, @defaults = nil
  @entries.clear
end

#defaultsObject



59
60
61
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 59

def defaults
  @defaults ||= reject { |property| property.lazy? }
end

#eachObject



54
55
56
57
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 54

def each
  @entries.each { |property| yield property }
  self
end

#empty?Boolean

Returns:



50
51
52
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 50

def empty?
  @entries.empty?
end

#get(resource) ⇒ Object



81
82
83
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 81

def get(resource)
  map { |property| property.get(resource) }
end

#has_property?(name) ⇒ Boolean

Returns:



21
22
23
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 21

def has_property?(name)
  !!property_for(name)
end

#indexesObject



67
68
69
70
71
72
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 67

def indexes
  index_hash = {}
  repository_name = repository.name
  each { |property| parse_index(property.index, property.field(repository_name), index_hash) }
  index_hash
end

#inspectObject



127
128
129
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 127

def inspect
  '#<PropertySet:{' + map { |property| property.inspect }.join(',') + '}>'
end

#keyObject



63
64
65
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 63

def key
  @key ||= select { |property| property.key? }
end

#lazy_context(name) ⇒ Object



101
102
103
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 101

def lazy_context(name)
  lazy_contexts[name] ||= []
end

#lazy_load_context(names) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 105

def lazy_load_context(names)
  if names.kind_of?(Array) && names.empty?
    raise ArgumentError, '+names+ cannot be empty', caller
  end

  result = []

  Array(names).each do |name|
    contexts = property_contexts(name)
    if contexts.empty?
      result << name  # not lazy
    else
      result |= lazy_contexts.values_at(*contexts).flatten.uniq
    end
  end
  result
end

#lengthObject



46
47
48
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 46

def length
  @entries.length
end

#property_contexts(name) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 93

def property_contexts(name)
  contexts = []
  lazy_contexts.each do |context,property_names|
    contexts << context if property_names.include?(name)
  end
  contexts
end

#set(resource, values) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 85

def set(resource, values)
  if values.kind_of?(Array) && values.length != length
    raise ArgumentError, "+values+ must have a length of #{length}, but has #{values.length}", caller
  end

  each_with_index { |property,i| property.set(resource, values.nil? ? nil : values[i]) }
end

#slice(*names) ⇒ Object



25
26
27
28
29
30
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 25

def slice(*names)
  @key, @defaults = nil
  names.map do |name|
    property_for(name)
  end
end

#to_query(bind_values) ⇒ Object



123
124
125
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 123

def to_query(bind_values)
  Hash[ *zip(bind_values).flatten ]
end

#unique_indexesObject



74
75
76
77
78
79
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/property_set.rb', line 74

def unique_indexes
  index_hash = {}
  repository_name = repository.name
  each { |property| parse_index(property.unique_index, property.field(repository_name), index_hash) }
  index_hash
end