Class: Greentext::Base

Inherits:
Object
  • Object
show all
Includes:
Memoizable
Defined in:
lib/greentext/base.rb

Overview

Internal: The base class for the models.

Direct Known Subclasses

Board, Post

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Base

Internal: Initialize a new Object with given attributes.

attrs - An optional Hash of attributes.

Returns an instance of the Object



53
54
55
# File 'lib/greentext/base.rb', line 53

def initialize(attrs)
  @attrs = attrs
end

Instance Attribute Details

#attrsObject (readonly) Also known as: to_h

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

Class Method Details

.attr_epoch(key, value = key) ⇒ Object

Internal: Dynamically define an date method for a given attribute.

key - A Symbol of the method name. value - An optional Symbol of the attribute key.

Returns nothing.



42
43
44
45
# File 'lib/greentext/base.rb', line 42

def attr_epoch(key, value=key)
  define_method(key) { DateTime.strptime(@attrs[value.to_s].to_s, "%s") }
  memoize(key)
end

.attr_predicate(key, value = key) ⇒ Object

Internal: Dynamically define a predicate method for a given attribute.

key - A Symbol of the method name. value - An optional Symbol of the attribute key.

Returns nothing.



31
32
33
34
# File 'lib/greentext/base.rb', line 31

def attr_predicate(key, value=key)
  define_method(:"#{key}?") { @attrs[value.to_s] == 1 }
  memoize(:"#{key}?")
end

.attr_reader(key, value = key) ⇒ Object

Internal: Dynamically define an attribute method for a given attribute

key - A Symbol of the method name. value - An optional Symbol of the attribute key.

Yields an optional attribute key.

Returns nothing.



20
21
22
23
# File 'lib/greentext/base.rb', line 20

def attr_reader(key, value=key)
  define_method(key) { @attrs[value.to_s] }
  memoize(key)
end