Class: ProtoJ::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/proto_j/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = {}) ⇒ Base

Returns a new instance of Base.



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
85
86
# File 'lib/proto_j/base.rb', line 60

def initialize(json={})
  if json.is_a?(Hash)
    hash = json
  else
    hash = JSON.parse(json) rescue {}
  end

  self.class.fields.each do |f|
    key = f[:key]
    instance_variable_set("@#{key}", hash[key.to_s])
  end

  self.class.associations.each do |a|
    key = a[:key]

    case a[:type].to_sym
    when :has_one
      instance_variable_set("@#{key}", a[:class].new(hash[key.to_s]))
    when :has_many
      instance_variable_set("@#{key}", Associations::HasManyAssociation.new(a[:class]))

      (hash[key.to_s] || []).each do |item|
        instance_variable_get("@#{key}").reader << a[:class].new(item)
      end
    end
  end
end

Class Method Details

.associationsObject



14
15
16
17
18
19
20
21
22
# File 'lib/proto_j/base.rb', line 14

def associations
  begin
    return class_variable_get("@@associations")
  rescue
    var = []
    class_variable_set("@@associations", var)
    return var
  end
end

.field(key, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/proto_j/base.rb', line 24

def field(key, options={})
  fields << { key: key, options: options }

  define_method key do
    normalize_value(instance_variable_get("@#{key}"), options)
  end
  define_method "#{key}=" do |val|
    val = normalize_value(val, options)
    instance_variable_set("@#{key}", val)
  end
end

.fieldsObject



4
5
6
7
8
9
10
11
12
# File 'lib/proto_j/base.rb', line 4

def fields
  begin
    return class_variable_get("@@fields")
  rescue
    var = []
    class_variable_set("@@fields", var)
    return var
  end
end

.has_many(key, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/proto_j/base.rb', line 50

def has_many(key, options={})
  options[:class] ||= "#{self.name}::#{key.to_s.singularize.capitalize}".constantize
  associations << { key: key, options: options, type: :has_many, class: options[:class] }

  define_method key do
    instance_variable_get("@#{key}").reader
  end
end

.has_one(key, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/proto_j/base.rb', line 36

def has_one(key, options={})
  options[:class] ||= "#{self.name}::#{key.to_s.capitalize}".constantize
  associations << { key: key, options: options, type: :has_one, class: options[:class] }

  define_method key do
    instance_variable_get("@#{key}")
  end
  define_method "#{key}=" do |val|
    # TODO change to association exception class way
    raise 'Class Mismatch' unless val.is_a?(options[:class])
    instance_variable_set("@#{key}", val)
  end
end

Instance Method Details

#to_hashObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/proto_j/base.rb', line 115

def to_hash
  hash = {}

  self.class.fields.each do |f|
    hash[f[:key].to_s] = ::ProtoJ::Utils.to_sorted_hash(self.send(f[:key]))
  end

  self.class.associations.each do |a|
    hash[a[:key].to_s] = self.send(a[:key]).to_hash
  end

  Hash[hash.sort]
end

#to_jsonObject



111
112
113
# File 'lib/proto_j/base.rb', line 111

def to_json
  to_hash.to_json
end

#update(json = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/proto_j/base.rb', line 88

def update(json={})
  if json.is_a?(Hash)
    hash = json
  else
    hash = JSON.parse(json) rescue {}
  end

  self.class.fields.each do |f|
    key = f[:key].to_s
    if hash.has_key?(key)
      self.send("#{key}=", hash[key])
    end
  end

  self.class.associations.each do |a|
    key = a[:key].to_s

    if hash.has_key?(key)
      self.send(key).update(hash[key])
    end
  end
end