Class: Freeb::ModelConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/freeb/model_config.rb

Class Method Summary collapse

Class Method Details

.get(model) ⇒ Object



10
11
12
13
# File 'lib/freeb/model_config.rb', line 10

def self.get(model)
  key = model_to_key(model)
  @models[key]
end

.get_has_many_properties(model) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/freeb/model_config.rb', line 26

def self.get_has_many_properties(model)
  config = get(model)
  properties = {}
  config[:has_many].each do |key, association|
    association_class = association[:class_name].classify.constantize
    association_class_config = get(association_class)
    association_properties = {:id => nil, :name => nil}
    association_properties.merge!(get_query_properties(association_class))
    key = association[:id]
    properties[key] = [association_properties]
  end
  properties
end

.get_migration_properties(model) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/freeb/model_config.rb', line 40

def self.get_migration_properties(model)
  config = get(model)
  schema = Freeb.mqlread({
    :name => nil,
    :id => config[:type],
    :type => [{:id => "/type/type"}],
    "!/type/property/schema" => [{"/type/property/expected_type" => nil, "id" => nil, "name" => nil}]
  })
  property_types = {}
  schema["!/type/property/schema"].each do |property|
    property_types[property["id"]] = property["/type/property/expected_type"]
  end
  config[:properties].collect do |key, property|
    expected_type = property_types[property[:id]]
    type = case expected_type
    when "/type/boolean"
      :boolean
    when "/type/datetime"
      :datetime
    when "/type/float"
      :float
    when "/type/int"
      :integer
    when "/type/text"
      :text
    else
      :string
    end
    if property[:id] == "description"
      type = "text"
    end
    { :key => key, :type => type }
  end
end

.get_query_properties(model) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/freeb/model_config.rb', line 15

def self.get_query_properties(model)
  config = get(model)
  query_properties = {}
  raise "Empty Freeb config for #{model}" if config.blank?
  query_properties.merge!(config[:properties].inject({}) { |h, (k, property)| h[property[:id]] = nil; h })
  query_properties.delete("description")
  query_properties.merge!(config[:topics].inject({}) { |h, (k, topic)| h[topic[:id]] = [{:id => nil, :name => nil}]; h })
  query_properties.merge!(get_has_many_properties(model))
  query_properties
end

.key_to_id(key, options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/freeb/model_config.rb', line 161

def self.key_to_id(key, options)
  key = key.to_s
  if key == "description"
    key
  elsif key[0,1] == "/"
    key
  else
    "#{options[:type]}/#{key}"
  end
end

.model_to_key(model) ⇒ Object



75
76
77
# File 'lib/freeb/model_config.rb', line 75

def self.model_to_key(model)
  model.name.underscore.to_sym
end

.normalize_has_many(options) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/freeb/model_config.rb', line 136

def self.normalize_has_many(options)
  associations = {}
  options[:has_many] = [options[:has_many]] if !options[:has_many].is_a?(Array)
  options[:has_many].each do |association|
    if association.is_a?(String) || association.is_a?(Symbol)
      association = association.to_s
      associations[association.to_sym] = {
        :key => association.to_sym,
        :id => key_to_id(value, options),
        :class_name => association.singularize.camelize
      }
    elsif association.is_a?(Hash)
      association.each do |key, value|
        key = key.to_s
        associations[key.to_sym] = {
          :key => key,
          :id => key_to_id(value, options),
          :class_name => key.singularize.camelize
        }
      end
    end
  end
  options[:has_many] = associations 
end

.normalize_options(options) ⇒ Object



79
80
81
82
83
84
# File 'lib/freeb/model_config.rb', line 79

def self.normalize_options(options)
  normalize_properties(options)
  normalize_topics(options)
  normalize_has_many(options)
  options
end

.normalize_properties(options) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/freeb/model_config.rb', line 86

def self.normalize_properties(options)
  properties = {}
  options[:properties].flatten(1).each do |property|
    if property.is_a?(String)
      properties[property.to_sym] = {
        :key => property,
        :id => key_to_id(property, options)
      }
    elsif property.is_a?(Hash)
      property.each do |key, value|
        if value.is_a?(String)
          properties[key.to_sym] = {
            :key => value,
            :id => key_to_id(value, options)
          }
        elsif value.is_a?(Hash)
          defaults = {
            :key => key,
            :id => key_to_id(key, options)
          }
          properties[key.to_sym] = defaults.merge(value)
        end
      end
    end 
  end
  options[:properties] = properties
end

.normalize_topics(options) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/freeb/model_config.rb', line 114

def self.normalize_topics(options)
  topics = {}
  options[:topics] = [options[:topics]] if !options[:topics].is_a?(Array)
  options[:topics].each do |topic|
    if topic.is_a?(String) || topic.is_a?(Symbol)
      topic = topic.to_s
      topics[topic.to_sym] = {
        :key => topic,
        :id => key_to_id(topic, options)
      }
    elsif topic.is_a?(Hash)
      topic.each do |key, value|
        topics[key.to_sym] = {
          :key => key,
          :id => key_to_id(value, options)
        }
      end
    end
  end
  options[:topics] = topics 
end

.register(model, options) ⇒ Object



5
6
7
8
# File 'lib/freeb/model_config.rb', line 5

def self.register(model, options)
  key = model_to_key(model)
  @models[key] = normalize_options(options)
end