Sha256: f984eed0a86b130faa623977cd89540a93d61a9d2b9b98084a9b952c7f802e1e

Contents?: true

Size: 1.33 KB

Versions: 29

Compression:

Stored size: 1.33 KB

Contents

require 'yao/resources/restfully_accessible'
require 'time'

module Yao::Resources
  class Base
    def self.friendly_attributes(*names)
      names.map(&:to_s).each do |name|
        define_method(name) do
          self[name]
        end
      end
    end

    def self.map_attribute_to_attribute(k_and_v)
      as_json, as_method = *k_and_v.to_a.first.map(&:to_s)
      define_method(as_method) do
        self[as_json]
      end
    end

    def self.map_attribute_to_resource(k_and_v)
      _name, klass = *k_and_v.to_a.first
      name = _name.to_s
      define_method(name) do
        self[[name, klass].join("__")] ||= klass.new(self[name])
      end
    end

    def self.map_attribute_to_resources(k_and_v)
      _name, klass = *k_and_v.to_a.first
      name = _name.to_s
      define_method(name) do
        self[[name, klass].join("__")] ||= self[name].map {|d|
          klass.new(d)
        }
      end
    end

    def initialize(data_via_json)
      @data = data_via_json
    end

    def [](name)
      @data[name]
    end

    def []=(name, value)
      @data[name] = value
    end

    def id
      self["id"]
    end

    def created
      if date = self["created"]
        Time.parse(date)
      end
    end

    def updated
      if date = self["updated"]
        Time.parse(date)
      end
    end

    extend RestfullyAccessible
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
yao-0.3.6 lib/yao/resources/base.rb
yao-0.3.5 lib/yao/resources/base.rb
yao-0.3.4 lib/yao/resources/base.rb
yao-0.3.3 lib/yao/resources/base.rb
yao-0.3.2 lib/yao/resources/base.rb
yao-0.3.1 lib/yao/resources/base.rb
yao-0.3.0 lib/yao/resources/base.rb
yao-0.2.13 lib/yao/resources/base.rb
yao-0.2.12 lib/yao/resources/base.rb
yao-0.2.11 lib/yao/resources/base.rb
yao-0.2.10 lib/yao/resources/base.rb
yao-0.2.9 lib/yao/resources/base.rb
yao-0.2.8 lib/yao/resources/base.rb
yao-0.2.7 lib/yao/resources/base.rb
yao-0.2.6 lib/yao/resources/base.rb
yao-0.2.5 lib/yao/resources/base.rb
yao-0.2.4 lib/yao/resources/base.rb
yao-0.2.2 lib/yao/resources/base.rb
yao-0.2.1 lib/yao/resources/base.rb
yao-0.2.0 lib/yao/resources/base.rb