Module: BSNS

Included in:
Base
Defined in:
lib/bsns.rb

Defined Under Namespace

Classes: Base

Constant Summary collapse

@@config =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(opts) ⇒ Object



43
44
45
# File 'lib/bsns.rb', line 43

def self.configure opts
  @@config = opts
end

.get_config(key) ⇒ Object



47
48
49
# File 'lib/bsns.rb', line 47

def self.get_config key
  @@config[key.to_sym]
end

Instance Method Details

#acts_as_collectionObject



39
40
41
# File 'lib/bsns.rb', line 39

def acts_as_collection
  set_v :single_file, true
end

#has_many(model, opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bsns.rb', line 7

def has_many model, opts = {}
  field = opts[:as] || model.to_s.downcase.pluralize
  with  = opts[:with] || nil
  embed = opts[:embedded] || false
  model = model.to_s.singularize
  class_eval do 
    define_method field do
      collection_from_array instance_variable_get("@#{field}"), model, opts
    end
  end
end

#has_one(model, opts = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bsns.rb', line 19

def has_one model, opts = {}
  model = model.to_s
  field = opts[:as] || model.downcase
  class_eval do
    define_method field do
      klass = model.capitalize.constantize
      data  = instance_variable_get "@#{field}"
      if opts[:embedded]
        obj = klass.new data
      else
        if data.is_a? Hash
          obj = klass.load data[0]
        else
          obj = klass.load data
        end
      end
    end
  end
end