Module: Datapathy::Model::ClassMethods

Defined in:
lib/datapathy/model.rb

Instance Method Summary collapse

Instance Method Details

#adapterObject



131
132
133
# File 'lib/datapathy/model.rb', line 131

def adapter
  Datapathy.adapter
end

#define_getter_method(name) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/datapathy/model.rb', line 99

def define_getter_method(name)
  class_eval <<-CODE
    def #{name}
      @attributes[:#{name}]
    end
    alias #{name}? #{name}
  CODE
end

#define_setter_method(name) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/datapathy/model.rb', line 108

def define_setter_method(name)
  class_eval <<-CODE
    def #{name}=(val)
      @attributes[:#{name}] = val
    end
  CODE
end

#keyObject



127
128
129
# File 'lib/datapathy/model.rb', line 127

def key
  :href
end

#modelObject



135
136
137
# File 'lib/datapathy/model.rb', line 135

def model
  self
end

#new(*attributes) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/datapathy/model.rb', line 80

def new(*attributes)
  attributes = [{}] if attributes.empty?
  resources = attributes.map do |attrs|
    super(attrs)
  end

  collection = Datapathy::Collection.new(*resources)
  collection.size == 1 ? collection.first : collection
end

#new_from_attributes(attributes = {}) ⇒ Object



120
121
122
123
124
125
# File 'lib/datapathy/model.rb', line 120

def new_from_attributes(attributes = {})
  m = allocate
  m.merge!(attributes = {})
  m.new_record = false
  m
end

#persisted_attributesObject



116
117
118
# File 'lib/datapathy/model.rb', line 116

def persisted_attributes
  @persisted_attributes ||= []
end

#persists(*args) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/datapathy/model.rb', line 90

def persists(*args)
  persisted_attributes.push(*args)
  args.each do |name|
    name = name.to_s.gsub(/\?\Z/, '')
    define_getter_method(name)
    define_setter_method(name)
  end
end