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

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

Instance Method Summary collapse

Instance Method Details

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



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

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



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

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

#new_from_hash(data = {}) ⇒ Object



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

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

#primary_keysObject



73
74
75
76
77
# File 'lib/query-interface-client/model.rb', line 73

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

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



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

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|
      self.set_value(name, value)
    end
  end

  if block_given?
    property.instance_eval &definitions
  end

  @properties[name] = property
end