Class: Formed::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Delegation
Defined in:
lib/formed/relation.rb,
lib/formed/relation/delegation.rb

Defined Under Namespace

Modules: Delegation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegation

delegated_classes, uncacheable_methods

Constructor Details

#initialize(klass, values: {}) ⇒ Relation

Returns a new instance of Relation.



14
15
16
17
18
19
20
21
# File 'lib/formed/relation.rb', line 14

def initialize(klass, values: {})
  @klass  = klass
  @values = values
  @loaded = true
  @delegate_to_klass = false
  @future_result = nil
  @records = nil
end

Instance Attribute Details

#klassObject (readonly) Also known as: model

Returns the value of attribute klass.



7
8
9
# File 'lib/formed/relation.rb', line 7

def klass
  @klass
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



7
8
9
# File 'lib/formed/relation.rb', line 7

def loaded
  @loaded
end

#skip_preloading_valueObject

Returns the value of attribute skip_preloading_value.



8
9
10
# File 'lib/formed/relation.rb', line 8

def skip_preloading_value
  @skip_preloading_value
end

Instance Method Details

#any?Boolean

Returns true if there are any records.

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/formed/relation.rb', line 91

def any?
  return super if block_given?

  !empty?
end

#empty?Boolean

Returns true if there are no records.

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/formed/relation.rb', line 75

def empty?
  if loaded?
    records.empty?
  else
    !exists?
  end
end

#encode_with(coder) ⇒ Object

Serializes the relation objects Array.



61
62
63
# File 'lib/formed/relation.rb', line 61

def encode_with(coder)
  coder.represent_seq(nil, records)
end

#initialize_copy(_other) ⇒ Object



23
24
25
26
# File 'lib/formed/relation.rb', line 23

def initialize_copy(_other)
  @values = @values.dup
  reset
end

#many?Boolean

Returns true if there is more than one record.

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/formed/relation.rb', line 106

def many?
  return super if block_given?
  return records.many? if loaded?

  limited_count > 1
end

#new(attributes = nil, &block) ⇒ Object Also known as: build

Initializes new record from relation while maintaining the current scope.

Expects arguments in the same format as ActiveRecord::Base.new.

users = User.where(name: 'DHH')
user = users.new # => #<User id: nil, name: "DHH", created_at: nil, updated_at: nil>

You can also pass a block to new with the new record as argument:

user = users.new { |user| user.name = 'Oscar' }
user.name # => Oscar


40
41
42
43
44
45
46
47
# File 'lib/formed/relation.rb', line 40

def new(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| new(attr, &block) }
  else
    block = current_scope_restoring_block(&block)
    scoping { _new(attributes, &block) }
  end
end

#none?Boolean

Returns true if there are no records.

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/formed/relation.rb', line 84

def none?
  return super if block_given?

  empty?
end

#one?Boolean

Returns true if there is exactly one record.

Returns:

  • (Boolean)


98
99
100
101
102
103
# File 'lib/formed/relation.rb', line 98

def one?
  return super if block_given?
  return records.one? if loaded?

  limited_count == 1
end

#recordsObject

:nodoc:



56
57
58
# File 'lib/formed/relation.rb', line 56

def records # :nodoc:
  @records
end

#sizeObject

Returns size of the records.



66
67
68
69
70
71
72
# File 'lib/formed/relation.rb', line 66

def size
  if loaded?
    records.length
  else
    count(:all)
  end
end

#to_aryObject Also known as: to_a

Converts relation objects to Array.



51
52
53
# File 'lib/formed/relation.rb', line 51

def to_ary
  records.dup
end