Module: Jason::Persistence::ClassMethods

Defined in:
lib/jason/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_name, *args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jason/persistence.rb', line 27

def method_missing(meth_name,*args,&block)
  if meth_name.to_s.match(/find_by_/)
    conditions = meth_name.to_s.split("find_by_").last
    if defined_attributes_include?(conditions.to_sym)
      options = {:klass => self}
      options[conditions.to_sym] = args.pop
      return Encoding::PersistenceHandler::restore(:with_conditions, options)
    else
      super
    end
  else
    super
  end
end

Instance Attribute Details

#new_recordObject

Returns the value of attribute new_record.



16
17
18
# File 'lib/jason/persistence.rb', line 16

def new_record
  @new_record
end

Instance Method Details

#allObject



61
62
63
# File 'lib/jason/persistence.rb', line 61

def all
  Encoding::PersistenceHandler::restore(:all,{:klass => self})
end

#attribute(*args) ⇒ Object

PUBLIC Add attribute to persistence layer for this model.

Defines getter and setter methods for given attribute

The Setter methods converts the attribute into the given data type.

args - List of arguments:

* first argument   - attribute name as symbol
* second argument  - data type of attribute

Currently three data types are supported:

  • String

  • Integer

  • Date



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jason/persistence.rb', line 87

def attribute(*args)
  attribute_name,attribute_type = args[0], args[1]

  unless DATA_TYPES.keys.include?("#{attribute_type}".to_sym)
    raise Errors::NotSupportedDataTypeError.new("This Kind of type is not supported or missing!") 
  end

  cast_to = DATA_TYPES["#{attribute_type}".to_sym]#eval "Jason::#{attribute_type}"

  define_method attribute_name do 
    instance_variable_get("@#{attribute_name}")
  end

  define_method "#{attribute_name}=" do |attribute|
    instance_variable_set("@#{attribute_name}", attribute.send(cast_to))
  end

  defined_attributes << {:name => attribute_name, :type => attribute_type} unless defined_attributes.include?(attribute_name)

  unless defined_attributes_include?(:id)
    define_method :id do 
      instance_variable_get("@id")
    end

    define_method "id=" do |val|
      instance_variable_set("@id", val)
    end
    defined_attributes << {:name => :id, :type => String}
  end
end

#data_type_for_attribute(attribute) ⇒ Object



46
47
48
# File 'lib/jason/persistence.rb', line 46

def data_type_for_attribute(attribute)
  defined_attributes.detect{|item| item[:name] == attribute}[:attribute_type]
end

#defined_attributesObject



50
51
52
# File 'lib/jason/persistence.rb', line 50

def defined_attributes
  @defined_attributes ||= []
end

#defined_attributes_include?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/jason/persistence.rb', line 42

def defined_attributes_include?(symbol)
  defined_attributes.map{|item| item[:name]}.include?(symbol)
end

#find(id) ⇒ Object



54
55
56
57
58
59
# File 'lib/jason/persistence.rb', line 54

def find(id)
  #with_id id do
  #  klass_to_restore self
  #end
  Encoding::PersistenceHandler::restore(:by_id,{:id => id,:klass=>self})
end

#with_id(id, &block) ⇒ Object



65
66
67
68
# File 'lib/jason/persistence.rb', line 65

def with_id(id,&block)
  p id 
  block.call
end