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)


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

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

#collection?Boolean

Returns:

  • (Boolean)


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

def collection?
  cardinality == :n
end

#denormalize_params(item, params) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/strobe/association.rb', line 40

def denormalize_params(item, params)
  return item unless targets = params[target_pluralized]

  if collection?
    collection = item[name.to_s] = []
    id, key = item["id"], "#{source.singular_resource_name}_id"

    targets.each do |target|
      collection << target if target[key] == id
    end if id
  else
    if id = item["#{name}_id"]
      item.merge!(name.to_s => targets.find { |t| t["id"] == id })
    end
  end

  item
end

#include?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/strobe/association.rb', line 36

def include?
  options[:include]
end

#nested?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/strobe/association.rb', line 32

def nested?
  options[:nested]
end

#readerObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/strobe/association.rb', line 59

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



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

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

#target_pluralizedObject



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

def target_pluralized
  name.to_s.pluralize
end

#writerObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/strobe/association.rb', line 83

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

  if collection?
    ruby << "  if val.is_a?(Array)"
    ruby << "    @__#{name} = Strobe::Collection.new( "                      \
            "      #{target_name}, { '#{source.singular_resource_name}_id' " \
            "      => self[:id] }, val)"
    ruby << "  elsif val.is_a?(Strobe::Collection)"
    ruby << "    @__#{name} = val"
    ruby << "  else"
    ruby << "    raise 'invalid association type for #{name}='"
    ruby << "  end"
  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 'invalid association type for #{name}='"
    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