Module: Kvbean::Base::ClassMethods

Defined in:
lib/kvbean/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/kvbean/base.rb', line 101

def method_missing(method_symbol, *args)
  method_name = method_symbol.to_s

  if method_name =~ /^find_by_(\w+)!/
    send("find_by_#{$1}", *args) || raise(UnknownRecord)
  elsif method_name =~ /^find_by_(\w+)/
    find_by_attribute($1, args.first)
  elsif method_name =~ /^find_or_create_by_(\w+)/
    send("find_by_#{$1}", *args) || create($1 => args.first)
  elsif method_name =~ /^find_all_by_(\w+)/
    find_all_by_attribute($1, args.first)
  else
    super
  end
end

Instance Attribute Details

#kv_fieldsObject

Returns the value of attribute kv_fields.



6
7
8
# File 'lib/kvbean/base.rb', line 6

def kv_fields
  @kv_fields
end

#primary_keyObject

Returns the value of attribute primary_key.



6
7
8
# File 'lib/kvbean/base.rb', line 6

def primary_key
  @primary_key
end

Instance Method Details

#allObject



59
60
61
62
63
# File 'lib/kvbean/base.rb', line 59

def all
  clear_all
  super.each { |item| records[item.id] = item.dup }
  collection.new(records.values.deep_dup)
end

#clear_allObject



78
79
80
# File 'lib/kvbean/base.rb', line 78

def clear_all
  records.clear
end

#collection(&block) ⇒ Object



26
27
28
29
30
# File 'lib/kvbean/base.rb', line 26

def collection(&block)
  @collection ||= Class.new(Array)
  @collection.class_eval(&block) if block_given?
  @collection
end

#countObject



65
66
67
# File 'lib/kvbean/base.rb', line 65

def count
  all.size
end

#create(attrs = {}) ⇒ Object



82
83
84
85
# File 'lib/kvbean/base.rb', line 82

def create(attrs = {})
  record = self.new(attrs)
  record.save && record
end

#create!(attrs = {}) ⇒ Object



87
88
89
# File 'lib/kvbean/base.rb', line 87

def create!(attrs = {})
  create(*attrs) || raise(Kvbean::InvalidRecord)
end

#destroy_allObject



73
74
75
76
# File 'lib/kvbean/base.rb', line 73

def destroy_all
  all.each { |item| item.destroy }
  clear_all
end

#find(id) ⇒ Object



44
45
46
47
# File 'lib/kvbean/base.rb', line 44

def find(id)
  item = raw_find(id)
  item && item.dup
end

#find_all_by_attribute(name, value) ⇒ Object



96
97
98
99
# File 'lib/kvbean/base.rb', line 96

def find_all_by_attribute(name, value)
  items = records.values.select {|r| r.send(name) == value }
  collection.new(items.deep_dup)
end

#find_by_attribute(name, value) ⇒ Object



91
92
93
94
# File 'lib/kvbean/base.rb', line 91

def find_by_attribute(name, value)
  item = records.values.find {|r| r.send(name) == value }
  item && item.dup
end

#firstObject



49
50
51
52
# File 'lib/kvbean/base.rb', line 49

def first
  item = records.values[0]
  item && item.dup
end

#kv_field(*attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kvbean/base.rb', line 8

def kv_field(*attributes)
  define_attribute_methods attributes
  @kv_fields ||= []
  attributes.each do |attribute|
    @kv_fields << attribute
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def #{attribute}
        @#{attribute}
      end

      def #{attribute}=(val)
        #{attribute}_will_change! unless val == @#{attribute}
        @#{attribute} = val
      end
    EOS
  end
end

#lastObject



54
55
56
57
# File 'lib/kvbean/base.rb', line 54

def last
  item = records.values[-1]
  item && item.dup
end

#raw_find(id) ⇒ Object



40
41
42
# File 'lib/kvbean/base.rb', line 40

def raw_find(id)
  records[id] || raise(UnknownRecord, "Couldn't find #{self.name} with ID=#{id}")
end

#recordsObject



36
37
38
# File 'lib/kvbean/base.rb', line 36

def records
  @records ||= {}
end

#update(id, attrs) ⇒ Object



69
70
71
# File 'lib/kvbean/base.rb', line 69

def update(id, attrs)
  find(id).update_attributes(attrs)
end