Class: Users::Attribute

Inherits:
Object show all
Defined in:
lib/users/attribute.rb

Overview

Attributes belong to users and reference a specific class of capabilities which the user may level up. Attribute types have requirements which the user must satisfy to unlock additional levels and attributes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Attribute

Attribute initializer

Options Hash (args):

  • :type,'type' (AttributeClass)

    type to assign to the attribute

  • :type_id,'type_id' (String)

    id of type to assign to attribture

  • :level,'level' (Integer)

    level to assign to attribute

  • :progression,'progression' (Float)

    progression to assign to attribute


40
41
42
43
44
45
46
47
48
49
# File 'lib/users/attribute.rb', line 40

def initialize(args = {})
  attr_from_args args, :type => nil, :level => 0, :progression => 0

  [:type_id, 'type_id'].each { |type_id|
    @type =
      AttributeClass.subclasses.find { |ac|
        ac.id == args[type_id].intern
    } if args[type_id]
  }
end

Instance Attribute Details

#levelObject

Level which this attribute is currently at


21
22
23
# File 'lib/users/attribute.rb', line 21

def level
  @level
end

#progressionObject

Progression to the next level


24
25
26
# File 'lib/users/attribute.rb', line 24

def progression
  @progression
end

#typeObject

Handle to attribute type instance specifying requirements and metadata


18
19
20
# File 'lib/users/attribute.rb', line 18

def type
  @type
end

#userObject

Handle to user owning this attribute


27
28
29
# File 'lib/users/attribute.rb', line 27

def user
  @user
end

Class Method Details

.json_create(o) ⇒ Object

Create new attribute from json representation


116
117
118
119
# File 'lib/users/attribute.rb', line 116

def self.json_create(o)
  attribute = new(o['data'])
  return attribute
end

Instance Method Details

#to_json(*a) ⇒ Object

Convert user to json representation and return it


105
106
107
108
109
110
111
112
113
# File 'lib/users/attribute.rb', line 105

def to_json(*a)
  # TODO
  {
    'json_class' => self.class.name,
    'data'       => {:type_id     => @type.id,
                     :progression => @progression,
                     :level       => @level}
  }.to_json(*a)
end

#to_sObject

Convert attribute to human readable string and return it


100
101
102
# File 'lib/users/attribute.rb', line 100

def to_s
  "attribute-#{@type.nil? ? nil : @type.id}(@#{@progression} to #{@level})"
end

#totalObject

Return the attribute 'total' or the level + progression


30
31
32
# File 'lib/users/attribute.rb', line 30

def total
  @level + @progression
end

#update!(change) ⇒ Object

Update progression of attribute.

Ensures validity of attribute, also invokes attribute_callbacks (and/or in Attribute)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/users/attribute.rb', line 55

def update!(change)
  return if change < 0 && @level == 0 && @progression == 0
  old_l = @level ; old_p = @progression ; old_t = self.total
  # TODO if change type multiplier is set, scale change down by multipler&level
  @progression += change
  until (0...1.0).include?(@progression)
    if @progression >= 1.00
      @level += 1
      @progression -= 1
    elsif @progression < 0
      @level -= 1
      @progression += 1
    end
  end

  if @level < 0
    @level = 0
    @progression = 0
  end

  # invoke callbacks
  unless @type.nil?
    if @level > old_l
      # level_up, progression
      @type.invoke_callbacks(:level_up, self)
      @type.invoke_callbacks(:progression, self)

    elsif @level < old_l
      # level_down, regression
      @type.invoke_callbacks(:level_down, self)
      @type.invoke_callbacks(:regression, self)

    elsif @progression > old_p
      # progression
      @type.invoke_callbacks(:progression, self)

    elsif @progression < old_p
      # regression
      @type.invoke_callbacks(:regression, self)

    end
  end
end