Class: Moonrope::Structure
- Inherits:
-
Object
- Object
- Moonrope::Structure
- Defined in:
- lib/moonrope/structure.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
Attributes which should be included in this structure.
-
#base ⇒ Moonrope::Base
readonly
The base API.
-
#basic ⇒ Proc
The basic data block.
-
#dsl ⇒ Moonrope::DSL::StructureDSL
readonly
The DSL.
-
#expansions ⇒ Hash
readonly
All expansions for the structure.
-
#full ⇒ Proc
The full data block.
-
#name ⇒ Symbol
The name of the structure.
Instance Method Summary collapse
-
#hash(object, options = {}) ⇒ Hash
Return a hash for this struture.
-
#initialize(base, name) { ... } ⇒ Structure
constructor
Initialize a new structure.
Constructor Details
#initialize(base, name) { ... } ⇒ Structure
Initialize a new structure
31 32 33 34 35 36 37 38 |
# File 'lib/moonrope/structure.rb', line 31 def initialize(base, name, &block) @base = base @name = name @expansions = {} @attributes = {:basic => [], :full => [], :expansion => []} @dsl = Moonrope::DSL::StructureDSL.new(self) @dsl.instance_eval(&block) if block_given? end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns attributes which should be included in this structure.
23 24 25 |
# File 'lib/moonrope/structure.rb', line 23 def attributes @attributes end |
#base ⇒ Moonrope::Base (readonly)
Returns the base API.
20 21 22 |
# File 'lib/moonrope/structure.rb', line 20 def base @base end |
#basic ⇒ Proc
Returns the basic data block.
8 9 10 |
# File 'lib/moonrope/structure.rb', line 8 def basic @basic end |
#dsl ⇒ Moonrope::DSL::StructureDSL (readonly)
Returns the DSL.
14 15 16 |
# File 'lib/moonrope/structure.rb', line 14 def dsl @dsl end |
#expansions ⇒ Hash (readonly)
Returns all expansions for the structure.
17 18 19 |
# File 'lib/moonrope/structure.rb', line 17 def expansions @expansions end |
#full ⇒ Proc
Returns the full data block.
11 12 13 |
# File 'lib/moonrope/structure.rb', line 11 def full @full end |
#name ⇒ Symbol
Returns the name of the structure.
5 6 7 |
# File 'lib/moonrope/structure.rb', line 5 def name @name end |
Instance Method Details
#hash(object, options = {}) ⇒ Hash
Return a hash for this struture
47 48 49 50 51 52 53 54 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 |
# File 'lib/moonrope/structure.rb', line 47 def hash(object, = {}) # Set up an environment environment = EvalEnvironment.new(base, [:request], :o => object) # Set a new hash hash = Hash.new # Add the 'basic' structured fields DeepMerge.deep_merge! hash_for_attributes(@attributes[:basic], object, environment), hash # Always get a basic hash to work from if self.basic.is_a?(Proc) DeepMerge.deep_merge! environment.instance_eval(&self.basic), hash end # Enhance with the full hash if requested if [:full] # Add the 'full' structured fields DeepMerge.deep_merge! hash_for_attributes(@attributes[:full], object, environment), hash if self.full.is_a?(Proc) full_hash = environment.instance_eval(&self.full) DeepMerge.deep_merge! full_hash,hash end end # Add expansions if [:expansions] # Add structured expansions @attributes[:expansion].each do |attribute| next if [:expansions].is_a?(Array) && ![:expansions].include?(attribute.name.to_sym) DeepMerge.deep_merge! hash_for_attributes([attribute], object, environment), hash end # Add the expansions expansions.each do |name, expansion| next if [:expansions].is_a?(Array) && ![:expansions].include?(name.to_sym) DeepMerge.deep_merge!({name.to_sym => environment.instance_eval(&expansion)}, hash) end end # Return the hash hash end |