Module: EmbeddedRecord

Defined in:
lib/embedded_record.rb

Defined Under Namespace

Modules: Record

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
# File 'lib/embedded_record.rb', line 4

def self.included(klass)
  klass.extend self
end

Instance Method Details

#embed_record(name, options = {}) ⇒ Object

Options:

- :class - Class to embed
- :scope - Boolean wheter to install ActiveRecord scope


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/embedded_record.rb', line 12

def embed_record(name, options = {})
  klass = options[:class] || EmbeddedRecord.constantize(name.to_s)
  attr = "#{name}_mask"
  all = klass.all

  define_method name do
    if val = send(attr)
      klass.all[val]
    elsif klass.null_record
      klass.null_record
    end
  end

  define_method "#{name}_id" do
    if val = send(attr)
      klass.all[val].id
    end
  end

  define_method "#{name}_id=" do |id|
    type_method = embed_id_type_method(klass)
    index = klass.all.index { |obj| obj.id == id.send(type_method) }
    send "#{attr}=", index
    index
  end

  if options[:scope] == true
    embed_record_scope name
  end
end

#embed_record_scope(name) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/embedded_record.rb', line 84

def embed_record_scope(name)
  klass = EmbeddedRecord.constantize(name)

  send :scope, :"with_#{name}", lambda { |*ids|
    masks = ids.map { |id| klass.find(id).index }
    where("#{name}_mask in (?)", masks)
  }
end

#embed_records(name, options = {}) ⇒ Object

Options:

- class - Class of record
- singular - singular form of name

Example:

class Shirt
  embed_records :colors, :class => Color, :singular => :color
end


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/embedded_record.rb', line 54

def embed_records(name, options = {})
  singular = options[:singular] || EmbeddedRecord.singularize(name.to_s)
  klass = options[:class] || EmbeddedRecord.constantize(singular.to_s)
  all_ids = klass.all.map { |obj| obj.id }
  attr = "#{name}_mask"

  define_method "#{singular}_ids=" do |ids|
    type_method = embed_id_type_method(klass)
    ids = ids.map(&type_method)
    self.send "#{attr}=",
      (ids & all_ids).map { |r| 2**all_ids.index(r) }.inject(0, :+)
  end

  define_method "#{singular}_ids" do
    all_ids.reject { |r| ((send(attr) || 0) & 2**all_ids.index(r)).zero? }
  end

  define_method name do
    ids = send("#{singular}_ids")

    klass.all.select do |obj|
      ids.include? obj.id
    end
  end

  if options[:scope] == true
    embed_records_scope name
  end
end

#embed_records_scope(name) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/embedded_record.rb', line 93

def embed_records_scope(name)
  klass = EmbeddedRecord.constantize(name)

  send :scope, :"with_#{name}", lambda { |*ids|
    masks = ids.map { |id| 2 ** klass.find(id).index }.join(" | ")
    where("#{name}_mask & (?)", masks)
  }
end