Class: Strobe::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, cardinality, name, options) ⇒ Association

Returns a new instance of Association.



5
6
7
8
9
10
# File 'lib/strobe/association.rb', line 5

def initialize(source, cardinality, name, options)
  @source      = source
  @cardinality = cardinality
  @name        = name.to_sym
  @options     = options
end

Instance Attribute Details

#cardinalityObject (readonly)

Returns the value of attribute cardinality.



3
4
5
# File 'lib/strobe/association.rb', line 3

def cardinality
  @cardinality
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/strobe/association.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/strobe/association.rb', line 3

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/strobe/association.rb', line 3

def source
  @source
end

Instance Method Details

#autoload?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/strobe/association.rb', line 24

def autoload?
  !options.key?(:autoload) || options[:autoload]
end

#collection?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/strobe/association.rb', line 20

def collection?
  cardinality == :n
end

#include?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/strobe/association.rb', line 28

def include?
  options[:include]
end

#readerObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/strobe/association.rb', line 32

def reader
  ruby = []
  ruby <<   "def #{name}"

  if autoload? && collection?

    ruby << "  @__#{name} ||= Strobe::Collection.new( "                  \
            "    #{target_name}, '#{source.singular_resource_name}_id' " \
            "    => self[:id] ) if self[:id]"

  elsif autoload?

    ruby << " return @__#{name} if @__#{name}"
    ruby << " return nil unless self[:#{name}_id]"
    ruby << " @__#{name} = #{target_name}.get!( self[:#{name}_id] )"

  end

  ruby <<   "  @__#{name}"

  ruby <<   "end"
  ruby.join("\n")
end

#targetObject



16
17
18
# File 'lib/strobe/association.rb', line 16

def target
  @target ||= target_name.constantize
end

#target_nameObject



12
13
14
# File 'lib/strobe/association.rb', line 12

def target_name
  'Strobe::Resources::' + name.to_s.classify
end

#writerObject



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
83
84
85
# File 'lib/strobe/association.rb', line 56

def writer
  ruby = []
  ruby <<   "def #{name}=(val)"

  if collection?

    ruby << "  raise NotImplementedError"

  else

    ruby << "  if val"
    ruby << "    if val.is_a?(Hash)"
    ruby << "      val = #{target_name}.new(val)"
    ruby << "    elsif !val.is_a?(#{target_name})"
    ruby << "      raise 'fail'"
    ruby << "    end"

    ruby << "    self[:#{name}_id] = val[:id]" if autoload?
    ruby << "    @__#{name} = val"
    ruby << "  else"
    ruby << "    self[:#{name}_id] = nil" if autoload?
    ruby << "    @__#{name} = nil"
    ruby << "  end"

  end

  ruby <<   "end"

  ruby.join("\n")
end