Class: MLS::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/mls/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil, persisted = true) ⇒ Parser

Returns a new instance of Parser.



5
6
7
# File 'lib/mls/parser.rb', line 5

def initialize(object=nil, persisted=true)
  @object = object || self.class.object_class.new({}, persisted)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



50
51
52
# File 'lib/mls/parser.rb', line 50

def method_missing(method, *args, &block)
  object.__send__(method, *args, &block) if object.methods.include?(method)
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/mls/parser.rb', line 3

def object
  @object
end

Class Method Details

.build(attributes) ⇒ Object



42
43
44
# File 'lib/mls/parser.rb', line 42

def self.build(attributes)
  self.new.build(attributes)
end

.build_collection(hash, options = {}) ⇒ Object

TODO: TEST ME



29
30
31
32
33
34
35
36
# File 'lib/mls/parser.rb', line 29

def self.build_collection(hash, options={})
  root = options[:collection_root_element] || collection_root_element
  collection = []
  hash[root].each do |attrs|
    collection << build(attrs)
  end
  collection
end

.collection_root_elementObject



70
71
72
# File 'lib/mls/parser.rb', line 70

def self.collection_root_element
  object_class.collection_root_element
end

.extract_attributes(data) ⇒ Object



58
59
60
# File 'lib/mls/parser.rb', line 58

def self.extract_attributes(data)
  Yajl::Parser.new(:symbolize_keys => true).parse(data)
end

.object_classObject



62
63
64
# File 'lib/mls/parser.rb', line 62

def self.object_class
  @object_class ||= ActiveSupport::Inflector.constantize(self.to_s.sub('::Parser',''))
end

.parse(data) ⇒ Object



24
25
26
# File 'lib/mls/parser.rb', line 24

def self.parse(data)
  self.new.parse(data)
end

.parse_collection(data, options = {}) ⇒ Object



38
39
40
# File 'lib/mls/parser.rb', line 38

def self.parse_collection(data, options={})
  build_collection(extract_attributes(data), options)
end

.root_elementObject



66
67
68
# File 'lib/mls/parser.rb', line 66

def self.root_element
  object_class.root_element
end

.update(object, data) ⇒ Object



46
47
48
# File 'lib/mls/parser.rb', line 46

def self.update(object, data)
  self.new(object).parse(data)    
end

Instance Method Details

#build(attributes) ⇒ Object



15
16
17
18
# File 'lib/mls/parser.rb', line 15

def build(attributes)
  update_attributes(attributes)
  object
end

#extract_attributes(data) ⇒ Object



54
55
56
# File 'lib/mls/parser.rb', line 54

def extract_attributes(data)
  Yajl::Parser.new(:symbolize_keys => true).parse(data)
end

#parse(data) ⇒ Object



9
10
11
12
13
# File 'lib/mls/parser.rb', line 9

def parse(data)
  attributes = extract_attributes(data)[self.class.root_element]
  update_attributes(attributes)
  object
end

#update_attributes(attributes) ⇒ Object



20
21
22
# File 'lib/mls/parser.rb', line 20

def update_attributes(attributes)
  attributes.each {|k,v| self.send("#{k}=".to_sym, v) } if attributes
end