Class: Aegis::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/aegis/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, type, options) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
13
# File 'lib/aegis/resource.rb', line 6

def initialize(parent, name, type, options)
  @parent = parent
  @children = []
  @name = name
  @type = type
  @actions = initial_actions(options)
  # @never_takes_object = options[:object] == false
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



4
5
6
# File 'lib/aegis/resource.rb', line 4

def actions
  @actions
end

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/aegis/resource.rb', line 4

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/aegis/resource.rb', line 4

def name
  @name
end

#never_takes_objectObject (readonly)

Returns the value of attribute never_takes_object.



4
5
6
# File 'lib/aegis/resource.rb', line 4

def never_takes_object
  @never_takes_object
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/aegis/resource.rb', line 4

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/aegis/resource.rb', line 4

def type
  @type
end

Instance Method Details

#action_names_with_aliases(native_name, aliases) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/aegis/resource.rb', line 69

def action_names_with_aliases(native_name, aliases)
  names = [native_name]
  aliases.each do |key, value|
    names << key if value == native_name
  end
  names
end

#action_paths(action, aliases) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aegis/resource.rb', line 55

def action_paths(action, aliases)
  if root?
    action_names_with_aliases(action.name, aliases)
  else
    action_names_with_aliases(action.name, aliases).collect do |action_name|
      build_path(
        action_name,
        parent && parent.path(false),
        action.pluralize_resource ? name.pluralize : name.singularize
      )
    end
  end
end

#collection?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/aegis/resource.rb', line 43

def collection?
  type == :collection
end

#create_or_update_action(name, create_options, update_options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/aegis/resource.rb', line 24

def create_or_update_action(name, create_options, update_options)
  action = nil
  if action = find_action_by_name(name)
    action.update(update_options)
  else
    action = Action.new(name, create_options)
    @actions << action
  end
  action
end

#find_action_by_name(name) ⇒ Object



19
20
21
22
# File 'lib/aegis/resource.rb', line 19

def find_action_by_name(name)
  name = name.to_s
  @actions.detect { |action| action.name == name }
end

#index_actions_by_path(aliases) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/aegis/resource.rb', line 77

def index_actions_by_path(aliases)
  index = {}
  actions.each do |action|
    action_paths(action, aliases).each do |path|
      index[path] = action
    end
  end
  children.each do |child|
    index.merge! child.index_actions_by_path(aliases)
  end
  index
end

#inspectObject



15
16
17
# File 'lib/aegis/resource.rb', line 15

def inspect
  "Resource(#{{:name => name || 'root', :actions => actions, :children => children, :parent => parent}.inspect})"      
end

#new_action_takes_object?(action_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/aegis/resource.rb', line 90

def new_action_takes_object?(action_options = {})
  collection? && action_options[:collection] != true # && !never_takes_object
end

#new_action_takes_parent_object?(action_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/aegis/resource.rb', line 94

def new_action_takes_parent_object?(action_options = {})
  parent && parent.collection? # && !parent.never_takes_object
end

#reading_actionsObject



47
48
49
# File 'lib/aegis/resource.rb', line 47

def reading_actions
  actions.reject(&:writing)
end

#root?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/aegis/resource.rb', line 35

def root?
  type == :root
end

#singleton?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/aegis/resource.rb', line 39

def singleton?
  type == :singleton
end

#writing_actionsObject



51
52
53
# File 'lib/aegis/resource.rb', line 51

def writing_actions
  actions.select(&:writing)
end