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 |
# File 'lib/strobe/association.rb', line 40 def denormalize_params(item, params) return item unless targets = params[target_pluralized] if collection? raise NotImplemented else if id = item["#{name}_id"] return 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/strobe/association.rb', line 54 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
78 79 80 81 82 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 |
# File 'lib/strobe/association.rb', line 78 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 |