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



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

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



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

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

.collection_root_elementObject



64
65
66
# File 'lib/mls/parser.rb', line 64

def self.collection_root_element
  object_class.collection_root_element
end

.extract_attributes(data) ⇒ Object



52
53
54
# File 'lib/mls/parser.rb', line 52

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

.object_classObject



56
57
58
# File 'lib/mls/parser.rb', line 56

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



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

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

.root_elementObject



60
61
62
# File 'lib/mls/parser.rb', line 60

def self.root_element
  object_class.root_element
end

.update(object, data) ⇒ Object



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

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



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

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