Class: Ice::BaseCube

Inherits:
Object show all
Extended by:
CubeAssociation
Defined in:
lib/ice/base_cube.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CubeAssociation

belongs_to, has_many

Constructor Details

#initialize(source) ⇒ BaseCube

Returns a new instance of BaseCube.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ice/base_cube.rb', line 49

def initialize(source)
  @source = source
  
  unless self.class.attribute_names.count > 0
    to_hash.each_key do |key|
      unless self.respond_to? key.to_sym
        self.class.send :define_method, key.to_sym do
          @source.send(key.to_sym)
        end
      end
    end
  end
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/ice/base_cube.rb', line 4

def source
  @source
end

Class Method Details

.attribute_namesObject



14
15
16
# File 'lib/ice/base_cube.rb', line 14

def self.attribute_names
  @@attribute_names ||= []
end

.revealing(*attributes) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/ice/base_cube.rb', line 18

def self.revealing(* attributes)
  attribute_names.concat(attributes)

  attributes.each do |attr|
    define_method attr.to_sym do
      @source.send(attr).to_ice
    end
  end
end

Instance Method Details

#idObject



10
11
12
# File 'lib/ice/base_cube.rb', line 10

def id
  @source.id
end

#to_hashObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ice/base_cube.rb', line 29

def to_hash
  if self.class.attribute_names.count > 0
    hash = {}
    ([:id, :created_at, :updated_at] +
        self.class.attribute_names).each do |method|
      if @source.respond_to? method
        hash[method] = source.send(method)
      end
    end
    hash
  else
    @hash ||= @source.serializable_hash.to_ice
  end
end

#to_iceObject



6
7
8
# File 'lib/ice/base_cube.rb', line 6

def to_ice
  self
end

#to_jsonObject



44
45
46
# File 'lib/ice/base_cube.rb', line 44

def to_json
  to_hash.to_json
end