Class: DataMapper::PropertySet
- Inherits:
-
Object
- Object
- DataMapper::PropertySet
- Includes:
- Assertions, Enumerable
- Defined in:
- lib/dm-core/property_set.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, property) ⇒ Object
- #add(*properties) ⇒ Object (also: #<<)
- #defaults ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #get(resource) ⇒ Object
- #has_property?(name) ⇒ Boolean
- #indexes ⇒ Object
- #inheritance_property ⇒ Object
- #inspect ⇒ Object
- #key ⇒ Object
- #lazy_context(name) ⇒ Object
- #lazy_load_context(names) ⇒ Object
- #length ⇒ Object
- #property_contexts(name) ⇒ Object
- #set(resource, values) ⇒ Object
- #slice(*names) ⇒ Object
- #to_query(bind_values) ⇒ Object
- #unique_indexes ⇒ Object
Methods included from Assertions
Instance Method Details
#[](name) ⇒ Object
6 7 8 |
# File '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 |
# File 'lib/dm-core/property_set.rb', line 10 def []=(name, property) 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: <<
28 29 30 31 32 |
# File 'lib/dm-core/property_set.rb', line 28 def add(*properties) @entries.push(*properties) properties.each { |property| property.hash } self end |
#defaults ⇒ Object
49 50 51 |
# File 'lib/dm-core/property_set.rb', line 49 def defaults reject { |property| property.lazy? } end |
#each ⇒ Object
44 45 46 47 |
# File 'lib/dm-core/property_set.rb', line 44 def each @entries.each { |property| yield property } self end |
#empty? ⇒ Boolean
40 41 42 |
# File 'lib/dm-core/property_set.rb', line 40 def empty? @entries.empty? end |
#get(resource) ⇒ Object
75 76 77 |
# File 'lib/dm-core/property_set.rb', line 75 def get(resource) map { |property| property.get(resource) } end |
#has_property?(name) ⇒ Boolean
20 21 22 |
# File 'lib/dm-core/property_set.rb', line 20 def has_property?(name) !!@property_for[name] end |
#indexes ⇒ Object
57 58 59 60 61 62 |
# File 'lib/dm-core/property_set.rb', line 57 def indexes index_hash = {} repository_name = repository.name each { |property| parse_index(property.index, property.field(repository_name), index_hash) } index_hash end |
#inheritance_property ⇒ Object
71 72 73 |
# File 'lib/dm-core/property_set.rb', line 71 def inheritance_property detect { |property| property.type == DataMapper::Types::Discriminator } end |
#inspect ⇒ Object
121 122 123 |
# File 'lib/dm-core/property_set.rb', line 121 def inspect '#<PropertySet:{' + map { |property| property.inspect }.join(',') + '}>' end |
#key ⇒ Object
53 54 55 |
# File 'lib/dm-core/property_set.rb', line 53 def key select { |property| property.key? } end |
#lazy_context(name) ⇒ Object
95 96 97 |
# File 'lib/dm-core/property_set.rb', line 95 def lazy_context(name) lazy_contexts[name] end |
#lazy_load_context(names) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/dm-core/property_set.rb', line 99 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 |
#length ⇒ Object
36 37 38 |
# File 'lib/dm-core/property_set.rb', line 36 def length @entries.length end |
#property_contexts(name) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/dm-core/property_set.rb', line 87 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
79 80 81 82 83 84 85 |
# File 'lib/dm-core/property_set.rb', line 79 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
24 25 26 |
# File 'lib/dm-core/property_set.rb', line 24 def slice(*names) @property_for.values_at(*names) end |
#to_query(bind_values) ⇒ Object
117 118 119 |
# File 'lib/dm-core/property_set.rb', line 117 def to_query(bind_values) Hash[ *zip(bind_values).flatten ] end |
#unique_indexes ⇒ Object
64 65 66 67 68 69 |
# File 'lib/dm-core/property_set.rb', line 64 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 |