Module: Top4R::ModelMixin::ClassMethods

Defined in:
lib/top4r/model.rb

Instance Method Summary collapse

Instance Method Details

#default_private_fieldsObject



6
# File 'lib/top4r/model.rb', line 6

def default_private_fields; []; end

#default_public_fieldsObject



4
# File 'lib/top4r/model.rb', line 4

def default_public_fields; []; end

#fieldsObject



29
30
31
# File 'lib/top4r/model.rb', line 29

def fields
  (self.default_public_fields + self.default_private_fields).uniq.join(',')
end

#unmarshal(raw) ⇒ Object

Unmarshal object singular or plural array of model objects from JSON serialization. Currently JSON is only supported.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/top4r/model.rb', line 10

def unmarshal(raw)
  # input = JSON.parse(raw)
  input = raw
  # puts "\ninput: #{input.inspect}"
  def unmarshal_model(hash)
    mine = self.new(hash)
    # puts "\n mine: #{mine.inspect}"
    mine.unmarshal_other_attrs if mine.respond_to? :unmarshal_other_attrs
    mine
  end
  return unmarshal_model(input) if input.is_a?(Hash) # singular case
  result = []
  input.each do |hash|
    model = unmarshal_model(hash) if hash.is_a?(Hash)
    result << model
  end if input.is_a?(Array)
  result # plural case
end

#unmarshal_model(hash) ⇒ Object

puts “ninput: #Top4R::ModelMixin::ClassMethods.inputinput.inspect”



14
15
16
17
18
19
# File 'lib/top4r/model.rb', line 14

def unmarshal_model(hash)
  mine = self.new(hash)
  # puts "\n mine: #{mine.inspect}"
  mine.unmarshal_other_attrs if mine.respond_to? :unmarshal_other_attrs
  mine
end