Class: Resty::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/resty/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Attributes

Returns a new instance of Attributes.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/resty/attributes.rb', line 5

def initialize(data)
  @href = data[':href']
  @fully_populated = if @href
                       !data[':partial'] && data.length > 1
                     else
                       true
                     end

  @data = data
  @wrapped = {}
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



3
4
5
# File 'lib/resty/attributes.rb', line 3

def href
  @href
end

Instance Method Details

#[](name) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/resty/attributes.rb', line 27

def [](name)
  until_populated do
    key_variants(name) do |key|
      return wrap_data(key)
    end
  end

  nil
end

#actionsObject



65
66
67
68
69
70
71
72
# File 'lib/resty/attributes.rb', line 65

def actions
  unless @actions
    populate! unless populated?
    @actions = Resty::Actions.new(@data[':actions'])
  end
    
  @actions
end

#itemsObject



37
38
39
40
# File 'lib/resty/attributes.rb', line 37

def items
  populate! unless populated?
  @wrapped[':items'] ||= (@data[':items'] || []).map { |item| Resty.wrap(item) }
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
# File 'lib/resty/attributes.rb', line 17

def key?(name)
  until_populated do
    key_variants(name) do |key|
      return true
    end
  end

  false
end

#populate!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/resty/attributes.rb', line 46

def populate!
  new_data = Resty::Transport.request_json(@href)

  @data = case new_data
          when Array
            { ':href' => @href, ':items' => new_data }
          else
            new_data
          end
          
  @fully_populated = true
  @wrapped = {}
end

#populated?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/resty/attributes.rb', line 42

def populated?(key = nil)
  @fully_populated
end

#populated_dataObject



60
61
62
63
# File 'lib/resty/attributes.rb', line 60

def populated_data
  populate! unless populated?
  @data
end