Class: Kartograph::Property
- Inherits:
-
Object
- Object
- Kartograph::Property
- Defined in:
- lib/kartograph/property.rb
Instance Attribute Summary collapse
-
#map ⇒ Object
Returns the value of attribute map.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #dup ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Property
constructor
A new instance of Property.
- #key ⇒ Object
- #plural? ⇒ Boolean
- #scopes ⇒ Object
- #value_for(object, scope = nil) ⇒ Object
- #value_from(object, scope = nil) ⇒ Object
Constructor Details
#initialize(name, options = {}, &block) ⇒ Property
Returns a new instance of Property.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/kartograph/property.rb', line 5 def initialize(name, = {}, &block) @name = name @options = if mapped_class = [: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 @artist = Artist.new(@map) end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
3 4 5 |
# File 'lib/kartograph/property.rb', line 3 def map @map end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/kartograph/property.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/kartograph/property.rb', line 3 def @options end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/kartograph/property.rb', line 59 def ==(other) %i(name options map).inject(true) do |equals, method| break unless equals send(method) == other.send(method) end end |
#dup ⇒ Object
53 54 55 56 57 |
# File 'lib/kartograph/property.rb', line 53 def dup Property.new(name, .dup).tap do |property| property.map = map.dup if self.map end end |
#key ⇒ Object
23 24 25 26 |
# File 'lib/kartograph/property.rb', line 23 def key @key ||= ([:key] || name).to_s.freeze @key end |
#plural? ⇒ Boolean
49 50 51 |
# File 'lib/kartograph/property.rb', line 49 def plural? !![:plural] end |
#scopes ⇒ Object
45 46 47 |
# File 'lib/kartograph/property.rb', line 45 def scopes Array([:scopes] || []) end |
#value_for(object, scope = nil) ⇒ Object
33 34 35 36 37 |
# File 'lib/kartograph/property.rb', line 33 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
39 40 41 42 43 |
# File 'lib/kartograph/property.rb', line 39 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 |