Class: Zephyrus::Base
- Inherits:
-
Object
- Object
- Zephyrus::Base
- Defined in:
- lib/zephyrus/resources/base.rb
Class Method Summary collapse
- .field(name, options = {}) ⇒ Object
- .has_many(name, options = {}) ⇒ Object
- .has_one(name, options = {}) ⇒ Object
- .inherited(subclass) ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}, associations = {}) {|_self| ... } ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(attributes = {}, associations = {}) {|_self| ... } ⇒ Base
Returns a new instance of Base.
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, = {} ) self.fields[ name.to_sym ] = .merge( name: name ) class_eval( "def #{name}(); " + "@#{name}.is_a?( FalseClass ) ? @#{name} : (" + "@#{name} || " + ( [ :default ].nil? ? "nil" : ( [ :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, = {} ) define_method name do self.instance_variable_get( "@_#{name}" ) || [ :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, = {} ) define_method name do associations = self.instance_variable_get( "@_#{name.to_s.pluralize}" ) associations.present? ? associations.first : [ :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 |