Class: Zephyrus::Base

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

Direct Known Subclasses

Error, Input, Output

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, associations = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



54
55
56
57
58
59
60
61
62
63
# File 'lib/zephyrus/resources/base.rb', line 54

def initialize( attributes = {}, associations = {} )
  self.type_name = self.class.name.gsub( /Zephyrus::/, '' ).underscore
  attributes.each do | key, value |
    send( "#{key}=", value ) if respond_to?( "#{key}=" )
  end
  associations.each do | key, value |
    self.instance_variable_set( "@_#{key}", value )
  end
  yield self if block_given?
end

Class Method Details

.field(name, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zephyrus/resources/base.rb', line 11

def field( name, options = {} )
  
  self.fields[ name.to_sym ] = options.merge( name: name )
  
  class_eval(
    "def #{name}(); " +
    "@#{name}.is_a?( FalseClass ) ? @#{name} : (" +
       "@#{name} || " +
         ( options[ :default ].nil? ?
            "nil" :
            ( options[ :default ].is_a?( String ) ? 
                "'#{options[ :default ]}'" : 
                  "#{options[ :default ]}" ) ) + ");" +
    "end;" +
    " " +
    "attr_writer :#{name};",
    __FILE__, 
    __LINE__
  )

end

.has_many(name, options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/zephyrus/resources/base.rb', line 40

def has_many( name, options = {} )  
  define_method name do 
    self.instance_variable_get( "@_#{name}" ) || options[ :default ] || []
  end
end

.has_one(name, options = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/zephyrus/resources/base.rb', line 33

def has_one( name, options = {} )
  define_method name do 
    associations = self.instance_variable_get( "@_#{name.to_s.pluralize}" )
    associations.present? ? associations.first : options[ :default ]
  end
end

.inherited(subclass) ⇒ Object



7
8
9
# File 'lib/zephyrus/resources/base.rb', line 7

def inherited( subclass )
  subclass.fields = {}.merge( self.fields )
end