Class: Fantasydata::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fantasydata/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Fantasydata::Base

Initializes a new object

Parameters:

  • attrs (Hash) (defaults to: {})


33
34
35
36
37
38
39
40
# File 'lib/fantasydata/base.rb', line 33

def initialize(attrs={})
  new_attrs = {}
  attrs = {} if attrs.nil?
  attrs.to_hash.each_pair do |k,v|
    new_attrs.merge!({underscore_key(k) => v})
  end
  @attrs = new_attrs
end

Class Method Details

.attr_reader(*attrs) ⇒ Object

Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key

Parameters:

  • attrs (Array, Set, Symbol)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fantasydata/base.rb', line 6

def self.attr_reader(*attrs)
  #@attr_readers ||= []
  #@attr_readers.concat attrs
  mod = Module.new do
    attrs.each do |attribute|
      define_method attribute do
        @attrs[attribute.to_sym] if @attrs
      end
      define_method "#{attribute}?" do
        !!@attrs[attribute.to_sym]
      end
    end
  end
  const_set(:Attributes, mod)
  include mod
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


25
26
27
# File 'lib/fantasydata/base.rb', line 25

def ==(other)
  super || attr_equal(:id, other) || attrs_equal(other)
end

#attrsHash Also known as: to_hash

Retrieve the attributes of an object

Returns:

  • (Hash)


45
46
47
# File 'lib/fantasydata/base.rb', line 45

def attrs
  @attrs
end

#update(attrs) ⇒ Fantasydata::Base

Update the attributes of an object

Parameters:

  • attrs (Hash)

Returns:



54
55
56
57
# File 'lib/fantasydata/base.rb', line 54

def update(attrs)
  @attrs.update(attrs)
  self
end