Module: QueryInterface::Client::Model::ClassMethods

Defined in:
lib/query-interface-client/model.rb

Instance Method Summary collapse

Instance Method Details

#get(path, params = {}) ⇒ Object



89
90
91
92
# File 'lib/query-interface-client/model.rb', line 89

def get(path, params={})
  data = self.get_raw(path: path, params: params)[:parsed_data][:data]
  self.new_from_hash(data)
end

#new_collection(collection) ⇒ Object



83
84
85
86
87
# File 'lib/query-interface-client/model.rb', line 83

def new_collection(collection)
  collection[:data].map do |data|
    self.new_from_hash(data)
  end
end

#new_from_hash(data = {}) ⇒ Object



94
95
96
97
98
# File 'lib/query-interface-client/model.rb', line 94

def new_from_hash(data={})
  instance = self.new
  instance.assign_attributes(data)
  instance
end

#primary_keysObject



77
78
79
80
81
# File 'lib/query-interface-client/model.rb', line 77

def primary_keys
  @properties.map do |name, property|
    name if property.primary
  end.compact
end

#property(name, type, options = {}, &definitions) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/query-interface-client/model.rb', line 52

def property(name, type, options={}, &definitions)
  property = QueryInterface::Client::Property.new(name, type, options)

  define_method name do
    self.get_value(name)
  end

  unless property.primary || !property.update
    define_method "#{name}=".to_sym do |value|
      if self.get_value(name) != value
        self.property_changes << name unless self.property_changes.include?(name)
        self.set_value(name, value)
      else
        value
      end
    end
  end

  if block_given?
    property.instance_eval &definitions
  end

  @properties[name] = property
end