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
- #include? ⇒ Boolean
-
#initialize(source, cardinality, name, options) ⇒ Association
constructor
A new instance of Association.
- #reader ⇒ Object
- #target ⇒ Object
- #target_name ⇒ 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
24 25 26 |
# File 'lib/strobe/association.rb', line 24 def autoload? !.key?(:autoload) || [:autoload] end |
#collection? ⇒ Boolean
20 21 22 |
# File 'lib/strobe/association.rb', line 20 def collection? cardinality == :n end |
#include? ⇒ Boolean
28 29 30 |
# File 'lib/strobe/association.rb', line 28 def include? [:include] end |
#reader ⇒ Object
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 |
#target ⇒ Object
16 17 18 |
# File 'lib/strobe/association.rb', line 16 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 |
#writer ⇒ Object
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 |
# 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 |