Class: Strobe::Association
- Inherits:
-
Object
- Object
- Strobe::Association
- Defined in:
- lib/strobe/association.rb
Instance Attribute Summary collapse
-
#cardinality ⇒ Object
readonly
Returns the value of attribute cardinality.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #autoload? ⇒ Boolean
- #collection? ⇒ Boolean
- #denormalize_params(item, params) ⇒ Object
- #include? ⇒ Boolean
-
#initialize(source, cardinality, name, options) ⇒ Association
constructor
A new instance of Association.
- #nested? ⇒ Boolean
- #reader ⇒ Object
- #target ⇒ Object
- #target_name ⇒ Object
- #target_pluralized ⇒ Object
- #writer ⇒ Object
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, ) @source = source @cardinality = cardinality @name = name.to_sym @options = end |
Instance Attribute Details
#cardinality ⇒ Object (readonly)
Returns the value of attribute cardinality.
3 4 5 |
# File 'lib/strobe/association.rb', line 3 def cardinality @cardinality end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/strobe/association.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/strobe/association.rb', line 3 def @options end |
#source ⇒ Object (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
28 29 30 |
# File 'lib/strobe/association.rb', line 28 def autoload? !.key?(:autoload) || [:autoload] end |
#collection? ⇒ 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
36 37 38 |
# File 'lib/strobe/association.rb', line 36 def include? [:include] end |
#nested? ⇒ Boolean
32 33 34 |
# File 'lib/strobe/association.rb', line 32 def nested? [:nested] end |
#reader ⇒ Object
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 |
#target ⇒ Object
20 21 22 |
# File 'lib/strobe/association.rb', line 20 def target @target ||= target_name.constantize end |
#target_name ⇒ Object
12 13 14 |
# File 'lib/strobe/association.rb', line 12 def target_name 'Strobe::Resources::' + name.to_s.classify end |
#target_pluralized ⇒ Object
16 17 18 |
# File 'lib/strobe/association.rb', line 16 def target_pluralized name.to_s.pluralize end |
#writer ⇒ Object
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 |