public
Fork of stevenbristol/lovd-by-less
Description: Integrate Ankoder Video Professional Service with lovd-by-less
Homepage: http://www.ankoder.com
Clone URL: git://github.com/janx/lovd-by-less.git
100644 119 lines (108 sloc) 3.766 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
class String
  # convert "undercore_string" in "UnderscoreString"
  #
  # Method from Rails
  def camelize
    self.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  end
end
 
module Ankoder
  module CoreExtension
    module HashExtension
      # Replace "-" by "_" in all keys of the hash
      def underscore_keys!
        self.keys.each do |k|
          self[k] = self[k].underscore_keys! if self[k].is_a?(Hash)
          self[k.to_s.gsub("-", "_")] = self[k]
          delete(k) if k.to_s =~ /-/
        end
        self
      end
      
      # Cast each value of the hash in the right format (integer, float, time, boolean)
      def type_cast!
        self.keys.each do |k|
          self[k] = self[k].type_cast! if self[k].is_a?(Hash)
          self[k] = self[k].to_i if self[k] =~ /^[0-9]+$/
          self[k] = self[k].to_f if self[k] =~ /^[0-9]+\.[0-9]+$/
          self[k] = Time.parse(self[k]) if self[k] =~ /[a-z]+ [a-z]+ [0-9]+ [0-9]{2}:[0-9]{2}:[0-9]{2} [a-z0-9\+]+ [0-9]{4}/i
          self[k] = true if self[k] == "true"
          self[k] = false if self[k] == "false"
          self[k] = nil if self[k] == {}
        end
        self
      end
      
      # The hash keys are available by calling the method name "key"
      #
      # h = {"profile_id" => 54, "video_id" => 12}
      # h.profile_id
      # => 54
      def method_missing(m, *args)
        if has_key? m.to_s
          self[m.to_s]
        else
          raise NoMethodError, "undefined method `#{m.to_s}' for #{self.class}"
        end
      end
    end
 
 
    module ArrayExtension
      def find_with_options(options={})
        collection = self
        if options[:conditions]
          options[:conditions].each_pair do |k,v|
            collection = collection.find_with_conditions(k => v)
          end
        end
        collection.limit(options[:limit]).order(options[:order]).include_ankoder_object(options[:include])
      end
      
      
      # Select Ankoder#Base object containing the conditions
      #
      # Profile.find(:all).find_with_conditions(:name => "bar")
      def find_with_conditions(conditions={})
        return self if conditions.nil? or conditions.empty?
        res = []
        self.each do |object|
          object.attributes.each_pair do |k,v|
            if conditions[k.to_sym]
              condition = []
              if not conditions[k.to_sym].is_a?(Regexp) and conditions[k.to_sym].to_s =~ /([<|>|<=|>=|==]+)[\s]*([0-9]+)$/
                conditions[k.to_sym].to_s.split(" and ").each do |c|
                  c.match(/([<|>|<=|>=|==]+)[\s]*([0-9]+)$/)
                  condition << "#{v} #{$1} #{$2}"
                end
                res << object if eval(condition.join(" and "))
              elsif conditions[k.to_sym].is_a?(Regexp)
                res << object if v =~ conditions[k.to_sym]
              else
                res << object if v == conditions[k.to_sym]
              end
            end
          end
        end
        return res
      end
 
      def limit(lim=nil)
        return self if lim.nil?
        self[0..lim-1]
      end
 
      def order(order_str=nil)
        return self if order_str.nil?
        field, sort = order_str.match(/([a-z0-9_]+)([\s+]*[ASC|DESC]*)/i).to_a[1..-1]
        sort = "ASC" if sort.nil? or sort.empty?
        begin
          self.sort do |x,y|
            case sort
            when /ASC/i then x.send(field) <=> y.send(field)
            when /DESC/i then y.send(field) <=> x.send(field)
            end
          end
        rescue
          self
        end
      end
      
      def include_ankoder_object(inc=nil)
        return self if inc.nil?
        self.each {|object| object.include_ankoder_object(inc)}
      end
    end
  end
end