Class: Kartograph::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/kartograph/property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Property

Returns a new instance of Property.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kartograph/property.rb', line 6

def initialize(name, options = {}, &block)
  @name = name
  @options = options

  if mapped_class = options[:include]
    # Perform a safe duplication into our properties map
    # This allows the user to define more attributes on the map should they need to
    @map = mapped_class.kartograph.dup
  end

  if block_given?
    @map ||= Map.new
    block.arity > 0 ? block.call(map) : map.instance_eval(&block)
  end
end

Instance Attribute Details

#mapObject

Returns the value of attribute map.



4
5
6
# File 'lib/kartograph/property.rb', line 4

def map
  @map
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/kartograph/property.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/kartograph/property.rb', line 3

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
56
57
# File 'lib/kartograph/property.rb', line 52

def ==(other)
  %i(name options map).inject(true) do |equals, method|
    break unless equals
    send(method) == other.send(method)
  end
end

#dupObject



46
47
48
49
50
# File 'lib/kartograph/property.rb', line 46

def dup
  Property.new(name, options.dup).tap do |property|
    property.map = map.dup if self.map
  end
end

#keyObject



22
23
24
# File 'lib/kartograph/property.rb', line 22

def key
  (options[:key] || name).to_s
end

#plural?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/kartograph/property.rb', line 42

def plural?
  !!options[:plural]
end

#scopesObject



38
39
40
# File 'lib/kartograph/property.rb', line 38

def scopes
  Array(options[:scopes] || [])
end

#value_for(object, scope = nil) ⇒ Object



26
27
28
29
30
# File 'lib/kartograph/property.rb', line 26

def value_for(object, scope = nil)
  value = object.send(name)
  return if value.nil?
  map ? artist_value(value, scope) : value
end

#value_from(object, scope = nil) ⇒ Object



32
33
34
35
36
# File 'lib/kartograph/property.rb', line 32

def value_from(object, scope = nil)
  return if object.nil?
  value = object.has_key?(key) ? object[key] : object[key.to_sym]
  map ? sculpt_value(value, scope) : value
end