Class: RestModel::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_model/key.rb,
lib/rest_model/key/builder.rb

Direct Known Subclasses

Association, Href, Property

Defined Under Namespace

Modules: Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Key

Returns a new instance of Key.



5
6
7
8
# File 'lib/rest_model/key.rb', line 5

def initialize(name, options = {})
  @name    = name
  @options = options
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/rest_model/key.rb', line 3

def model
  @model
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/rest_model/key.rb', line 3

def name
  @name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/rest_model/key.rb', line 3

def options
  @options
end

#summarizeObject

Returns the value of attribute summarize.



3
4
5
# File 'lib/rest_model/key.rb', line 3

def summarize
  @summarize
end

Instance Method Details

#convert_input_keysObject



10
11
12
13
# File 'lib/rest_model/key.rb', line 10

def convert_input_keys
  options.fetch(:convert_input_keys, model.try(:convert_input_keys) ||
                RestModel::Configuration.convert_input_keys)
end

#present?(resource) ⇒ Boolean

Returns:



32
33
34
# File 'lib/rest_model/key.rb', line 32

def present?(resource)
  !options[:if] ? true : resource.instance_eval(&options[:if])
end

#relation?Boolean

Returns:



44
45
46
# File 'lib/rest_model/key.rb', line 44

def relation?
  false
end

#root_path?Boolean

Returns:



27
28
29
30
# File 'lib/rest_model/key.rb', line 27

def root_path?
  path_definition = options.slice(:start_key, :field)
  path_definition.any? and path_definition.first[1].empty?
end

#source_pathObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rest_model/key.rb', line 15

def source_path
  return @source_path.clone if @source_path

  @source_path = Source::Path.resolve(options, convert_input_keys)

  if @source_path.empty? and !root_path?
    @source_path = convert_input_keys.call([name])
  end

  @source_path.clone
end

#visible?(resource) ⇒ Boolean

Returns:



36
37
38
39
40
41
42
# File 'lib/rest_model/key.rb', line 36

def visible?(resource)
  !present?(resource) ? false : case visible = options[:visible]
                                when nil  then true
                                when Proc then resource.instance_eval(&visible)
                                else visible
                                end
end