Class: Boxxspring::Parser
- Inherits:
-
Object
- Object
- Boxxspring::Parser
- Defined in:
- lib/boxxspring/parser.rb
Instance Method Summary collapse
- #associations ⇒ Object
-
#initialize(content = {}) ⇒ Parser
constructor
A new instance of Parser.
- #key ⇒ Object
- #keys ⇒ Object
- #name ⇒ Object
- #resource_associations_by(name, key) ⇒ Object
- #resource_attribute_index ⇒ Object
- #resource_by(name, key) ⇒ Object
- #resources ⇒ Object
- #type_name ⇒ Object
Constructor Details
#initialize(content = {}) ⇒ Parser
Returns a new instance of Parser.
7 8 9 |
# File 'lib/boxxspring/parser.rb', line 7 def initialize( content = {} ) @content = content end |
Instance Method Details
#associations ⇒ Object
33 34 35 36 37 |
# File 'lib/boxxspring/parser.rb', line 33 def associations @content.include?( '$associations' ) ? @content[ '$associations' ] : nil end |
#key ⇒ Object
23 24 25 |
# File 'lib/boxxspring/parser.rb', line 23 def key 'id' end |
#keys ⇒ Object
27 28 29 30 31 |
# File 'lib/boxxspring/parser.rb', line 27 def keys @content.include?( '$this' ) ? @content[ '$this' ][ 'ids' ] : nil end |
#name ⇒ Object
11 12 13 14 15 |
# File 'lib/boxxspring/parser.rb', line 11 def name @content.include?( '$this' ) ? @content[ '$this' ][ 'name' ] : nil end |
#resource_associations_by(name, key) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/boxxspring/parser.rb', line 66 def resource_associations_by( name, key ) result = Hash.new { | hash, key | hash[ key ] = [] } associations = self.associations if associations && associations.include?( name ) associations = associations[ name ].detect do | association | association[ 'id' ] == key end associations.each do | key, value | unless key == 'id' result[ key ] = value.map do | associated_id | self.resource_by( key, associated_id ) end result[ key ].compact! end end end result end |
#resource_attribute_index ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/boxxspring/parser.rb', line 85 def resource_attribute_index @resource_attribute_index ||= begin index = Hash.new { | hash, key | hash[ key ] = {} } @content.each do | key, resources_attributes | unless key[0] == '$' resources_attributes.each do | resource_attributes | index[ key ][ resource_attributes[ 'id' ] ] = resource_attributes end end end index end end |
#resource_by(name, key) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/boxxspring/parser.rb', line 47 def resource_by( name, key ) @resources_index ||= Hash.new { | hash, key | hash[ key ] = {} } @resources_index[ name ][ key ] ||= begin result = nil resource_attributes = resource_attribute_index[ name ][ key ] if resource_attributes.present? type_name = resource_attributes.delete( 'type_name' ) || self.type_name klass = Boxxspring.const_get( type_name.classify ) rescue nil if klass.present? result = klass.new( resource_attributes, self.resource_associations_by( name, key ) ) end end result end end |
#resources ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/boxxspring/parser.rb', line 39 def resources result = nil unless self.name.blank? result = self.keys.map { | key | self.resource_by( name, key ) } end result end |
#type_name ⇒ Object
17 18 19 20 21 |
# File 'lib/boxxspring/parser.rb', line 17 def type_name @content.include?( '$this' ) ? @content[ '$this' ][ 'name' ] : nil end |