Class: MiniModel

Inherits:
Object
  • Object
show all
Extended by:
AssociationClassMethods, ClassMethods
Defined in:
lib/minimodel.rb,
lib/minimodel/associations.rb

Defined Under Namespace

Modules: AssociationClassMethods, ClassMethods Classes: DuplicateKeyError, HasManyAssociation

Instance Method Summary collapse

Methods included from ClassMethods

all, count, find, indexed_by, insert, keys, load_from, options, primary_key

Methods included from AssociationClassMethods

belongs_to, has_many

Constructor Details

#initialize(attributes) ⇒ MiniModel

Returns a new instance of MiniModel.



4
5
6
# File 'lib/minimodel.rb', line 4

def initialize(attributes)
  @attributes = attributes.to_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/minimodel.rb', line 28

def method_missing(symbol, *args, &block)
  if @attributes.key?(symbol) && args.empty? && block.nil?
    return @attributes[symbol]
  else
    super symbol, *args, &block
  end
end

Instance Method Details

#eql?(object) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/minimodel.rb', line 12

def eql?(object)
  object.class == self.class && object.hash == self.hash
end

#hashObject



8
9
10
# File 'lib/minimodel.rb', line 8

def hash
  @attributes.hash
end

#read_attribute(name) ⇒ Object



24
25
26
# File 'lib/minimodel.rb', line 24

def read_attribute(name)
  @attributes[name]
end

#respond_to_missing?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/minimodel.rb', line 36

def respond_to_missing?(symbol, include_private = false)
  @attributes.key?(symbol)
end

#to_hashObject



16
17
18
# File 'lib/minimodel.rb', line 16

def to_hash
  @attributes
end

#to_jsonObject



20
21
22
# File 'lib/minimodel.rb', line 20

def to_json
  @attributes.to_json
end