Class: Rumx::Attribute

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

Direct Known Subclasses

HashAttribute, ListAttribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, description, allow_read, allow_write, options) ⇒ Attribute

Returns a new instance of Attribute.



5
6
7
8
9
10
11
12
13
# File 'lib/rumx/attribute.rb', line 5

def initialize(name, type, description, allow_read, allow_write, options)
  @name        = name.to_sym
  @type        = type
  @description = description
  # List and hash attributes might set up the object for reading but the individual elements for writing
  @allow_read  = options[:allow_read] || allow_read
  @allow_write = options[:allow_write] || allow_write
  @options     = options
end

Instance Attribute Details

#allow_readObject (readonly)

Returns the value of attribute allow_read.



3
4
5
# File 'lib/rumx/attribute.rb', line 3

def allow_read
  @allow_read
end

#allow_writeObject (readonly)

Returns the value of attribute allow_write.



3
4
5
# File 'lib/rumx/attribute.rb', line 3

def allow_write
  @allow_write
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/rumx/attribute.rb', line 3

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rumx/attribute.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/rumx/attribute.rb', line 3

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object



33
34
35
# File 'lib/rumx/attribute.rb', line 33

def [](key)
  @options[key]
end

#each_attribute_info(bean, ancestry) {|AttributeInfo.new(self, bean, ancestry+[@name], get_value(bean))| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/rumx/attribute.rb', line 19

def each_attribute_info(bean, ancestry, &block)
  yield AttributeInfo.new(self, bean, ancestry+[@name], get_value(bean))
end

#get_value(bean) ⇒ Object



15
16
17
# File 'lib/rumx/attribute.rb', line 15

def get_value(bean)
  @allow_read ? bean.send(@name) : nil
end

#param_value(params, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rumx/attribute.rb', line 37

def param_value(params, &block)
  if params.has_key?(@name)
    yield params[@name]
  elsif params.has_key?(@name.to_s)
    yield params[@name.to_s]
  end
end

#write?(bean, params) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/rumx/attribute.rb', line 23

def write?(bean, params)
  if @allow_write
    param_value(params) do |value|
      bean.send(@name.to_s+'=', @type.string_to_value(value))
      return true
    end
  end
  return false
end